hos/kernel/lang/string.h

108 lines
3.1 KiB
C++

// string.h
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 05/10/05
#ifndef __HOS_STRING__
#define __HOS_STRING__ __HOS_STRING__
class string
{
private:
/* myChars is a pointer to a character array
* It will always be a valid pointer, not null.
* The constructor is responsible for initializing
* it to point to a 1-char array with myChars[0] == 0 */
char * myChars;
/* myLength holds how many characters are in the
* string, not including the trailing null value
* (ASCII 0 character) */
unsigned int myLength;
public:
/* Basic constructors */
string();
~string();
/* Copy constructor */
string(const string & orig);
/* Construct strings from c-style strings:
* Allows declarations like string s = "data"; */
string(const char *cstring);
/* Assignment operators */
string & operator=(const string & orig);
string & operator=(const char *cstring);
/* Boolean comparison operators */
bool operator==(const string & second) const;
bool operator==(const char *cstring) const;
bool operator!=(const string & second) const;
bool operator!=(const char *cstring) const;
bool operator<(const string & second) const;
bool operator>(const string & second) const;
bool operator<(const char *cstring) const;
bool operator>(const char *cstring) const;
bool operator<=(const string & second) const;
bool operator>=(const string & second) const;
bool operator<=(const char *cstring) const;
bool operator>=(const char *cstring) const;
/* Construct strings made up of combinations of other
* strings, c-style strings, and characters */
string(const string & str1, const string & str2);
string(const string & str1, const char *cstring);
string(const char *cstring, const string & str);
string(const string & str1, char chr);
string(char chr, const string & str1);
/* Append operators */
string & operator+=(const string & str);
string & operator+=(const char *cstring);
string & operator+=(char chr);
string operator+(const string & str);
string operator+(const char *cstring);
string operator+(char chr);
/* Direct character-access operators */
const char & operator[](unsigned int index) const;
char & operator[](unsigned int index);
/* Returns handle to actual character-array for programs
* that need raw data instead of a string object */
char * data() const;
/* Returns the size of the string. This is myLength
* and is the number of characters in the string
* not counting the trailing null (ASCII 0) character */
int size() const;
};
static inline bool operator==(char *cstring, const string & str)
{ return str == cstring; }
static inline bool operator!=(char *cstring, const string & str)
{ return str != cstring; }
static inline bool operator<(const char *cstring, const string & str)
{ return str > cstring; }
static inline bool operator>(const char *cstring, const string & str)
{ return str < cstring; }
static inline bool operator<=(const char *cstring, const string & str)
{ return str >= cstring; }
static inline bool operator>=(const char *cstring, const string & str)
{ return str <= cstring; }
#endif