File: rtstring.cpp

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (506 lines) | stat: -rwxr-xr-x 11,867 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
 * rtstring.cpp
 * 
 * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
 * and Martin Trautmann (martintrautmann@gmx.de) 
 * 
 * This file may be distributed and/or modified under the terms of the 
 * GNU General Public License version 2 as published by the Free Software 
 * Foundation. 
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 */

// String.cpp: Implementierung der Klasse String.
//
//////////////////////////////////////////////////////////////////////

#include "rtstring.h"
#include "rtchar.h"
#include "rtmath.h"

#ifdef __SYMBIAN32__
#include <e32std.h>
#else
#include <stdio.h>
#endif


namespace lrt {

//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////

String::String() : Vector<char>(0)
{


}

String::String(const char* cStr) : Vector<char>(cStrLength(cStr))
{
	System::copyMemory(cStr, data, len);
}

String::String(const char chr, int length) : Vector<char>(length)
{
	System::fillMemory(data, len, chr);
}
/*
String::String(const short* ucStr) : Vector<char>(cStrLength(cStr))
{
	for(int i = 0; i < len; i++)
		data[i] = (char)(cStr[i]);
}*/

String::String(int i, int radix, int minLength) : Vector<char>(0)
{
	if((radix < 2) || (radix > Char::MAX_RADIX))
		System::exit(-5,String("Unsupported radix: ") + radix);
	bool neg = false;
	if(i < 0) {
		neg = true;
		//i = -i;
	}
	if(i == 0) {
		(*this)+='0';
	}
	else { // something to do
		int last = -1;
		do { // compute characters
			(*this)+= Char::charValue(Math::abs(i % radix));
			i /= radix;
			last++;
		}while(i != 0);
		// inverse order in string
		for(int j = last / 2; j >= 0; j--)
		{
			char temp = data[j];
			data[j] = data[last - j];
			data[last - j] = temp;
		}
		if(neg) insert('-', 0);
	}
	if(len < minLength)
		for(int s = len; s < minLength; s++)
			(*this)+= ' ';
}

String::String(double d, int length) : Vector<char>(0)
{
#ifdef __SYMBIAN32__
	TBuf<KDefaultRealWidth + 3> buf;
	TRealFormat myFormat;
	myFormat.iPoint = '.';
	buf.Num(d, myFormat);
	(*this) += (char*)(buf.PtrZ());
#else
	char* buf = new char[15];
	sprintf(buf, "%g", d);
	(*this) += buf;
#endif

	// we're too long, try to cut it (if after the dot)
	if(len > length) {
		int pidx = indexOf('.');
		len = Math::max(length, pidx);
	}
	// we're too short, append spaces
	else if(len < length) {
		check(length);
		for(int i = len; i < length; i++)
			data[i] = ' ';
	}
	
#ifndef __SYMBIAN32__ // cleanup buffer for ANSI C implementation
	delete[] buf;
#endif
}

String::String(const String& str) : Vector<char>(str.len)
{
   copy(&str, 0, this, 0, str.len);
}
/*
String String::operator += (const char*  cStr)
{
  Vector<char>::operator+=( String(cStr) );
  return *this;
}
*/
String& String::operator += (const int i)
{
  Vector<char>::operator+=( String(i) );
  return *this;
}

String& String::operator += (const char ch)
{
  Vector<char>::operator+=( ch );
  return *this;
}

String& String::operator += (const String& str)
{
  Vector<char>::operator+= (str );
  return *this;
}

String& String::operator= (const String& str)
{
	Vector<char>::operator =(str);
	return *this;
}

/*
const char& String::operator[](int index) const
{
	return Array<char>::operator[](index);
}
char& String::operator[](int index)
{
	return Vector<char>::operator[](index);
}
*/

//! Returns a new string like this one, but with any whitespace 
//! from the beginning and end of the string removed.
String String::trim() const
{
	if(len == 0) { return String(); } // that could be critical
	int start = 0, end = len - 1;
	while((start < len) && Char::isSpace(data[start]))
		start++;
	while((end > start) && Char::isSpace(data[end]))
		end--;
	return String(*this, start, end + 1);
}

String String::substring(int start, int end) const
{
	// special cases, may lead to problems
	if(len == 0) { return String(); }
	if(end <= start) { return String(); }
	if(start == len) { return String(); }

	// so that the optional parameter works.
	if(end > len) end = len;

	checkConst(start);
	return String(*this, start, end);
}

String String::lowerCase() const
{
	String ret(*this);
	for(int i = 0; i < len; i++)
		ret.data[i] = Char::lowerCase(ret.data[i]);
	return ret;
}

String String::upperCase() const
{
	String ret(*this);
	for(int i = 0; i < len; i++)
		ret.data[i] = Char::upperCase(ret.data[i]);
	return ret;
}
/* // obsolete because Vector<> does now contain this method too
int String::indexOf(char ch, int fromIndex) const
{
	int curIndex = Math::max(fromIndex, 0);
	while(curIndex < len) {
		if(data[curIndex] == ch) return curIndex;
		curIndex++;
	}
	return -1;
}
*/

int String::indexOf(const String& str, int fromIndex) const
{
	if(str.len > len) return -1; // cannot be contained in this string
	int curIndex = Math::max(fromIndex, 0);
	while(curIndex <= len - str.len) {
		if(!compare(curIndex, str, 0, str.len)) return curIndex;
		curIndex++;
	}
	return -1;
}

int String::lastIndexOf(char ch, int fromIndex) const
{
	int curIndex = Math::min(fromIndex, len - 1);
	while(curIndex >= 0) {
		if(data[curIndex] == ch) return curIndex;
		curIndex--;
	}
	return -1;
}

int String::lastIndexOf(const String& str, int fromIndex) const
{
	if(str.len > len) return -1; // cannot be contained in this string
	int curIndex = Math::min(fromIndex, len - str.len);
	while(curIndex >= 0) {
		if(!compare(curIndex, str, 0, str.len)) return curIndex;
		curIndex--;
	}
	return -1;
}

String String::replace(char oldCh, char newCh) const
{
	String ret(*this);
	for(int i = 0; i < len; i++)
		if(ret.data[i] == oldCh)
			ret.data[i] = newCh;
	return ret;
}

String String::replace(const String& oldStr, const String& newStr) const
{
    String ret;
    int pos = 0, end;
    while(pos < len)
    {
      end = indexOf(oldStr, pos);
      if(end < 0) { 
        ret += substring(pos);
        break;
      }
      ret += substring(pos, end);
      ret += newStr;
      pos = end + oldStr.len;
    }
    return ret;
}

Array<String> String::split(const String& separators, const String& combiners, 
							int maxParts, SplitFlags flags) const
{
	if(maxParts <= 0) maxParts = Math::MAX_INT;

    Vector<String> parts(0);
        
    char currComb;
    int end; // hinters Ende
    int begin, nextBegin;
    bool isSep = true; // derzeitiger Teil ist Space
    
    String str = trim();
    
    while(str.length() > 0)
    {
      if(separators.indexOf(str.data[0]) >= 0)
      {
		if(isSep && (flags & CREATE_EMPTY)) // last was a separator too
			parts += String();
        isSep = true;
        end = 1;
        nextBegin = end;
        begin = 0;
      }
      else
      {
        isSep = false;
		// if we are at the last allowed part, just take the rest of string and stuff it 
		// into the parts as the last one
		if(parts.length() >= maxParts - 1) {
			parts += str;
			break;
		}

        int cbin = combiners.indexOf(str.data[0]);
        if(cbin >= 0)
        {
          currComb = combiners.data[cbin];
          end = str.indexOf(currComb, 1);
          if(end < 0)
          {
            end = str.length();
            nextBegin = end;
          }
          else nextBegin = end + 1;
          begin = 1;
        }
        else
        {
          end = str.findNextOf(separators, 0);
          nextBegin = end;
          begin = 0;
        }
      }

	  // add the last part to the string
      String part = str.substring(begin, end);
      str = str.substring(nextBegin);
      if(!isSep) parts += part; // part was a string, do add it to the array

      str = str.trim();
    }
	if(isSep && (flags & CREATE_EMPTY))
		parts += String(); // the last part was a separator, add an empty string after it

    return parts;
}

String String::join(const Array<String>& arr, const String& joiner, const int upTo)
{
	if((arr.length() == 0) || (upTo < 0)) {return String();}
    String ret;
    int end = Math::min(arr.length() - 1, upTo);
    for(int i = 0; i < end; i++)
    {
      ret += arr[i];
      ret += joiner;
    }
    ret += arr[end];
    return ret;
}

int String::intValue(int def, int radix) const
{
	if((radix < 2) || (radix > Char::MAX_RADIX))
		System::exit(-5,String("Unsupported radix: ") + radix);
	bool neg = false, valid = false;
	int ret = 0;
	for(int i = 0; i < len; i++)
	{
		if(Char::isDigit(data[i], radix))
		{
			ret = ret * radix + Char::numericValue(data[i]);
			valid = true;
		}
		else if(!valid)
		{
			if(data[i] == '-')
				neg = !neg;
			else if(data[i] == '+')
				; // do nothing
			else break; // invalid string
		}
		else break; // invalid string
	}
	if(!valid) return def;
	if(neg) ret = -ret;
	return ret;
}

#ifdef __SYMBIAN32__
#include "rtetools.cpp"
double String::doubleValue(double def) const
{
	TLex lex(ESTRING((*this)));
	double ret;
	if(lex.Val(ret, '.') < 0)
		return def;
	else return ret;
}
#else
double String::doubleValue(double def) const
{
	float t;
	int res = sscanf(cStr(), "%g", &t);
	if((res != 0) && (res != EOF))
		return (double)t;
	else
		return def;
}
#endif

const char*  String::cStr() const
{
  String* const localThis = 
    const_cast<String* const>(this);

  localThis->check(len); // increase capacity by 1
  data[len] = char(0);
  return data;
}

////////// destructor
String::~String()
{
}

/////////////// private
//! Copies the part from this[start] up to and excluding this[end] (i.e. including this[end-1])
//! to a new string.
//! Does not check start, end for correctness! That's why it's private.
String::String(const String& str, int start, int end) : Vector<char>(end - start)
{
	copy(&str, start, this, 0, end - start);
}

//! Returns a negative, zero, or a positive number as this is less, equal, or greater than other.
//! Does not check start, end for correctness! That's why it's private.
int String::compare(int thisPos, const String& other, int otherPos, int length) const
{
	char chThis, chOther;
	for(int i = 0; i < length; i++)
	{
		chThis = data[thisPos + i];
		chOther = other.data[otherPos + i];
		if(chThis != chOther)
			return chThis - chOther;
	}
	return 0;
}

//! Returns a negative, zero, or a positive number as this is less, equal, or greater than other.
//! Does not check start, end for correctness! That's why it's private.
int String::compareIgnoreCase(int thisPos, const String& other, int otherPos, int length) const
{
	char chThis, chOther;
	for(int i = 0; i < length; i++)
	{
		chThis = Char::lowerCase(data[thisPos + i]);
		chOther = Char::lowerCase(other.data[otherPos + i]);
		if(chThis != chOther)
			return chThis - chOther;
	}
	return 0;
}

int String::cStrLength(const char* cStr)
{
	int len = 0;
	while(cStr[len] != 0)
		len++;
	return len;
}
/*
int String::cStrLength(const short* cStr)
{
	int len = 0;
	while(cStr[len] != 0)
		len++;
	return len;
}*/

/*! Returns the first position of one of the characters in what, or the
          position of the last character in the string if none of
          them was found. */
int String::findNextOf(const String& what, int start) const
{
	int pos = start;
	while((pos < len) && (what.indexOf(data[pos]) < 0))
	  pos++;

	return pos;
}


/////////////////// not-a-member
String operator + (const String& s1, const String& s2)
{
	String ret(s1);
	ret += s2;
	return ret;
}
} // namespace