3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 * Copyright (C) 2001 NaN Technologies B.V.
33 * This file was formerly known as: GEN_StdString.h.
34 * @date April, 25, 2001
37 #ifndef _STR_String_H_
38 #define _STR_String_H_
40 #ifndef STR_NO_ASSERTD
42 #define assertd(exp) ((void)NULL)
56 typedef unsigned long dword;
57 typedef const STR_String& rcSTR_String;
58 typedef unsigned char byte;
61 * Smart String Value class. Is used by parser when an expression tree is build containing string.
70 STR_String(char c, int len);
71 STR_String(const char *str);
72 STR_String(const char *str, int len);
73 STR_String(const STR_String &str);
74 STR_String(const STR_String & str, int len);
75 STR_String(const char *src1, int src1_len, const char *src2, int src2_len);
76 explicit STR_String(int val);
77 explicit STR_String(dword val);
78 explicit STR_String(float val);
79 explicit STR_String(double val);
80 inline ~STR_String() { delete[] pData; }
83 STR_String& Format(const char *fmt, ...); // Set formatted text to string
84 STR_String& FormatAdd(const char *fmt, ...); // Add formatted text to string
85 inline void Clear() { Len = pData[0] = 0; }
86 inline const STR_String & Reverse()
88 for (int i1=0, i2=Len-1; i1<i2; i1++, i2--)
89 swap(pData[i1], pData[i2]); return *this;
95 inline bool IsEmpty() const { return Len==0; }
96 inline int Length() const { return Len; }
99 inline STR_String& SetLength(int len) { AllocBuffer(len, true); Len=len; pData[len]=0; return *this; }
100 inline char GetAt(int pos) const { assertd(pos<Len); return pData[pos]; }
101 inline void SetAt(int pos, char c) { assertd(pos<Len); pData[pos]=c; }
102 inline void SetAt(int pos, rcSTR_String str);
103 inline void SetAt(int pos, int num, rcSTR_String str);
104 void Replace(int pos, rcSTR_String str);
105 void Replace(int pos, int num, rcSTR_String str);
108 inline STR_String Left(int num) const { num = (num < Len ? num:Len ); return STR_String(pData, num); }
109 inline STR_String Right(int num) const { num = (num < Len ? num:Len ); return STR_String(pData+Len-num, num); }
110 inline STR_String Mid(int pos, int num = INT_MAX) const { pos = (pos < Len ? pos:Len ); num = (num < (Len - pos) ? num : (Len - pos)); return STR_String(pData+pos, num); }
113 int Compare(rcSTR_String rhs) const;
114 int CompareNoCase(rcSTR_String rhs) const;
115 inline bool IsEqual(rcSTR_String rhs) const { return (Compare(rhs)==0); }
116 inline bool IsEqualNoCase(rcSTR_String rhs) const { return (CompareNoCase(rhs)==0); }
119 int Find(char c, int pos = 0) const;
120 int Find(const char *str, int pos = 0) const;
121 int Find(rcSTR_String str, int pos = 0) const;
122 int RFind(char c) const;
123 int FindOneOf(const char *set, int pos = 0) const;
124 int RFindOneOf(const char *set, int pos = 0) const;
126 vector<STR_String> Explode(char c) const;
131 STR_String& Capitalize();
132 STR_String& TrimLeft();
133 STR_String& TrimLeft(char *set);
134 STR_String& TrimRight();
135 STR_String& TrimRight(char *set);
137 STR_String& Trim(char *set);
138 STR_String& TrimQuotes();
141 // inline operator char*() { return pData; }
142 inline operator const char *() const { return pData; }
143 inline char *Ptr() { return pData; }
144 inline const char *ReadPtr() const { return pData; }
145 inline float ToFloat() const { float x=(float)(atof(pData)); return x; }
146 inline int ToInt() const { return atoi(pData); }
149 inline rcSTR_String operator=(const byte *rhs) { return Copy((const char *)rhs, strlen((const char *)rhs)); }
150 inline rcSTR_String operator=(rcSTR_String rhs) { return Copy(rhs.ReadPtr(), rhs.Length()); }
151 inline rcSTR_String operator=(char rhs) { return Copy(&rhs, 1); }
152 inline rcSTR_String operator=(const char *rhs) { return Copy(rhs, strlen(rhs)); }
154 inline rcSTR_String operator+=(const char *rhs) { return Concat(rhs, strlen(rhs)); }
155 inline rcSTR_String operator+=(rcSTR_String rhs) { return Concat(rhs.ReadPtr(), rhs.Length()); }
156 inline rcSTR_String operator+=(char rhs) { return Concat(&rhs, 1); }
159 inline friend bool operator<(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<0); }
160 inline friend bool operator<(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)<0); };
161 inline friend bool operator<(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<0); }
162 inline friend bool operator>(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>0); }
163 inline friend bool operator>(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)>0); }
164 inline friend bool operator>(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>0); }
165 inline friend bool operator<=(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<=0); }
166 inline friend bool operator<=(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)<=0); }
167 inline friend bool operator<=(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)<=0); }
168 inline friend bool operator>=(rcSTR_String lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>=0); }
169 inline friend bool operator>=(rcSTR_String lhs, const char *rhs) { return (strcmp(lhs, rhs)>=0); }
170 inline friend bool operator>=(const char *lhs, rcSTR_String rhs) { return (strcmp(lhs, rhs)>=0); }
171 inline friend bool operator==(rcSTR_String lhs, rcSTR_String rhs) { return ((lhs.Length() == rhs.Length()) && (memcmp(lhs, rhs, lhs.Length())==0)); }
172 inline friend bool operator==(rcSTR_String lhs, const char *rhs) { return (memcmp(lhs, rhs, lhs.Length()+1)==0); }
173 inline friend bool operator==(const char *lhs, rcSTR_String rhs) { return (memcmp(lhs, rhs, rhs.Length()+1)==0); }
174 inline friend bool operator!=(rcSTR_String lhs, rcSTR_String rhs) { return ((lhs.Length() != rhs.Length()) || (memcmp(lhs, rhs, lhs.Length())!=0)); }
175 inline friend bool operator!=(rcSTR_String lhs, const char *rhs) { return (memcmp(lhs, rhs, lhs.Length()+1)!=0); }
176 inline friend bool operator!=(const char *lhs, rcSTR_String rhs) { return (memcmp(lhs, rhs, rhs.Length()+1)!=0); }
179 //int Serialize(pCStream stream);
183 void AllocBuffer(int len, bool keep_contents);
184 rcSTR_String Copy(const char *src, int len);
185 rcSTR_String Concat(const char *data, int len);
187 static bool isLower(char c) { return !isUpper(c); }
188 static bool isUpper(char c) { return (c>='A') && (c <= 'Z'); }
189 static bool isSpace(char c) { return (c==' ') || (c=='\t'); }
191 char *pData; // -> STR_String data
192 int Len; // Data length
193 int Max; // Space in data buffer
196 inline STR_String operator+(rcSTR_String lhs, rcSTR_String rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), rhs.ReadPtr(), rhs.Length()); }
197 inline STR_String operator+(rcSTR_String lhs, char rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), &rhs, 1); }
198 inline STR_String operator+(char lhs, rcSTR_String rhs) { return STR_String(&lhs, 1, rhs.ReadPtr(), rhs.Length()); }
199 inline STR_String operator+(rcSTR_String lhs, const char *rhs) { return STR_String(lhs.ReadPtr(), lhs.Length(), rhs, strlen(rhs)); }
200 inline STR_String operator+(const char *lhs, rcSTR_String rhs) { return STR_String(lhs, strlen(lhs), rhs.ReadPtr(), rhs.Length()); }
203 #endif //_STR_String_H_