Category : C++ Source Code
Archive   : WHEATLIB.ZIP
Filename : STRINGS.H

 
Output of file : STRINGS.H contained in archive : WHEATLIB.ZIP
#ifndef StringsIncluded
#define StringsIncluded

// copyright (c) 1992 by Paul Wheaton
// 1916 Brooks #205, Missoula, MT 59801
//
// voice phone: (406)543-7543
// modem phone: (406)543-1144 (2400N81)
// CompuServe: 72707,207
// Internet: [email protected]

#include
#include

#define DefaultStringExtra 15
/* used internally by "Strings". Since most strings are very small yet
will have a little bit more added to them, this extra allocation should
save quite a bit of time. The only time it would be a hendrance is when
an array of strings is created */

class String;

class SubString
/* A limited form of String that could speed some string handling. Don't
use this unless you have a *real* good feel of what it can do. Not
fully implemented. */
{
char* SPtr; // pointer to where the substring starts
int SLen; // substring Length
const String* St; // pointer to String this is a SubString of
SubString(const String& S, int Index, int Len);
SubString(const SubString&);
int Compare(const char*) const;
int Compare(const String&) const;
int Compare(const SubString&) const;
friend String;
public:
const char* Ptr() { return SPtr; }
int Pos();
int Length() const { return SLen; }

void operator=(const String&);
void operator=(const SubString&);
void operator=(const char*);

Bool operator<(const String& S) { return Compare(S) < 0; }
Bool operator>(const String& S) { return Compare(S) > 0; }
Bool operator<=(const String& S) { return Compare(S) <= 0; }
Bool operator>=(const String& S) { return Compare(S) >= 0; }
Bool operator==(const String& S) { return Compare(S) == 0; }
Bool operator!=(const String& S) { return Compare(S) != 0; }

Bool operator<(const SubString& SS) { return Compare(SS)<0; }
Bool operator>(const SubString& SS) { return Compare(SS)>0; }
Bool operator<=(const SubString& SS){ return Compare(SS)<=0; }
Bool operator>=(const SubString& SS){ return Compare(SS)>=0; }
Bool operator==(const SubString& SS){ return Compare(SS)==0; }
Bool operator!=(const SubString& SS){ return Compare(SS)!=0; }

Bool operator<(const char* cs) { return Compare(cs) < 0; }
Bool operator>(const char* cs) { return Compare(cs) > 0; }
Bool operator<=(const char* cs) { return Compare(cs) <= 0; }
Bool operator>=(const char* cs) { return Compare(cs) >= 0; }
Bool operator==(const char* cs) { return Compare(cs) == 0; }
Bool operator!=(const char* cs) { return Compare(cs) != 0; }

friend Bool operator<(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}
friend Bool operator>(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}
friend Bool operator<=(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}
friend Bool operator>=(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}
friend Bool operator==(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}
friend Bool operator!=(const char* cs, const SubString& SS)
{ Bool B=(SS.Compare(cs) > 0); return B;}

String operator+(const String&);
String operator+(const SubString&);
String operator+(const char*);
friend String operator+(const char*, const SubString&);

};

class String
{
char* P; // character string
int Len; // Length of string, excluding null character
int Alloc; // amount of actual storage allocated (including null char)
void ReNew(int NewCapacity);
void New(); // Uses Alloc
friend SubString;
public:
String(const char& C, int L=1, int Extra=DefaultStringExtra);
// String S(' ',20) constructs a string consisting of 20 spaces
String(int Extra=DefaultStringExtra);
// constructor based on size desired or size not yet known
String(const char*, int Extra=DefaultStringExtra);
// a char* type string is used to construct a String type string
String(const String&, int Extra=DefaultStringExtra);
// creating one String from another
String(const SubString&, int Extra=DefaultStringExtra);
// creating a String from a SubString
~String() {delete P;}
operator const char*() const {return P;}
// this is what converts "String"s to "char*"s
char& operator[](int Index);
// To access and modify specific chars of the string just like char*.
// Completely range protected.

// assignment operators
void operator=(const String&);
void operator=(const SubString&);
void operator=(const char*);

// String to String comparison (if (Str1 Bool operator<(const String& S) const
{ return strcmp(P, S.P) < 0; }
Bool operator>(const String& S) const { return strcmp(P, S.P) > 0; }
Bool operator<=(const String& S) const { return strcmp(P, S.P) <= 0; }
Bool operator>=(const String& S) const { return strcmp(P, S.P) >= 0; }
Bool operator==(const String& S) const;
Bool operator!=(const String& S) const { return !(*this==S); }

// String to SubString comparison
Bool operator<(const SubString& SS) const { return SS.Compare(*this) > 0; }
Bool operator>(const SubString& SS) const { return SS.Compare(*this) < 0; }
Bool operator<=(const SubString& SS) const { return SS.Compare(*this) >= 0; }
Bool operator>=(const SubString& SS) const { return SS.Compare(*this) <= 0; }
Bool operator==(const SubString& SS) const { return SS.Compare(*this) == 0; }
Bool operator!=(const SubString& SS) const { return SS.Compare(*this) != 0; }

// String to char* comparison
Bool operator<(const char* CS) const { return strcmp(P,CS) < 0; }
Bool operator>(const char* CS) const { return strcmp(P,CS) > 0; }
Bool operator<=(const char* CS) const { return strcmp(P,CS) <= 0; }
Bool operator>=(const char* CS) const { return strcmp(P,CS) >= 0; }
Bool operator==(const char* CS) const { return strcmp(P,CS) == 0; }
Bool operator!=(const char* CS) const { return strcmp(P,CS) != 0; }

// char* to String comparison
friend Bool operator<(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) < 0; }
friend Bool operator>(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) > 0; }
friend Bool operator<=(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) <= 0; }
friend Bool operator>=(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) >= 0; }
friend Bool operator==(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) == 0; }
friend Bool operator!=(const char* cs, const String& S)
{ return strcmp(cs, (S.P)) != 0; }

// String concatination (S1=S2+S3)
String operator+(const String& S) const;
String operator+(const SubString& SS) const;
String operator+(const char* CS) const;
String operator+(char C) const;
friend String operator+(const char*, const SubString&);
friend String operator+(const char* CS, const String& S);
friend String operator+(char C, const String& S);

// Appending stuff to a String
void operator+=(const String&);
void operator+=(const SubString&);
void operator+=(const char* cs);
void operator+=(char C);

void Left(int NewSize); // resize string and left justify text
void Right(int NewSize);
void Center(int NewSize);
void Just(int Type, int NewSize);
// parses out to Left, Right or Center
void Tail(int NewSize); // works like "Right" cept left chars may be chopped
int Length() const { return Len; }
int ReAlloc(int NewCapacity);
void ToLower(); // force all of String to lower case
void ToUpper(); // force all of String to upper case
int Capacity() const { return Alloc - 1; }
// how big a string you can have without having to do extra memory mgmt
int Size() const { return (Len+2); } // the current size to store
int Index(char SearchChar, int StartIndex=0) const;
// returns the const NotFound if the char is not found in the string
// otherwise returns the index
int Index(const char* SearchStr, int StartIndex=0) const;
char At(int Index) const;
char operator()(int Index) const {return At(Index);}
String At(int Index, int Length) const;
String operator()(int Index, int Length) const {return At(Index,Length);}
String Before(int Index) const {return At(0,Index);}
String Through(int Index) const {return At(0,Index+1);}
String From(int Index) const {return At(Index,Len-Index);}
String After(int Index) const {return At(Index+1,Len-Index-1);}

SubString Sub(int Index, int Length)
{SubString S(*this,Index,Length); return S;}
// retrieves a substring of the optimized type SubString
SubString AtSub(int Index, int Length)
{SubString S=Sub(Index,Length); return S;}
SubString BeforeSub(int Index) {return AtSub(0,Index);}
SubString ThroughSub(int Index) {return AtSub(0,Index+1);}
SubString FromSub(int Index) {return AtSub(Index,Len-Index+1);}
SubString AfterSub(int Index) {return AtSub(Index,Len-Index);}

void Insert(char C,int Index=0);
void Insert(const char* St,int Index=0);
void Delete(int Index=0,int Length=1);

void Trim(); // chop off leading and trailing spaces
void TrimLead();
void TrimTrail();
};

inline int SubString::Pos(){return int(SPtr - (St->P));}

String Str(long); // convert integers to plain, unformatted strings
String DStr(double Num); // very slow yet works outside of SLong range
String HexStr(Long Num);
String OctalStr(Long Num);
String Form(const char* Format,double Val);
String Form(int FLen,SLong Val);
// will someday be much faster than using double
String Form(int FLen,const double& Val);
String Form(int FLen,const Long& Val);
String Form(int FLen,const float& Val);

String StringOf(int HowMany, char What);
String Spaces(int HowMany);

String LeftText(const char* CS, int NewSize);
String RightText(const char* CS, int NewSize);
String CenterText(const char* CS, int NewSize);
String JustText(const char* CS, int Type, int NewSize);
String TailText(const char* CS, int NewSize);
String Trim(const char* S);
String TrimLead(const char* S);
String TrimTrail(const char* S);

#endif


  3 Responses to “Category : C++ Source Code
Archive   : WHEATLIB.ZIP
Filename : STRINGS.H

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/