src/str.h

changeset 72
03e4d9db3fd9
parent 71
11f23fabf8a6
child 73
1ee9b312dc18
equal deleted inserted replaced
71:11f23fabf8a6 72:03e4d9db3fd9
1 /* 1 #ifndef STRING_H
2 * botc source code 2 #define STRING_H
3 * Copyright (C) 2012 Santeri `Dusk` Piippo 3
4 * All rights reserved. 4 #include <deque>
5 * 5 #include <string>
6 * Redistribution and use in source and binary forms, with or without 6 #include <stdarg.h>
7 * modification, are permitted provided that the following conditions are met: 7 #include "types.h"
8 * 8 #include "containers.h"
9 * 1. Redistributions of source code must retain the above copyright notice, 9
10 * this list of conditions and the following disclaimer. 10 class string;
11 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 class string_list;
12 * this list of conditions and the following disclaimer in the documentation 12
13 * and/or other materials provided with the distribution. 13 // =============================================================================
14 * 3. Neither the name of the developer nor the names of its contributors may 14 //
15 * be used to endorse or promote products derived from this software without 15 class string
16 * specific prior written permission. 16 {
17 * 4. Redistributions in any form must be accompanied by information on how to 17 public:
18 * obtain complete source code for the software and any accompanying 18 typedef typename ::std::string::iterator iterator;
19 * software that uses the software. The source code must either be included 19 typedef typename ::std::string::const_iterator const_iterator;
20 * in the distribution or be available for no more than the cost of 20 using length_type = int;
21 * distribution plus a nominal fee, and must be freely redistributable 21
22 * under reasonable conditions. For an executable file, complete source 22 string() {}
23 * code means the source code for all modules it contains. It does not 23
24 * include source code for modules or files that typically accompany the 24 string (char a)
25 * major components of the operating system on which the executable file 25 {
26 * runs. 26 m_string = &a;
27 * 27 }
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 string (const char* data)
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 {
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 31 m_string = data;
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 }
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 string (std::string data)
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 {
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 m_string = data;
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 }
38 * POSSIBILITY OF SUCH DAMAGE. 38
39 */ 39 void dump() const;
40 40 int compare (const string& other) const;
41 #ifndef __STR_H__ 41 bool ends_with (const string& other);
42 #define __STR_H__ 42 int count (const char needle) const;
43 43 int first (const char* c, length_type a = 0) const;
44 template<class T> class array; 44 int last (const char* c, length_type a = -1) const;
45 45 string to_lowercase() const;
46 char* vdynformat (const char* c, va_list v, unsigned int size); 46 bool is_numeric() const;
47 47 bool mask (const string& pattern) const;
48 #define SCCF_NUMBER 1<<0 48 length_type posof (int n) const;
49 #define SCCF_WORD 1<<1 49 void prepend (string a);
50 50 void replace (const char* a, const char* b);
51 // Dynamic string object, allocates memory when needed and 51 string_list split (string del) const;
52 // features a good bunch of manipulation methods 52 string_list split (char del) const;
53 class str { 53 void sprintf (const char* fmtstr, ...);
54 private: 54 bool starts_with (const string& other);
55 // The actual message 55 string strip (list< char > unwanted);
56 char* text; 56 string substring (long a, long b = -1) const;
57 57 double to_double (bool* ok = null) const;
58 // Where will append() place new characters? 58 float to_float (bool* ok = null) const;
59 unsigned int curs; 59 long to_long (bool* ok = null, int base = 10) const;
60 60 void trim (length_type n);
61 // Allocated length 61 string to_uppercase() const;
62 unsigned int alloclen; 62
63 63 string operator+ (const string data) const;
64 // Resize the text buffer to len characters 64 string operator+ (const char* data) const;
65 void resize (unsigned int len); 65 string& operator+= (const string data);
66 public: 66 string& operator+= (const char* data);
67 // ====================================================================== 67
68 // CONSTRUCTORS 68 inline bool is_empty() const
69 str (); 69 {
70 str (const char* c); 70 return m_string[0] == '\0';
71 str (char c); 71 }
72 72
73 // ====================================================================== 73 inline void append (const char* data)
74 // METHODS 74 {
75 75 m_string.append (data);
76 // Empty the string 76 }
77 void clear (); 77
78 78 inline void append (const char data)
79 // Length of the string 79 {
80 unsigned int len (); 80 m_string.push_back (data);
81 81 }
82 // The char* form of the string 82
83 char* chars (); 83 inline void append (const string& data)
84 84 {
85 // Dumps the character table of the string 85 m_string.append (data.chars());
86 void dump (); 86 }
87 87
88 // Appends text to the string 88 inline iterator begin()
89 void append (char c); 89 {
90 void append (const char* c); 90 return m_string.begin();
91 void append (str c); 91 }
92 92
93 // Appends formatted text to the string. 93 inline const_iterator begin() const
94 void appendformat (const char* c, ...); 94 {
95 void appendformat (str c, ...); 95 return m_string.cbegin();
96 96 }
97 // Returns the first occurrence of c in the string, optionally starting 97
98 // from a certain position rather than the start. 98 inline int capacity() const
99 unsigned int first (const char* c, unsigned int a = 0); 99 {
100 100 return m_string.capacity();
101 // Returns the last occurrence of c in the string, optionally starting 101 }
102 // from a certain position rather than the end. 102
103 unsigned int last (const char* c, int a = -1); 103 inline const char* chars() const
104 104 {
105 // Returns a substring of the string, from a to b. 105 return m_string.c_str();
106 str substr (unsigned int a, unsigned int b); 106 }
107 107
108 // Replace a substring with another substring. 108 inline const char* c_str() const
109 void replace (const char* o, const char* n, unsigned int a = 0); 109 {
110 110 return m_string.c_str();
111 // Removes a given index from the string, optionally more characters than just 1. 111 }
112 void remove (unsigned int idx, unsigned int dellen=1); 112
113 113 inline iterator end()
114 void trim (int dellen); 114 {
115 115 return m_string.end();
116 // Inserts a substring into a certain position. 116 }
117 void insert (char* c, unsigned int pos); 117
118 118 inline const_iterator end() const
119 // Reverses the string. 119 {
120 void reverse (); 120 return m_string.end();
121 121 }
122 // Repeats the string a given amount of times. 122
123 void repeat (unsigned int n); 123 inline void clear()
124 124 {
125 // Is the string a number? 125 m_string.clear();
126 bool isnumber (); 126 }
127 127
128 // Is the string a word, i.e consists only of alphabetic letters? 128 inline bool empty() const
129 bool isword (); 129 {
130 130 return m_string.empty();
131 // Convert string to lower case 131 }
132 str tolower (); 132
133 133 inline void erase (length_type pos)
134 // Convert string to upper case 134 {
135 str toupper (); 135 m_string.erase (m_string.begin() + pos);
136 136 }
137 // Compare this string with another 137
138 int compare (const char* c); 138 inline void insert (length_type pos, char c)
139 int compare (str c); 139 {
140 int icompare (str c); 140 m_string.insert (m_string.begin() + pos, c);
141 int icompare (const char* c); 141 }
142 142
143 // Counts the amount of substrings in the string 143 inline length_type len() const
144 unsigned int count (char* s); 144 {
145 145 return length();
146 array<str> split (str del); 146 }
147 147
148 // ====================================================================== 148 inline length_type length() const
149 // OPERATORS 149 {
150 str operator+ (str& c) { 150 return m_string.length();
151 append (c); 151 }
152 return *this; 152
153 } 153 inline int max_size() const
154 154 {
155 str& operator+= (char c) { 155 return m_string.max_size();
156 append (c); 156 }
157 return *this; 157
158 } 158 inline string mid (int a, int len) const
159 159 {
160 str& operator+= (const char* c) { 160 return m_string.substr (a, len);
161 append (c); 161 }
162 return *this; 162
163 } 163 inline void remove (int pos, int len)
164 164 {
165 str& operator+= (const str c) { 165 m_string.replace (pos, len, "");
166 append (c); 166 }
167 return *this; 167
168 } 168 inline void remove_from_start (int len)
169 169 {
170 str operator* (const int repcount) { 170 remove (0, len);
171 repeat (repcount); 171 }
172 return *this; 172
173 } 173 inline void remove_from_end (int len)
174 174 {
175 str& operator*= (const int repcount) { 175 remove (length() - len, len);
176 repeat (repcount); 176 }
177 return *this; 177
178 } 178 inline void resize (int n)
179 179 {
180 str operator- (const int trimcount) { 180 m_string.resize (n);
181 trim (trimcount); 181 }
182 return *this; 182
183 } 183 inline void replace (int pos, int n, const string& a)
184 184 {
185 str& operator-= (const int trimcount) { 185 m_string.replace (pos, n, a.chars());
186 trim (trimcount); 186 }
187 return *this; 187
188 } 188 inline void shrink_to_fit()
189 189 {
190 array<str> operator/ (str splitstring); 190 m_string.shrink_to_fit();
191 array<str> operator/ (char* splitstring); 191 }
192 array<str> operator/ (const char* splitstring); 192
193 193 inline const std::string& std_string() const
194 str operator+ () { 194 {
195 return toupper (); 195 return m_string;
196 } 196 }
197 197
198 str operator- () { 198 inline string strip (char unwanted)
199 return tolower (); 199 {
200 } 200 return strip ( {unwanted});
201 201 }
202 str operator! () { 202
203 reverse (); 203 string& operator+= (const char data)
204 return *this; 204 {
205 } 205 append (data);
206 206 return *this;
207 #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ 207 }
208 bool operator OPER (TYPE other) {return compare(other) OPER 0;} 208
209 #define DEFINE_OPERATOR(OPER) \ 209 string operator- (int n) const
210 DEFINE_OPERATOR_TYPE (OPER, str) \ 210 {
211 DEFINE_OPERATOR_TYPE (OPER, char*) \ 211 string new_string = m_string;
212 DEFINE_OPERATOR_TYPE (OPER, const char*) 212 new_string -= n;
213 213 return new_string;
214 DEFINE_OPERATOR (==) 214 }
215 DEFINE_OPERATOR (!=) 215
216 DEFINE_OPERATOR (>) 216 inline string& operator-= (int n)
217 DEFINE_OPERATOR (<) 217 {
218 DEFINE_OPERATOR (>=) 218 trim (n);
219 DEFINE_OPERATOR (<=) 219 return *this;
220 220 }
221 char operator [] (unsigned int pos) { 221
222 return text[pos]; 222 inline string operator+() const
223 } 223 {
224 224 return to_uppercase();
225 operator char* () const { 225 }
226 return text; 226
227 } 227 inline string operator-() const
228 228 {
229 operator int () const {return atoi(text);} 229 return to_lowercase();
230 operator unsigned int () const {return atoi(text);} 230 }
231
232 inline bool operator== (const string& other) const
233 {
234 return std_string() == other.std_string();
235 }
236
237 inline bool operator== (const char* other) const
238 {
239 return operator== (string (other));
240 }
241
242 inline bool operator!= (const string& other) const
243 {
244 return std_string() != other.std_string();
245 }
246
247 inline bool operator!= (const char* other) const
248 {
249 return operator!= (string (other));
250 }
251
252 inline bool operator> (const string& other) const
253 {
254 return std_string() > other.std_string();
255 }
256
257 inline bool operator< (const string& other) const
258 {
259 return std_string() < other.std_string();
260 }
261
262 inline operator const char*() const
263 {
264 return chars();
265 }
266
267 inline operator const std::string&() const
268 {
269 return std_string();
270 }
271
272 // Difference between indices @a and @b. @b can be -1, in which
273 // case it will be length() - 1.
274 inline int index_difference (int a, int b)
275 {
276 assert (b == -1 || b >= a);
277 return (b != -1 ? b - a : length() - 1 - a);
278 }
279
280 private:
281 std::string m_string;
231 }; 282 };
232 283
233 #endif // __STR_H__ 284 // =============================================================================
285 //
286 class string_list : public list<string>
287 {
288 public:
289 string_list() {}
290 string_list (std::initializer_list<string> vals) :
291 list<string> (vals) {}
292 string_list (const list<string>& a) : list<string> (a.std_deque()) {}
293 string_list (const list_type& a) : list<string> (a) {}
294
295 string join (const string& delim);
296 };
297
298 bool operator== (const char* a, const string& b);
299 string operator+ (const char* a, const string& b);
300
301 #endif // STRING_H

mercurial