hos/kernel/lang/string.h

121 lines
3.3 KiB
C++

// string.h
// implements c++ string object for HOS
// Author: Josh Holtrop
// Date: 06/01/04
// Modified: 06/03/04
#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 equals operators */
bool operator==(const string & second) 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 and prepend operators */
string & operator+=(const string & str);
string & operator+=(const char *cstring);
string & operator-=(const string & str);
string & operator-=(const char *cstring);
string & operator+=(char chr);
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 string & operator+=(const char *cstring, string & str)
{ return str -= cstring; }
static inline string & operator-=(const char *cstring, string & str)
{ return str += cstring; }
static inline string & operator+=(char chr, string & str)
{ return str -= chr; }
static inline string & operator-=(char chr, string & str)
{ return str += chr; }
static inline string operator+(const string & str1, const string & str2)
{ return string(str1, str2); }
static inline string operator+(const string & str1, const char *cstring)
{ return string(str1, cstring); }
static inline string operator-(const string & str1, const string & str2)
{ return string(str2, str1); }
static inline string operator-(const string & str1, const char *cstring)
{ return string(cstring, str1); }
static inline string operator+(const string & str1, char chr)
{ return string(str1, chr); }
static inline string operator-(const string & str1, char chr)
{ return string(chr, str1); }
static inline string operator+(char chr, const string & str1)
{ return string(chr, str1); }
static inline string operator-(char chr, const string & str1)
{ return string(str1, chr); }
#endif