summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htlib/String.cc
blob: 918a5881d8c195d5bdcf6c899ac91115cef96603 (plain)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//
// String.cc
//
// String: (interface in htString.h) Just Another String class.
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later 
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: String.cc,v 1.40 2004/05/28 13:15:21 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */


#include "htString.h"
#include "Object.h"

#ifndef _MSC_VER /* _WIN32 */
#include <unistd.h>
#else
#include <io.h>
#endif

#ifdef HAVE_STD
#include <iostream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <iostream.h>
#endif /* HAVE_STD */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>


const int MinimumAllocationSize = 4;	// Should be power of two.

#ifdef NOINLINE
String::String()
{
    Length = Allocated = 0;
    Data = 0;
}
#endif

String::String(int init)
{
    Length = 0;
    Allocated = init >= MinimumAllocationSize ? init : MinimumAllocationSize;
    Data = new char[Allocated];
}

String::String(const char *s)
{
    Allocated = Length = 0;
    Data = 0;

    int	len;
    if (s)
      {
	len = strlen(s);
	copy(s, len, len);
      }
}

String::String(const char *s, int len)
{
    Allocated = Length = 0;
    Data = 0;
    if (s && len > 0)
	copy(s, len, len);
}

String::String(const String &s)
{
    Allocated = Length = 0;
    Data = 0;

    if (s.length() > 0)
      copy(s.Data, s.length(), s.length());
}

//
// This can be used for performance reasons if it is known the
// String will need to grow.
//
String::String(const String &s, int allocation_hint)
{
    Allocated = Length = 0;
    Data = 0;

    if (s.length() != 0)
      {
	if (allocation_hint < s.length())
	  allocation_hint = s.length();
	copy(s.Data, s.length(), allocation_hint);
      }
}

String::~String()
{
    if (Allocated)
	delete [] Data;
}

void String::operator = (const String &s)
{
  if (s.length() > 0) 
    {
      allocate_space(s.length());
      Length = s.length();
      copy_data_from(s.Data, Length);
    }
  else
    {
      Length = 0;
    }
}

void String::operator = (const char *s)
{
    if (s)
    {
      int len = strlen(s);
	allocate_fix_space(len);
	Length = len;
	copy_data_from(s, Length);	
    }
    else
	Length = 0;
}

void String::append(const String &s)
{
    if (s.length() == 0)
	return;
    int	new_len = Length + s.length();

    reallocate_space(new_len);
    copy_data_from(s.Data, s.length(), Length);
    Length = new_len;
}

void String::append(const char *s)
{
    if (!s)
	return;
	
    append(s,strlen(s));
}

void String::append(const char *s, int slen)
{
    if (!s || !slen)
	return;

//    if ( slen == 1 ) 
//    {
//        append(*s);
//        return;
//    }
    int	new_len = Length + slen;

    if (new_len + 1 > Allocated)
    reallocate_space(new_len);
    copy_data_from(s, slen, Length);
    Length = new_len;
}

void String::append(char ch)
{
    int new_len = Length +1;
    if (new_len + 1 > Allocated)
    	reallocate_space(new_len);
    Data[Length] = ch;
    Length = new_len;
}

int String::compare(const String& obj) const
{
    int	len;
    int	result;
    const char	*p1 = Data;
    const char	*p2 = obj.Data;

    len = Length;
    result = 0;

    if (Length > obj.Length)
    {
	result = 1;
	len = obj.Length;
    }
    else if (Length < obj.Length)
	result = -1;

    while (len)
    {
	if (*p1 > *p2)
	    return 1;
	if (*p1 < *p2)
	    return -1;
	p1++;
	p2++;
	len--;
    }
    //
    // Strings are equal up to the shortest length.
    // The result depends upon the length difference.
    //
    return result;
}

int String::nocase_compare(const String &s) const
{
    const char	*p1 = get();
    const char	*p2 = s.get();

    return mystrcasecmp(p1, p2);
}

int String::Write(int fd) const
{
    int	left = Length;
    char	*wptr = Data;
	
    while (left)
    {
	int result = write(fd, wptr, left);
		
	if (result < 0)
	    return result;
		
	left -= result;
	wptr += result;
    }
    return left;
}

const char *String::get() const
{
  static const char	*null = "";
  if (!Allocated)
    return null;
  Data[Length] = '\0';	// We always leave room for this.
  return Data;
}

char *String::get()
{
  static char	*null = "";
  if (!Allocated)
    return null;
  Data[Length] = '\0';	// We always leave room for this.
  return Data;
}

char *String::new_char() const
{
    char	*r;
    if (!Allocated)
    {
	r = new char[1];
	*r = '\0';
	return r;
    }
    Data[Length] = '\0';	// We always leave room for this.
    r = new char[Length + 1];
    strcpy(r, Data);
    return r;
}


int String::as_integer(int def) const
{
    if (Length <= 0)
	return def;
    Data[Length] = '\0';
    return atoi(Data);
}

double String::as_double(double def) const
{
    if (Length <= 0)
	return def;
    Data[Length] = '\0';
    return atof(Data);
}

String String::sub(int start, int len) const
{
    if (start > Length)
	return 0;

    if (len > Length - start)
	len = Length - start;

    return String(Data + start, len);
}

String String::sub(int start) const
{
    return sub(start, Length - start);
}

int String::indexOf(const char *str) const
{
    char	*c;    
    //
    // Set the first char after string end to zero to prevent finding
    // substrings including symbols after actual end of string
    //
    if (!Allocated)
	return -1;
    Data[Length] = '\0';
    
    /* OLD CODE: for (i = 0; i < Length; i++) */
#ifdef HAVE_STRSTR
    if ((c = strstr(Data, str)) != NULL)
	return(c -Data);
#else
    int		len = strlen(str);
    int		i;
    for (i = 0; i <= Length-len; i++)
    {
	if (strncmp(&Data[i], str, len) == 0)
	    return i;
    }
#endif
    return -1;
}

int String::indexOf(char ch) const
{
    int		i;
    for (i = 0; i < Length; i++)
    {
	if (Data[i] == ch)
	    return i;
    }
    return -1;
}

int String::indexOf(char ch, int pos) const
{
    if (pos >= Length)
      return -1;
    for (int i = pos; i < Length; i++)
    {
	if (Data[i] == ch)
	    return i;
    }
    return -1;
}

int String::lastIndexOf(char ch, int pos) const
{
    if (pos >= Length)
	return -1;
    while (pos >= 0)
    {
	if (Data[pos] == ch)
	    return pos;
	pos--;
    }
    return -1;
}

int String::lastIndexOf(char ch) const
{
    return lastIndexOf(ch, Length - 1);
}
#ifdef NOINLINE
String &String::operator << (const char *str)
{
    append(str);
    return *this;
}

String &String::operator << (char ch)
{
    append(&ch, 1);
    return *this;
}
#endif

String &String::operator << (int i)
{
    char	str[20];
    sprintf(str, "%d", i);
    append(str);
    return *this;
}

String &String::operator << (unsigned int i)
{
    char	str[20];
    sprintf(str, "%u", i);
    append(str);
    return *this;
}

String &String::operator << (long l)
{
    char	str[20];
    sprintf(str, "%ld", l);
    append(str);
    return *this;
}

String &String::operator << (const String &s)
{
    append(s.get(), s.length());
    return *this;
}

char	String::operator >> (char c)
{
    c = '\0';
	
    if (Allocated && Length)
    {
	c = Data[Length - 1];
	Data[Length - 1] = '\0';
	Length--;
    }

    return c;
}

int String::lowercase()
{
  int converted = 0;
  for (int i = 0; i < Length; i++)
    {
      if (isupper((unsigned char)Data[i])) {
	Data[i] = tolower((unsigned char)Data[i]);
	converted++;
      }
    }
  return converted;
}


int String::uppercase()
{
  int converted = 0;
  for (int i = 0; i < Length; i++)
    {
      if (islower((unsigned char)Data[i])) {
	Data[i] = toupper((unsigned char)Data[i]);
	converted++;
      }
    }
  return converted;
}


void String::replace(char c1, char c2)
{
    for (int i = 0; i < Length; i++)
	if (Data[i] == c1)
	    Data[i] = c2;
}


int String::remove(const char *chars)
{
    if (Length <= 0)
	return 0;

    char	*good, *bad;
    int		skipped = 0;

    good = bad = Data;
    for (int i = 0; i < Length; i++)
    {
	if (strchr(chars, *bad))
	    skipped++;
	else
	    *good++ = *bad;
	bad++;
    }
    Length -= skipped;

    return skipped;
}

String &String::chop(int n)
{
    Length -= n;
    if (Length < 0)
	Length = 0;
    return *this;
}


String &String::chop(char ch)
{
	while (Length > 0 && Data[Length - 1] == ch)
	    Length--;
    return *this;
}


String &String::chop(const char *str)
{
	while (Length > 0 && strchr(str, Data[Length - 1]))
	    Length--;
    return *this;
}


void String::Serialize(String &dest)
{
    dest.append((char *) &Length, sizeof(Length));
    dest.append(get(), Length);
}


void String::Deserialize(String &source, int &index)
{
    memcpy((char *) &Length, (char *) source.get() + index, sizeof(Length));
    index += sizeof(Length);
    allocate_fix_space(Length);
    copy_data_from(source.get() + index, Length);
    index += Length;
}


//------------------------------------------------------------------------
// Non member operators.
//
String operator + (const String &a, const String &b)
{
    String	result(a, a.length() + b.length());
	
    result.append(b);
    return result;
}

int operator == (const String &a, const String &b)
{
    if (a.Length != b.Length)
	return 0;

    return a.compare(b) == 0;
}

int operator != (const String &a, const String &b)
{
    return a.compare(b) != 0;
}

int operator < (const String &a, const String &b)
{
    return a.compare(b) == -1;
}

int operator > (const String &a, const String &b)
{
    return a.compare(b) == 1;
}

int operator <= (const String &a, const String &b)
{
    return a.compare(b) <= 0;
}

int operator >= (const String &a, const String &b)
{
    return a.compare(b) >= 0;
}

#ifndef NOSTREAM
ostream &operator << (ostream &o, const String &s)
{
    o.write(s.Data, s.length());
    return o;
}
#endif /* NOSTREAM */

//------------------------------------------------------------------------
// Private Methods.
//

void String::copy_data_from(const char *s, int len, int dest_offset)
{
    memcpy(Data + dest_offset, s, len);
}

void String::allocate_space(int len)
{
    len++;				// In case we want to add a null.

    if (len <= Allocated)
      return;

    if (Allocated)
      delete [] Data;

    Allocated = MinimumAllocationSize;
    while (Allocated < len)
      Allocated <<= 1;

    Data = new char[Allocated];
}

void String::allocate_fix_space(int len)
{
    len++;				// In case we want to add a null.

    if (len <= Allocated)
      return;

    if (Allocated)
      delete [] Data;

    Allocated = len;
    if (Allocated < MinimumAllocationSize)
      Allocated = MinimumAllocationSize;
    Data = new char[Allocated];
}

void String::reallocate_space(int len)
{
	char	*old_data = 0;
	int	 old_data_len = 0;

    if (Allocated)
	{
	    old_data = Data;
	    old_data_len = Length;
	    Allocated = 0;
	}
    allocate_space(len);
    if (old_data)
      {
	copy_data_from(old_data, old_data_len);
	delete [] old_data;
      }
}

void String::copy(const char *s, int len, int allocation_hint)
{
  if (len == 0 || allocation_hint == 0)
    return;         // We're not actually copying anything!
  allocate_fix_space(allocation_hint);
  Length = len;
  copy_data_from(s, len);
}

#ifndef NOSTREAM
void String::debug(ostream &o)
{
    o << "Length: " << Length << " Allocated: " << Allocated <<
	" Data: " << ((void*) Data) << " '" << *this << "'\n";
}
#endif /* NOSTREAM */

int String::readLine(FILE *in)
{
    Length = 0;
    allocate_fix_space(2048);

    while (fgets(Data + Length, Allocated - Length, in))
    {
	Length += strlen(Data + Length);
	if (Length == 0)
	    continue;
	if (Data[Length - 1] == '\n')
	{
	    //
	    // A full line has been read.  Return it.
	    //
	    chop('\n');
	    return 1;
	}
	if (Allocated > Length + 1)
	{
	    //
	    // Not all available space filled. Probably EOF?
	    //
	    continue;
	}
	//
	// Only a partial line was read. Increase available space in 
	// string and read some more.
	//
	reallocate_space(Allocated << 1);
    }
    chop('\n');

    return Length > 0;
}

#ifndef NOSTREAM
istream &operator >> (istream &in, String &line)
{
    line.Length = 0;
    line.allocate_fix_space(2048);

    for (;;)
    {
	in.clear();
	in.getline(line.Data + line.Length, line.Allocated - line.Length);
	line.Length += strlen(line.Data + line.Length);
	// if read whole line, or eof, or read fewer chars than the max...
	if (!in.fail() || in.eof() || line.Length + 1 < line.Allocated)
	    break;
	//
	// Only a partial line was read. Increase available space in 
	// string and read some more.
	//
	line.reallocate_space(line.Allocated << 1);
    }

    return in;
}
#endif /* NOSTREAM */