File: string.cc

package info (click to toggle)
ivtools 1.2.11a2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,364 kB
  • sloc: cpp: 174,988; ansic: 12,717; xml: 5,359; perl: 2,164; makefile: 831; sh: 326
file content (473 lines) | stat: -rw-r--r-- 10,897 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
/*
 * Copyright (c) 1999 Vectaport Inc.
 * Copyright (c) 1987, 1988, 1989, 1990, 1991 Stanford University
 * Copyright (c) 1991 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and
 * its documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the names of the copyright holders not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The copyright holders make
 * no representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <cstdio>
#include <OS/string.h>
#include <ctype.h>
#include <string.h>
#include <iostream.h>
#include <streambuf>

/*
 * Just to be sure ...
 */

extern "C" {
#if defined(__APPLE__)&&__GNUC__<3
#ifndef tolower
    extern wchar_t tolower(wchar_t);
#endif
#ifndef toupper
    extern wchar_t toupper(wchar_t);
#endif
#else
#if !defined(__APPLE__)
#ifndef tolower
    extern int tolower(int);
#endif
#ifndef toupper
    extern int toupper(int);
#endif
#endif
#endif
    extern long int strtol(const char*, char**, int);
    extern double strtod(const char*, char**);
}

String::String() {
    data_ = nil;
    length_ = 0;
}

String::String(const char* s) {
    data_ = s;
    length_ = s ? strlen(s) : 0;
}

String::String(const char* s, int n) {
    data_ = s;
    length_ = n;
}

String::String(const String& s) {
    data_ = s.data_;
    length_ = s.length_;
}

String::~String() { }

unsigned long String::hash() const {
    const char* p;
    unsigned long v = 0;
    if (length_ == -1) {
	for (p = data_; *p != '\0'; p++) {
	    v = (v << 1) ^ (*p);
	}
	String* s = (String*)this;
	s->length_ = p - data_;
    } else {
	const char* q = &data_[length_];
	for (p = data_; p < q; p++) {
	    v = (v << 1) ^ (*p);
	}
    }
    unsigned long t = v >> 10;
    t ^= (t >> 10);
    return v ^ t;
}

String& String::operator =(const String& s) {
    data_ = s.data_;
    length_ = s.length_;
    return *this;
}

String& String::operator =(const char* s) {
    data_ = s;
    length_ = s ? strlen(s) : 0;
    return *this;
}

boolean String::operator ==(const String& s) const {
    return (length_ == s.length_) && (strncmp(data_, s.data_, length_) == 0);
}

boolean String::operator ==(const char* s) const {
    return (strncmp(data_, s, length_) == 0) && (s[length_] == '\0');
}

boolean String::operator !=(const String& s) const {
    return (length_ != s.length_) || (strncmp(data_, s.data_, length_) != 0);
}

boolean String::operator !=(const char* s) const {
    return (strncmp(data_, s, length_) != 0) || (s[length_] != '\0');
}

boolean String::operator >(const String& s) const {
    return strncmp(data_, s.data_, length_) > 0;
}

boolean String::operator >(const char* s) const {
    return strncmp(data_, s, length_) > 0;
}

boolean String::operator >=(const String& s) const {
    return strncmp(data_, s.data_, length_) >= 0;
}

boolean String::operator >=(const char* s) const {
    return strncmp(data_, s, length_) >= 0;
}

boolean String::operator <(const String& s) const {
    return strncmp(data_, s.data_, length_) < 0;
}

boolean String::operator <(const char* s) const {
    return strncmp(data_, s, length_) < 0;
}

boolean String::operator <=(const String& s) const {
    return strncmp(data_, s.data_, length_) <= 0;
}

boolean String::operator <=(const char* s) const {
    return strncmp(data_, s, length_) <= 0;
}

boolean String::case_insensitive_equal(const String& s) const {
    if (length() != s.length()) {
	return false;
    }
    const char* p = string();
    const char* p2 = s.string();
    const char* q = p + length();
    for (; p < q; p++, p2++) {
	int c1 = *p;
	int c2 = *p2;
	if (c1 != c2 && tolower(c1) != tolower(c2)) {
	    return false;
	}
    }
    return true;
}

boolean String::case_insensitive_equal(const char* s) const {
    return case_insensitive_equal(String(s));
}

/*
 * A negative value for start initializes the position at the end
 * of the string before indexing.  Any negative length makes
 * the substring extend to the end of the string.
 */

String String::substr(int start, int length) const {
    if (start >= length_ || start < -length_) {
	/* should raise exception */
	return *this;
    }
    int pos = (start >= 0) ? start : (length_ + start);
    if (pos + length > length_) {
	/* should raise exception */
	return *this;
    }
    int len = (length >= 0) ? length : (length_ - pos);
    return String(data_ + pos, len);
}

void String::set_to_substr(int start, int length) {
    if (start > length_ || start < -length_) {
	/* should raise exception */
	return;
    }
    int pos = (start >= 0) ? start : (length_ + start);
    if (pos + length > length_) {
	/* should raise exception */
	return;
    }
    int len = (length >= 0) ? length : (length_ - pos);
    data_ += pos;
    length_ = len;
}

boolean String::null_terminated() const { return false; }

void String::set_value(const char* s) {
    data_ = s;
    length_ = s ? strlen(s) : 0;
}

void String::set_value(const char* s, int len) {
    data_ = s;
    length_ = len;
}

/*
 * A negative value for start initializes the position to the end
 * of the string before indexing and searches right-to-left.
 */

int String::search(int start, u_char c) const {
    if (start >= length_ || start < -length_) {
	/* should raise exception */
	return -1;
    }
    if (start >= 0) {
	const char* end = data_ + length_;
	for (const char* p = data_ + start; p < end; p++) {
	    if (*p == c) {
		return p - data_;
	    }
	}
    } else {
	for (const char* p = data_ + length_ + start; p >= data_; p--) {
	    if (*p == c) {
		return p - data_;
	    }
	}
    }
    return -1;
}

/*
 * Convert a string to binary value.
 */

boolean String::convert(int& value) const {
    NullTerminatedString s(*this);
    const char* str = s.string();
    char* ptr;
    value = (int)strtol(str, &ptr, 0);
    return ptr != str;
}

boolean String::convert(long& value) const {
    NullTerminatedString s(*this);
    const char* str = s.string();
    char* ptr;
    value = strtol(str, &ptr, 0);
    return ptr != str;
}

boolean String::convert(float& value) const {
    NullTerminatedString s(*this);
    const char* str = s.string();
    char* ptr;
    value = (float)strtod(str, &ptr);
    return ptr != str;
}

boolean String::convert(double& value) const {
    NullTerminatedString s(*this);
    const char* str = s.string();
    char* ptr;
    value = strtod(str, &ptr);
    return ptr != str;
}

/*
 * return true if sub-string exists
 */

boolean String::contains(const char* str, int start) const {
  return strstr(string()+start, str)!= nil;
}

/*
 * return substring before match
 */

String String::before(const char* str) const {
  const char* ptr = strstr(string(), str);
  if (ptr) 
    return substr(0, ptr-string());
  else
    return substr(0,0);
}

/*
 * return substring from match
 */

String String::from(const char* str) const {
  const char* ptr = strstr(string(), str);
  if (ptr) {
    int offset = (int)(ptr-string());
    return substr(offset,length()-offset);
  }  else
    return substr(0,0);
}

/*
 * return number of occurences 
 */
int String::freq(const char* t) const {
  int count=0;
  char* oldptr = (char *)string();
  char* newptr;
  while((newptr = strstr(oldptr, t))) {
    oldptr = newptr;
    count++;
  }
  return count;
}

String::operator const char*() const {
  return string();
};

ostream& operator<< (ostream& out, const String& str) {
#if defined(sun) && !defined(solaris) || __GNUC__>=3
  out.write(str.string(), str.length());
#else
  out.write(str.string(), (streamsize)str.length());
#endif
  return out;	
}

/* class CopyString */

CopyString::CopyString() : String() { }

CopyString::CopyString(const char* s) : String() {
    set_value(s);
}

CopyString::CopyString(const char* s, int length) : String() {
    set_value(s, length);
}

CopyString::CopyString(const String& s) : String() {
    set_value(s.string(), s.length());
}

CopyString::CopyString(const CopyString& s) : String() {
    set_value(s.string(), s.length());
}

CopyString::~CopyString() {
    free();
}

String& CopyString::operator =(const String& s) {
    free();
    set_value(s.string(), s.length());
    return *this;
}

String& CopyString::operator =(const char* s) {
    free();
    set_value(s);
    return *this;
}

boolean CopyString::null_terminated() const { return true; }

void CopyString::set_value(const char* s) {
    set_value(s, s ? strlen(s) : 0);
}

/*
 * Guarantee null-terminated string for compatibility with printf et al.
 */

void CopyString::set_value(const char* s, int len) {
    char* ns = new char[len + 1];
    ns[len] = '\0';
    String::set_value(strncpy(ns, s, len), len);
}

void CopyString::free() {
    char* s = (char*)(string());
    delete[] s;
}

/*
 * class NullTerminatedString
 */

NullTerminatedString::NullTerminatedString() : String() {
    allocated_ = false;
}

NullTerminatedString::NullTerminatedString(const String& s) : String() {
    assign(s);
}

NullTerminatedString::NullTerminatedString(
    const NullTerminatedString& s
) : String() {
    allocated_ = false;
    String::set_value(s.string(), s.length());
}

NullTerminatedString::~NullTerminatedString() {
    free();
}

String& NullTerminatedString::operator =(const String& s) {
    free();
    assign(s);
    return *this;
}

String& NullTerminatedString::operator =(const char* s) {
    free();
    allocated_ = false;
    String::set_value(s, s ? strlen(s) : 0);
    return *this;
}

boolean NullTerminatedString::null_terminated() const { return true; }

void NullTerminatedString::assign(const String& s) {
    if (s.null_terminated()) {
	allocated_ = false;
	String::set_value(s.string(), s.length());
    } else {
	allocated_ = true;
	int len = s.length();
	char* ns = new char[len + 1];
	ns[len] = '\0';
	String::set_value(strncpy(ns, s.string(), len), len);
    }
}

void NullTerminatedString::free() {
    if (allocated_) {
	char* s = (char*)(string());
	delete[] s;
	allocated_ = false;
    }
}


char* strnew (const char* s) {
  if (!s) return nil;
  char* dup = new char[strlen(s) + 1];
  strcpy(dup, s);
  return dup;
}