| |
1 /* |
| |
2 * botc source code |
| |
3 * Copyright (C) 2012 Santeri `Dusk` Piippo |
| |
4 * All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright notice, |
| |
10 * this list of conditions and the following disclaimer. |
| |
11 * 2. Redistributions in binary form must reproduce the above copyright notice, |
| |
12 * this list of conditions and the following disclaimer in the documentation |
| |
13 * and/or other materials provided with the distribution. |
| |
14 * 3. Neither the name of the developer nor the names of its contributors may |
| |
15 * be used to endorse or promote products derived from this software without |
| |
16 * specific prior written permission. |
| |
17 * 4. Redistributions in any form must be accompanied by information on how to |
| |
18 * obtain complete source code for the software and any accompanying |
| |
19 * software that uses the software. The source code must either be included |
| |
20 * in the distribution or be available for no more than the cost of |
| |
21 * distribution plus a nominal fee, and must be freely redistributable |
| |
22 * under reasonable conditions. For an executable file, complete source |
| |
23 * code means the source code for all modules it contains. It does not |
| |
24 * include source code for modules or files that typically accompany the |
| |
25 * major components of the operating system on which the executable file |
| |
26 * runs. |
| |
27 * |
| |
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
38 * POSSIBILITY OF SUCH DAMAGE. |
| |
39 */ |
| |
40 |
| |
41 #ifndef __STR_H__ |
| |
42 #define __STR_H__ |
| |
43 |
| |
44 template<class T> class array; |
| |
45 |
| |
46 char* vdynformat (const char* c, va_list v, unsigned int size); |
| |
47 |
| |
48 #define SCCF_NUMBER 1<<0 |
| |
49 #define SCCF_WORD 1<<1 |
| |
50 |
| |
51 // Dynamic string object, allocates memory when needed and |
| |
52 // features a good bunch of manipulation methods |
| |
53 class str { |
| |
54 private: |
| |
55 // The actual message |
| |
56 char* text; |
| |
57 |
| |
58 // Where will append() place new characters? |
| |
59 unsigned int curs; |
| |
60 |
| |
61 // Allocated length |
| |
62 unsigned int alloclen; |
| |
63 |
| |
64 // Resize the text buffer to len characters |
| |
65 void resize (unsigned int len); |
| |
66 public: |
| |
67 // ====================================================================== |
| |
68 // CONSTRUCTORS |
| |
69 str (); |
| |
70 str (const char* c); |
| |
71 str (char c); |
| |
72 |
| |
73 // ====================================================================== |
| |
74 // METHODS |
| |
75 |
| |
76 // Empty the string |
| |
77 void clear (); |
| |
78 |
| |
79 // Length of the string |
| |
80 unsigned int len (); |
| |
81 |
| |
82 // The char* form of the string |
| |
83 char* chars (); |
| |
84 |
| |
85 // Dumps the character table of the string |
| |
86 void dump (); |
| |
87 |
| |
88 // Appends text to the string |
| |
89 void append (char c); |
| |
90 void append (const char* c); |
| |
91 void append (str c); |
| |
92 |
| |
93 // Appends formatted text to the string. |
| |
94 void appendformat (const char* c, ...); |
| |
95 void appendformat (str c, ...); |
| |
96 |
| |
97 // Returns the first occurrence of c in the string, optionally starting |
| |
98 // from a certain position rather than the start. |
| |
99 unsigned int first (const char* c, unsigned int a = 0); |
| |
100 |
| |
101 // Returns the last occurrence of c in the string, optionally starting |
| |
102 // from a certain position rather than the end. |
| |
103 unsigned int last (const char* c, int a = -1); |
| |
104 |
| |
105 // Returns a substring of the string, from a to b. |
| |
106 str substr (unsigned int a, unsigned int b); |
| |
107 |
| |
108 // Replace a substring with another substring. |
| |
109 void replace (const char* o, const char* n, unsigned int a = 0); |
| |
110 |
| |
111 // Removes a given index from the string, optionally more characters than just 1. |
| |
112 void remove (unsigned int idx, unsigned int dellen=1); |
| |
113 |
| |
114 void trim (int dellen); |
| |
115 |
| |
116 // Inserts a substring into a certain position. |
| |
117 void insert (char* c, unsigned int pos); |
| |
118 |
| |
119 // Reverses the string. |
| |
120 void reverse (); |
| |
121 |
| |
122 // Repeats the string a given amount of times. |
| |
123 void repeat (unsigned int n); |
| |
124 |
| |
125 // Is the string a number? |
| |
126 bool isnumber (); |
| |
127 |
| |
128 // Is the string a word, i.e consists only of alphabetic letters? |
| |
129 bool isword (); |
| |
130 |
| |
131 // Convert string to lower case |
| |
132 str tolower (); |
| |
133 |
| |
134 // Convert string to upper case |
| |
135 str toupper (); |
| |
136 |
| |
137 // Compare this string with another |
| |
138 int compare (const char* c); |
| |
139 int compare (str c); |
| |
140 int icompare (str c); |
| |
141 int icompare (const char* c); |
| |
142 |
| |
143 // Counts the amount of substrings in the string |
| |
144 unsigned int count (char* s); |
| |
145 |
| |
146 array<str> split (str del); |
| |
147 |
| |
148 // ====================================================================== |
| |
149 // OPERATORS |
| |
150 str operator+ (str& c) { |
| |
151 append (c); |
| |
152 return *this; |
| |
153 } |
| |
154 |
| |
155 str& operator+= (char c) { |
| |
156 append (c); |
| |
157 return *this; |
| |
158 } |
| |
159 |
| |
160 str& operator+= (const char* c) { |
| |
161 append (c); |
| |
162 return *this; |
| |
163 } |
| |
164 |
| |
165 str& operator+= (const str c) { |
| |
166 append (c); |
| |
167 return *this; |
| |
168 } |
| |
169 |
| |
170 str operator* (const int repcount) { |
| |
171 repeat (repcount); |
| |
172 return *this; |
| |
173 } |
| |
174 |
| |
175 str& operator*= (const int repcount) { |
| |
176 repeat (repcount); |
| |
177 return *this; |
| |
178 } |
| |
179 |
| |
180 str operator- (const int trimcount) { |
| |
181 trim (trimcount); |
| |
182 return *this; |
| |
183 } |
| |
184 |
| |
185 str& operator-= (const int trimcount) { |
| |
186 trim (trimcount); |
| |
187 return *this; |
| |
188 } |
| |
189 |
| |
190 array<str> operator/ (str splitstring); |
| |
191 array<str> operator/ (char* splitstring); |
| |
192 array<str> operator/ (const char* splitstring); |
| |
193 |
| |
194 str operator+ () { |
| |
195 return toupper (); |
| |
196 } |
| |
197 |
| |
198 str operator- () { |
| |
199 return tolower (); |
| |
200 } |
| |
201 |
| |
202 str operator! () { |
| |
203 reverse (); |
| |
204 return *this; |
| |
205 } |
| |
206 |
| |
207 #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ |
| |
208 bool operator OPER (TYPE other) {return compare(other) OPER 0;} |
| |
209 #define DEFINE_OPERATOR(OPER) \ |
| |
210 DEFINE_OPERATOR_TYPE (OPER, str) \ |
| |
211 DEFINE_OPERATOR_TYPE (OPER, char*) \ |
| |
212 DEFINE_OPERATOR_TYPE (OPER, const char*) |
| |
213 |
| |
214 DEFINE_OPERATOR (==) |
| |
215 DEFINE_OPERATOR (!=) |
| |
216 DEFINE_OPERATOR (>) |
| |
217 DEFINE_OPERATOR (<) |
| |
218 DEFINE_OPERATOR (>=) |
| |
219 DEFINE_OPERATOR (<=) |
| |
220 |
| |
221 char operator [] (unsigned int pos) { |
| |
222 return text[pos]; |
| |
223 } |
| |
224 |
| |
225 operator char* () const { |
| |
226 return text; |
| |
227 } |
| |
228 |
| |
229 operator int () const {return atoi(text);} |
| |
230 operator unsigned int () const {return atoi(text);} |
| |
231 }; |
| |
232 |
| |
233 #endif // __STR_H__ |