Mon, 18 Mar 2013 04:03:05 +0200
added dummy action for future inlining command. Also GCC says that deleting instances of classes with virtual members but no virtual destructors is bad.
0 | 1 | #ifndef __STR_H__ |
2 | #define __STR_H__ | |
3 | ||
4 | #include <string.h> | |
5 | #include <stdlib.h> | |
6 | #include <stdarg.h> | |
7 | // #include <initializer_list> | |
8 | #include <vector> | |
9 | ||
10 | #ifdef QT_VERSION | |
11 | #include <QString> | |
12 | #endif // QT_VERSION | |
13 | ||
14 | char* vdynformat (const char* csFormat, va_list vArgs, long int lSize); | |
15 | ||
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
16 | class vertex; |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
17 | |
0 | 18 | // Dynamic string object, allocates memory when needed and |
19 | // features a good bunch of manipulation methods | |
20 | class str { | |
21 | private: | |
22 | // The actual message | |
23 | char* text; | |
24 | ||
25 | // Where will append() place new characters? | |
26 | unsigned short curs; | |
27 | ||
28 | // Allocated length | |
29 | unsigned short alloclen; | |
30 | ||
31 | // Resize the text buffer to len characters | |
32 | void resize (unsigned int len); | |
33 | ||
34 | public: | |
35 | // ====================================================================== | |
36 | str (); | |
37 | str (const char* c); | |
38 | str (char c); | |
39 | ~str (); | |
40 | ||
41 | static str mkfmt (const char* fmt, ...) { | |
42 | va_list va; | |
43 | char* buf; | |
44 | ||
45 | va_start (va, fmt); | |
46 | buf = vdynformat (fmt, va, 256); | |
47 | va_end (va); | |
48 | ||
49 | str val = buf; | |
50 | delete[] buf; | |
51 | return val; | |
52 | } | |
53 | ||
54 | // ====================================================================== | |
55 | // METHODS | |
56 | ||
57 | // Empty the string | |
58 | void clear (); | |
59 | ||
60 | // Length of the string | |
61 | size_t len () { | |
62 | return strlen (text); | |
63 | } | |
64 | ||
65 | // The char* form of the string | |
66 | char* chars (); | |
67 | ||
68 | // Dumps the character table of the string | |
69 | void dump (); | |
70 | ||
71 | // Appends text to the string | |
72 | void append (char c); | |
73 | void append (const char* c); | |
74 | void append (str c); | |
75 | ||
76 | // Formats text to the string. | |
77 | void format (const char* fmt, ...); | |
78 | str format (...); | |
79 | ||
80 | // Appends formatted text to the string. | |
81 | void appendformat (const char* c, ...); | |
82 | ||
83 | // Returns the first occurrence of c in the string, optionally starting | |
84 | // from a certain position rather than the start. | |
85 | int first (const char* c, unsigned int a = 0); | |
86 | ||
87 | // Returns the last occurrence of c in the string, optionally starting | |
88 | // from a certain position rather than the end. | |
89 | int last (const char* c, int a = -1); | |
90 | ||
91 | // Returns a substring of the string, from a to b. | |
92 | str substr (unsigned int a, unsigned int b); | |
93 | ||
94 | // Replace a substring with another substring. | |
95 | void replace (const char* o, const char* n, unsigned int a = 0); | |
96 | ||
97 | // Removes a given index from the string, optionally more characters than just 1. | |
98 | void remove (unsigned int idx, unsigned int dellen=1); | |
99 | ||
100 | str trim (int dellen); | |
101 | ||
102 | // Inserts a substring into a certain position. | |
103 | void insert (char* c, unsigned int pos); | |
104 | ||
105 | // Reverses the string. | |
106 | str reverse (); | |
107 | ||
108 | // Repeats the string a given amount of times. | |
109 | str repeat (int n); | |
110 | ||
111 | // Is the string a number? | |
112 | bool isnumber (); | |
113 | ||
114 | // Is the string a word, i.e consists only of alphabetic letters? | |
115 | bool isword (); | |
116 | ||
117 | // Convert string to lower case | |
118 | str tolower (); | |
119 | ||
120 | // Convert string to upper case | |
121 | str toupper (); | |
122 | ||
123 | // Compare this string with another | |
124 | int compare (const char* c); | |
125 | int compare (str c); | |
126 | int icompare (str c); | |
127 | int icompare (const char* c); | |
128 | ||
129 | // Counts the amount of substrings in the string | |
130 | unsigned int count (char* s); | |
131 | unsigned int count (char s); | |
132 | ||
133 | // Counts where the given substring is seen for the nth time | |
134 | int instanceof (const char* s, unsigned n); | |
135 | ||
136 | char subscript (uint pos) { | |
137 | return operator[] (pos); | |
138 | } | |
139 | ||
140 | std::vector<str> split (str del); | |
141 | ||
142 | /* | |
143 | void strip (char c); | |
144 | void strip (std::initializer_list<char> unwanted); | |
145 | */ | |
146 | ||
147 | // ====================================================================== | |
148 | // OPERATORS | |
149 | str operator+ (str& c) { | |
150 | append (c); | |
151 | return *this; | |
152 | } | |
153 | ||
154 | str& operator+= (char c) { | |
155 | append (c); | |
156 | return *this; | |
157 | } | |
158 | ||
159 | str& operator+= (const char* c) { | |
160 | append (c); | |
161 | return *this; | |
162 | } | |
163 | ||
164 | str& operator+= (const str c) { | |
165 | append (c); | |
166 | return *this; | |
167 | } | |
168 | ||
18
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
169 | str& operator+= (vertex vrt); |
a6732098fed8
Convert the static getCoordinateRep to a common ftoa, use this function to get proper coordinate representation when converting objects to LDraw code
Santeri Piippo <crimsondusk64@gmail.com>
parents:
0
diff
changeset
|
170 | |
0 | 171 | str operator* (const int repcount) { |
172 | repeat (repcount); | |
173 | return *this; | |
174 | } | |
175 | ||
176 | str& operator*= (const int repcount) { | |
177 | str other = repeat (repcount); | |
178 | clear (); | |
179 | append (other); | |
180 | return *this; | |
181 | } | |
182 | ||
183 | str operator- (const int trimcount) { | |
184 | return trim (trimcount); | |
185 | } | |
186 | ||
187 | str& operator-= (const int trimcount) { | |
188 | str other = trim (trimcount); | |
189 | clear (); | |
190 | append (other); | |
191 | return *this; | |
192 | } | |
193 | ||
194 | std::vector<str> operator/ (str splitstring); | |
195 | std::vector<str> operator/ (char* splitstring); | |
196 | std::vector<str> operator/ (const char* splitstring); | |
197 | ||
198 | int operator% (str splitstring) { | |
199 | return count (splitstring.chars()); | |
200 | } | |
201 | ||
202 | int operator% (char* splitstring) { | |
203 | return count (splitstring); | |
204 | } | |
205 | ||
206 | int operator% (const char* splitstring) { | |
207 | return count (str (splitstring).chars()); | |
208 | } | |
209 | ||
210 | str operator+ () { | |
211 | return toupper (); | |
212 | } | |
213 | ||
214 | str operator- () { | |
215 | return tolower (); | |
216 | } | |
217 | ||
218 | str operator! () { | |
219 | return reverse (); | |
220 | } | |
221 | ||
222 | size_t operator~ () { | |
223 | return len (); | |
224 | } | |
225 | ||
226 | #define DEFINE_OPERATOR_TYPE(OPER, TYPE) \ | |
227 | bool operator OPER (TYPE other) {return compare(other) OPER 0;} | |
228 | #define DEFINE_OPERATOR(OPER) \ | |
229 | DEFINE_OPERATOR_TYPE (OPER, str) \ | |
230 | DEFINE_OPERATOR_TYPE (OPER, char*) \ | |
231 | DEFINE_OPERATOR_TYPE (OPER, const char*) | |
232 | ||
233 | DEFINE_OPERATOR (==) | |
234 | DEFINE_OPERATOR (!=) | |
235 | DEFINE_OPERATOR (>) | |
236 | DEFINE_OPERATOR (<) | |
237 | DEFINE_OPERATOR (>=) | |
238 | DEFINE_OPERATOR (<=) | |
239 | ||
240 | char& operator[] (int pos) { | |
241 | return text[pos]; | |
242 | } | |
243 | ||
244 | operator char* () const { | |
245 | return text; | |
246 | } | |
247 | ||
248 | #ifdef QT_VERSION | |
249 | operator QString () const { | |
250 | return text; | |
251 | } | |
252 | #endif // QT_VERSION | |
253 | ||
254 | operator int () const { | |
255 | return atoi (text); | |
256 | } | |
257 | ||
258 | operator uint () const { | |
259 | return operator int(); | |
260 | } | |
261 | }; | |
262 | ||
263 | #endif // __STR_H__ |