sanguino/cores/arduino/WString.cpp

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 WString.cpp - String library for Wiring & Arduino
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 ...mostly rewritten by Paul Stoffregen...
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4 Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 Copyright 2011, Paul Stoffregen, paul@pjrc.com
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 #include "WString.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 /* Constructors */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 String::String(const char *cstr)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 if (cstr) copy(cstr, strlen(cstr));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 String::String(const String &value)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 *this = value;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 #ifdef __GXX_EXPERIMENTAL_CXX0X__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 String::String(String &&rval)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 move(rval);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 String::String(StringSumHelper &&rval)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 move(rval);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54 String::String(char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 char buf[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58 buf[0] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 buf[1] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 String::String(unsigned char value, unsigned char base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66 char buf[9];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 utoa(value, buf, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 String::String(int value, unsigned char base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 char buf[18];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75 itoa(value, buf, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79 String::String(unsigned int value, unsigned char base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 char buf[17];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83 utoa(value, buf, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87 String::String(long value, unsigned char base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 char buf[34];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 ltoa(value, buf, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95 String::String(unsigned long value, unsigned char base)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
97 init();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
98 char buf[33];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
99 ultoa(value, buf, base);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
100 *this = buf;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
101 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
102
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
103 String::~String()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
104 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
105 free(buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
106 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
107
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
108 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
109 /* Memory Management */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
110 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
111
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
112 inline void String::init(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
113 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
114 buffer = NULL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
115 capacity = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
116 len = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
117 flags = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
118 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
119
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
120 void String::invalidate(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
121 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
122 if (buffer) free(buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
123 buffer = NULL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
124 capacity = len = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
125 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
126
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
127 unsigned char String::reserve(unsigned int size)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
128 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
129 if (buffer && capacity >= size) return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
130 if (changeBuffer(size)) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
131 if (len == 0) buffer[0] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
132 return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
133 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
134 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
135 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
136
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
137 unsigned char String::changeBuffer(unsigned int maxStrLen)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
138 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
139 char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
140 if (newbuffer) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
141 buffer = newbuffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
142 capacity = maxStrLen;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
143 return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
144 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
145 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
146 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
147
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
148 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
149 /* Copy and Move */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
150 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
151
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
152 String & String::copy(const char *cstr, unsigned int length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
153 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
154 if (!reserve(length)) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
155 invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
156 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
157 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
158 len = length;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
159 strcpy(buffer, cstr);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
160 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
161 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
162
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
163 #ifdef __GXX_EXPERIMENTAL_CXX0X__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
164 void String::move(String &rhs)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
165 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
166 if (buffer) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
167 if (capacity >= rhs.len) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
168 strcpy(buffer, rhs.buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
169 len = rhs.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
170 rhs.len = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
171 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
172 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
173 free(buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
174 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
175 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
176 buffer = rhs.buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
177 capacity = rhs.capacity;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
178 len = rhs.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
179 rhs.buffer = NULL;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
180 rhs.capacity = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
181 rhs.len = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
182 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
183 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
184
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
185 String & String::operator = (const String &rhs)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
186 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
187 if (this == &rhs) return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
188
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
189 if (rhs.buffer) copy(rhs.buffer, rhs.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
190 else invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
191
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
192 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
193 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
194
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
195 #ifdef __GXX_EXPERIMENTAL_CXX0X__
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
196 String & String::operator = (String &&rval)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
197 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
198 if (this != &rval) move(rval);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
199 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
200 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
201
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
202 String & String::operator = (StringSumHelper &&rval)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
203 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
204 if (this != &rval) move(rval);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
205 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
206 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
207 #endif
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
208
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
209 String & String::operator = (const char *cstr)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
210 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
211 if (cstr) copy(cstr, strlen(cstr));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
212 else invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
213
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
214 return *this;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
215 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
216
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
217 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
218 /* concat */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
219 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
220
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
221 unsigned char String::concat(const String &s)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
222 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
223 return concat(s.buffer, s.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
224 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
225
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
226 unsigned char String::concat(const char *cstr, unsigned int length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
227 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
228 unsigned int newlen = len + length;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
229 if (!cstr) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
230 if (length == 0) return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
231 if (!reserve(newlen)) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
232 strcpy(buffer + len, cstr);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
233 len = newlen;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
234 return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
235 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
236
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
237 unsigned char String::concat(const char *cstr)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
238 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
239 if (!cstr) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
240 return concat(cstr, strlen(cstr));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
241 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
242
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
243 unsigned char String::concat(char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
244 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
245 char buf[2];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
246 buf[0] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
247 buf[1] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
248 return concat(buf, 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
249 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
250
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
251 unsigned char String::concat(unsigned char num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
252 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
253 char buf[4];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
254 itoa(num, buf, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
255 return concat(buf, strlen(buf));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
256 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
257
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
258 unsigned char String::concat(int num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
259 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
260 char buf[7];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
261 itoa(num, buf, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
262 return concat(buf, strlen(buf));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
263 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
264
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
265 unsigned char String::concat(unsigned int num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
266 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
267 char buf[6];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
268 utoa(num, buf, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
269 return concat(buf, strlen(buf));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
270 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
271
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
272 unsigned char String::concat(long num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
273 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
274 char buf[12];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
275 ltoa(num, buf, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
276 return concat(buf, strlen(buf));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
277 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
278
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
279 unsigned char String::concat(unsigned long num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
280 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
281 char buf[11];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
282 ultoa(num, buf, 10);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
283 return concat(buf, strlen(buf));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
284 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
285
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
286 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
287 /* Concatenate */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
288 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
289
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
290 StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
291 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
292 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
293 if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
294 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
295 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
296
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
297 StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
298 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
299 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
300 if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
301 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
302 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
303
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
304 StringSumHelper & operator + (const StringSumHelper &lhs, char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
305 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
306 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
307 if (!a.concat(c)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
308 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
309 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
310
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
311 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
312 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
313 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
314 if (!a.concat(num)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
315 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
316 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
317
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
318 StringSumHelper & operator + (const StringSumHelper &lhs, int num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
319 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
320 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
321 if (!a.concat(num)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
322 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
323 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
324
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
325 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
326 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
327 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
328 if (!a.concat(num)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
329 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
330 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
331
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
332 StringSumHelper & operator + (const StringSumHelper &lhs, long num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
333 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
334 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
335 if (!a.concat(num)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
336 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
337 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
338
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
339 StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
340 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
341 StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
342 if (!a.concat(num)) a.invalidate();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
343 return a;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
344 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
345
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
346 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
347 /* Comparison */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
348 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
349
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
350 int String::compareTo(const String &s) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
351 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
352 if (!buffer || !s.buffer) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
353 if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
354 if (buffer && len > 0) return *(unsigned char *)buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
355 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
356 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
357 return strcmp(buffer, s.buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
358 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
359
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
360 unsigned char String::equals(const String &s2) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
361 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
362 return (len == s2.len && compareTo(s2) == 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
363 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
364
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
365 unsigned char String::equals(const char *cstr) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
366 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
367 if (len == 0) return (cstr == NULL || *cstr == 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
368 if (cstr == NULL) return buffer[0] == 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
369 return strcmp(buffer, cstr) == 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
370 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
371
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
372 unsigned char String::operator<(const String &rhs) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
373 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
374 return compareTo(rhs) < 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
375 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
376
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
377 unsigned char String::operator>(const String &rhs) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
378 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
379 return compareTo(rhs) > 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
380 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
381
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
382 unsigned char String::operator<=(const String &rhs) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
383 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
384 return compareTo(rhs) <= 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
385 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
386
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
387 unsigned char String::operator>=(const String &rhs) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
388 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
389 return compareTo(rhs) >= 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
390 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
391
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
392 unsigned char String::equalsIgnoreCase( const String &s2 ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
393 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
394 if (this == &s2) return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
395 if (len != s2.len) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
396 if (len == 0) return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
397 const char *p1 = buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
398 const char *p2 = s2.buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
399 while (*p1) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
400 if (tolower(*p1++) != tolower(*p2++)) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
401 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
402 return 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
403 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
404
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
405 unsigned char String::startsWith( const String &s2 ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
406 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
407 if (len < s2.len) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
408 return startsWith(s2, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
409 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
410
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
411 unsigned char String::startsWith( const String &s2, unsigned int offset ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
412 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
413 if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
414 return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
415 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
416
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
417 unsigned char String::endsWith( const String &s2 ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
418 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
419 if ( len < s2.len || !buffer || !s2.buffer) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
420 return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
421 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
422
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
423 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
424 /* Character Access */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
425 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
426
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
427 char String::charAt(unsigned int loc) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
428 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
429 return operator[](loc);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
430 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
431
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
432 void String::setCharAt(unsigned int loc, char c)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
433 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
434 if (loc < len) buffer[loc] = c;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
435 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
436
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
437 char & String::operator[](unsigned int index)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
438 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
439 static char dummy_writable_char;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
440 if (index >= len || !buffer) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
441 dummy_writable_char = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
442 return dummy_writable_char;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
443 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
444 return buffer[index];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
445 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
446
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
447 char String::operator[]( unsigned int index ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
448 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
449 if (index >= len || !buffer) return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
450 return buffer[index];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
451 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
452
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
453 void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
454 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
455 if (!bufsize || !buf) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
456 if (index >= len) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
457 buf[0] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
458 return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
459 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
460 unsigned int n = bufsize - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
461 if (n > len - index) n = len - index;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
462 strncpy((char *)buf, buffer + index, n);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
463 buf[n] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
464 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
465
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
466 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
467 /* Search */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
468 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
469
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
470 int String::indexOf(char c) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
471 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
472 return indexOf(c, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
473 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
474
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
475 int String::indexOf( char ch, unsigned int fromIndex ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
476 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
477 if (fromIndex >= len) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
478 const char* temp = strchr(buffer + fromIndex, ch);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
479 if (temp == NULL) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
480 return temp - buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
481 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
482
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
483 int String::indexOf(const String &s2) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
484 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
485 return indexOf(s2, 0);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
486 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
487
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
488 int String::indexOf(const String &s2, unsigned int fromIndex) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
489 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
490 if (fromIndex >= len) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
491 const char *found = strstr(buffer + fromIndex, s2.buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
492 if (found == NULL) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
493 return found - buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
494 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
495
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
496 int String::lastIndexOf( char theChar ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
497 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
498 return lastIndexOf(theChar, len - 1);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
499 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
500
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
501 int String::lastIndexOf(char ch, unsigned int fromIndex) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
502 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
503 if (fromIndex >= len) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
504 char tempchar = buffer[fromIndex + 1];
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
505 buffer[fromIndex + 1] = '\0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
506 char* temp = strrchr( buffer, ch );
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
507 buffer[fromIndex + 1] = tempchar;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
508 if (temp == NULL) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
509 return temp - buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
510 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
511
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
512 int String::lastIndexOf(const String &s2) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
513 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
514 return lastIndexOf(s2, len - s2.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
515 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
516
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
517 int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
518 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
519 if (s2.len == 0 || len == 0 || s2.len > len) return -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
520 if (fromIndex >= len) fromIndex = len - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
521 int found = -1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
522 for (char *p = buffer; p <= buffer + fromIndex; p++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
523 p = strstr(p, s2.buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
524 if (!p) break;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
525 if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
526 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
527 return found;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
528 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
529
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
530 String String::substring( unsigned int left ) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
531 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
532 return substring(left, len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
533 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
534
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
535 String String::substring(unsigned int left, unsigned int right) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
536 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
537 if (left > right) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
538 unsigned int temp = right;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
539 right = left;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
540 left = temp;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
541 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
542 String out;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
543 if (left > len) return out;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
544 if (right > len) right = len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
545 char temp = buffer[right]; // save the replaced character
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
546 buffer[right] = '\0';
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
547 out = buffer + left; // pointer arithmetic
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
548 buffer[right] = temp; //restore character
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
549 return out;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
550 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
551
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
552 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
553 /* Modification */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
554 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
555
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
556 void String::replace(char find, char replace)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
557 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
558 if (!buffer) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
559 for (char *p = buffer; *p; p++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
560 if (*p == find) *p = replace;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
561 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
562 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
563
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
564 void String::replace(const String& find, const String& replace)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
565 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
566 if (len == 0 || find.len == 0) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
567 int diff = replace.len - find.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
568 char *readFrom = buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
569 char *foundAt;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
570 if (diff == 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
571 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
572 memcpy(foundAt, replace.buffer, replace.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
573 readFrom = foundAt + replace.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
574 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
575 } else if (diff < 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
576 char *writeTo = buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
577 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
578 unsigned int n = foundAt - readFrom;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
579 memcpy(writeTo, readFrom, n);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
580 writeTo += n;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
581 memcpy(writeTo, replace.buffer, replace.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
582 writeTo += replace.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
583 readFrom = foundAt + find.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
584 len += diff;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
585 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
586 strcpy(writeTo, readFrom);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
587 } else {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
588 unsigned int size = len; // compute size needed for result
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
589 while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
590 readFrom = foundAt + find.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
591 size += diff;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
592 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
593 if (size == len) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
594 if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
595 int index = len - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
596 while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
597 readFrom = buffer + index + find.len;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
598 memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
599 len += diff;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
600 buffer[len] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
601 memcpy(buffer + index, replace.buffer, replace.len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
602 index--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
603 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
604 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
605 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
606
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
607 void String::toLowerCase(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
608 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
609 if (!buffer) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
610 for (char *p = buffer; *p; p++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
611 *p = tolower(*p);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
612 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
613 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
614
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
615 void String::toUpperCase(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
616 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
617 if (!buffer) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
618 for (char *p = buffer; *p; p++) {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
619 *p = toupper(*p);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
620 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
621 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
622
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
623 void String::trim(void)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
624 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
625 if (!buffer || len == 0) return;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
626 char *begin = buffer;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
627 while (isspace(*begin)) begin++;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
628 char *end = buffer + len - 1;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
629 while (isspace(*end) && end >= begin) end--;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
630 len = end + 1 - begin;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
631 if (begin > buffer) memcpy(buffer, begin, len);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
632 buffer[len] = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
633 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
634
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
635 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
636 /* Parsing / Conversion */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
637 /*********************************************/
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
638
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
639 long String::toInt(void) const
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
640 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
641 if (buffer) return atol(buffer);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
642 return 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
643 }
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
644
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
645

mercurial