File: TLString.m

package info (click to toggle)
tom 1.1.1-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 6,340 kB
  • ctags: 2,244
  • sloc: objc: 27,863; ansic: 9,804; sh: 7,411; yacc: 3,377; lex: 966; asm: 208; makefile: 62; cpp: 10
file content (682 lines) | stat: -rw-r--r-- 15,557 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
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
/* Implementation of TLString class.
   This file is part of TL, Tiggr's Library.
   Written by Tiggr <tiggr@es.ele.tue.nl>
   Copyright (C) 1995, 1996 Pieter J. Schoenmakers
   TL is distributed WITHOUT ANY WARRANTY.
   See the file LICENSE in the TL distribution for details.

   $Id: TLString.m,v 1.3 1998/01/10 11:07:36 tiggr Exp $  */

#import "tl/support.h"
#import "tl/TLString.h"
#import "tl/TLStream.h"
#import "tl/TLStringStream.h"
#import "tl/TLPatchedRoots.h"
#import "tl/TLSymbol.h"
#import <string.h>
#import <ctype.h>
#import <sys/param.h>

/* Hash the LEN bytes at S.  XXX Maybe LEN should be clipped.  */
unsigned int
tol_hash_string (const char *s, int len)
{
  unsigned int h, i;

  for (h = i = 0; i < len; i++)
    h = (h << 3) ^ s[i];
  return (h);
} /* tol_hash_string */

@implementation NXConstantString (TLNXConstantString)

#include "StringMethods.m"

#if NEXT_RUNTIME
/* This method is needed to complement NeXT's NXConstantString.  */
-(const char *) cString
{
  return (c_string);
} /* cString */
#endif

-(TLString *) string
{
  return [CO_TLString stringWithCString: c_string length: len];
} /* -string */

/******************** doing difficult avoidance ********************/

/* Make sure NXConstantString is never retained or released.  */

-autorelease
{
  return (self);
} /* -autorelease */

-free
{
  return (nil);
} /* -free */

-(void) gcMark
{
} /* -gcMark */

-(void) release
{
} /* -release */

-retain
{
  return (self);
} /* -retain */

-(unsigned int) retainCount
{
  return (42);
} /* retainCount */

@end

@implementation TLString

#include "StringMethods.m"

/******************** private methods ********************/

+initialize
{
  if (!tll_full_range)
    {
      tll_full_range = [TLRange rangeWithStart: 0 length: -1];
      [(id) tll_full_range gcLock];
    }
  return (self);
} /* +initialize */

-initWithCString: (const char *) an_s length: (int) l
{
  len = l;
  c_string = xmalloc (len + 1);
  memcpy (c_string, an_s, len);
  c_string[len] = 0;
  return (self);
} /* -initWithCString: */

-initWithCStringNoCopy: (char *) an_s length: (int) l
{
  len = l;
  c_string = an_s;
  return (self);
} /* -initWithCStringNoCopy: */

-initWithStream: (id <TLInputStream>) stream length: (int) length
{
  int i;

  c_string = xmalloc (length + 1);

  /* XXX Wouldn't persistent read be nice?  */
  for (len = 0; len < length; len += i)
    {
      i = [stream readBytes: length - len intoBuffer: c_string + len];
      if (i <= 0)
	{
	  [self warning: "error or EOF"];
	  INIT_RETURN_NIL ();
	}
    }

  c_string[len] = 0;
  return (self);
} /* -initWithStream:length: */

-initWithStream: (id <TLInputStream>) stream
{
  int i, cap = 512;

  for (;;)
    {
      cap *= 2;
      c_string = xrealloc (c_string, cap);
      i = [stream readBytes: cap - len intoBuffer: c_string + len];
      if (i < 0)
	INIT_RETURN_NIL ();
      else if (!i)
	break;
      len += i;
    }

  c_string = xrealloc (c_string, len + 1);
  c_string[len] = 0;
  return (self);
} /* -initWithStream:length: */

-initWithString: (id <TLString>) s length: (int) l byPadding: (int) pad
	   with: (char) c
{
  int i, sl = [s length];
  int ls, rs;

  if (l < sl)
    l = sl;
  len = l;
  c_string = xmalloc (1 + len);

  if (pad < 0)
    ls = 0, rs = l - sl;
  else if (pad > 0)
    rs = 0, ls = l - sl;
  else
    {
      rs = ls = (l - sl) / 2;
      if (rs + ls != l - sl)
	rs++;
    }

  if (ls)
    for (i = 0; i < ls; i++)
      c_string[i] = c;
  if (rs)
    for (i = len - rs; i < len; i++)
      c_string[i] = c;
  memcpy (c_string + ls, [s cString], sl);

  c_string[len] = 0;
  return (self);
} /* +initWithString:length:byPadding:with: */

/******************** public methods ********************/

+(TLString *) stringWithCString: (const char *) an_s
{
  return ([(TLString *) [self gcAlloc]
	   initWithCString: an_s length: strlen (an_s)]);
} /* +stringWithCString: */

+(TLString *) stringWithCStringNoCopy: (char *) an_s
{
  return ([(TLString *) [self gcAlloc]
	   initWithCStringNoCopy: an_s length: strlen (an_s)]);
} /* +stringWithCStringNoCopy: */

+(TLString *) stringWithCStringNoCopy: (char *) an_s length: (int) l
{
  return ([(TLString *) [self gcAlloc]
	   initWithCStringNoCopy: an_s length: l]);
} /* +stringWithCStringNoCopy:length: */

+(TLString *) stringWithCString: (const char *) an_s length: (int) l
{
  return ([(TLString *) [self gcAlloc] initWithCString: an_s length: l]);
} /* +stringWithCString:length: */

+(id <TLString>) stringWithHTMLQuotedCString: (const char *) s length: (int) l
{
  /* Slow...  */
  id r = [CO_TLMutableString mutableString];
  int i, j;

  for (i = 0; i < l; i = j)
    {
      for (j = i; j < l; j++)
	if (s[j] == '%')
	  break;
      if (j != i)
	[r writeBytes: j - i fromBuffer: s + i];
      if (j != l)
	{
	  int k, v;

	  for (k = j + 1, v = 0; k - j < 3 && k < l; k++)
	    if (!isxdigit (s[k]))
	      break;
	    else
	      {
		int digit = s[k] - '0';
		if (digit > 9)
		  digit -= 'A' - 1 - '9';
		if (digit > 15)
		  digit -= 'a' - 'A';
		v = 16 * v + digit;
	      }

	  [r writeByte: v];
	  j = k;
	}
    }
  return (r);
} /* stringWithHTMLQuotedCString:length: */

+(TLString *) stringWithFormat: (id <TLString>) fmt, ...
{
  va_list ap;
  id retval;

  va_start (ap, fmt);
  /* XXX Return a real string, not the mutable one returned by vformac.  */
  retval = [self stringWithString: vformac (nil, fmt, ap)];
  va_end (ap);
  return (retval);
} /* +stringWithFormat: */

+(TLString *) stringWithFormat: (id) fmt: (va_list) ap
{
  /* Return a real string, not the mutable one returned by vformac.  */
  return ([self stringWithString: vformac (nil, fmt, ap)]);
} /* +stringWithFormat:: */

+(TLString *) stringWithFormatVector: (TLVector *) vector
{
  return ([self stringWithString:
	   [CO_TLMutableString mutableStringWithFormatVector: vector]]);
} /* +stringWithFormatVector: */

+(TLString *) stringWithStream: (id <TLInputStream>) stream
{
  return ([[self gcAlloc] initWithStream: stream]);
} /* +stringWithStream: */

+(TLString *) stringWithStream: (id <TLInputStream>) stream
 length: (int) length
{
  return ([[self gcAlloc] initWithStream: stream length: length]);
} /* +stringWithStream:length: */

+(TLString *) stringWithString: (id <TLString>) s
{
  return ([(TLString *) [self gcAlloc]
	   initWithCString: [s cString] length: [s length]]);
} /* +stringWithString */

+(TLString *) stringWithString: (id <TLString>) s withLength: (int) l
 byPadding: (int) pad
{
  return ([[self gcAlloc] initWithString: s length: l
	   byPadding: pad with: ' ']);
} /* +stringWithString:withLength:byPadding: */

-(id <TLString>) basename
{
  TLRange *r = [self rangeOfString: @"/" options: TLSEARCH_BACKWARD];

  return (r ? [self stringWithRange: [TLRange rangeWithStart: 1 + [r _start]
				      length: -1]] : self);
} /* -basename */

-(int) characterAtIndex: (int) idx
{
  if (idx < -len || idx >= len)
    return (-1);
  return (c_string[idx < 0 ? len + idx : idx]);
} /* -characterAtIndex: */

-(id <TLVector>) componentsSeparatedByString: (id <TLString>) s
{
  id <TLMutableVector> v;
  id <TLRange> r;

  /* Splitting into a <TLVector> of characters is not very useful, as a
     TLString itself is such a beast.  */
  if (![s length])
    return (self);

  v = [CO_TLVector vector];
  for (r = tll_full_range;;)
    {
      int i = [r _start], j;

      r = [self rangeOfString: s range: r];
      if (r)
	{
	  j = [r _start];
	  [v addElement: [self stringWithStart: i length: j - i]];
	  r = [TLRange rangeWithStart: j + [r length] length: -1];
	}
      else
	{
	  [v addElement: [self stringWithStart: i length: -1]];
	  break;
	}
    }
  return (v);
} /* -componentsSeparatedByString: */

-(const char *) cString
{
  return (c_string);
} /* cString */

-elementAtIndex: io
{
  /* Do not call `_elementAtIndex:' for the sake of speed.  */
  int i = [io integerPIntValue];
  return (i < len && i >= 0 ? [CO_TLNumber numberWithChar: c_string[i]] : nil);
} /* -elementAtIndex: */

-_elementAtIndex: (int) i
{
  return (i < len && i >= 0 ? [CO_TLNumber numberWithChar: c_string[i]] : nil);
} /* -_elementAtIndex: */

-(id <TLString>) expandedFilename
{
  return ([self expandedFilenameRelativeTo: nil]);
} /* -expandedFilename */

-(id <TLString>) expandedFilenameRelativeTo: (id <TLString>) dir
{
  if (len < 1 || *c_string == '/')
    return (self);

  if (*c_string == '~')
    {
      if (len == 1 || c_string[1] == '/')
	return ([isa stringWithFormat: @"%s%s", getenv ("HOME"), c_string + 1]);
    }
  else
    {
      if (!dir)
	{
	  char buf[MAXPATHLEN + 1];

	  /* XXX Check.  */
	  getcwd (buf, sizeof (buf));
	  dir = [isa stringWithFormat: @"%s", buf];
	}
      return ([isa stringWithFormat: @"%@/%s", dir, c_string]);
    }

  return (self);
} /* -expandedFilenameRelativeTo: */

-(BOOL) _fileExistsP
{
  return (!access (c_string, F_OK));
} /* _fileExistsP */

-fileExistsP
{
  return ([self _fileExistsP] ? Qt : nil);
} /* -fileExistsP */

-(TLMutableString *) htmlQuoted
{
  id r = [CO_TLMutableString mutableString];
  int i, j;

  /* XXX This is, semantically, not good enough!  */
  for (i = 0; i < len; i = j)
    {
      for (j = i; j < len; j++)
	if (!isalnum (c_string[j]) && c_string[j] != '/' && c_string[j] != ':')
	  break;
      if (j != i)
	[r writeBytes: j - i fromBuffer: c_string + i];
      if (j != len)
	{
	  int v = (unsigned char) c_string[j];
	  /* XXX Should have zero left-padding.  */
	  formac (r, @"%%%s%X", v < 16 ? "0" : "", v);
	  j++;
	}
    }
  return (r);
} /* -htmlQuoted */

-(TLMutableString *) htmlUnquoted
{
  id r = [CO_TLMutableString mutableString];
  int i, j;

  /* XXX This is, semantically, not good enough!  */
  for (i = 0; i < len; i = j)
    {
      for (j = i; j < len; j++)
	if (c_string[j] == '%')
	  break;
      if (j != i)
	[r writeBytes: j - i fromBuffer: c_string + i];
      if (j != len)
	{
	  int k, v;

	  for (k = j + 1, v = 0; k - j < 3 && k < len; k++)
	    if (!isxdigit (c_string[k]))
	      break;
	    else
	      {
		int digit = c_string[k] - '0';
		if (digit > 9)
		  digit -= 'A' - 1 - '9';
		if (digit > 15)
		  digit -= 'a' - 'A';
		v = 16 * v + digit;
	      }

	  [r writeByte: v];
	  j = k;
	}
    }
  return (r);
} /* -htmlUnquoted */

-(int) indexOfCharacterMatchFrom: (id <TLString>) s
{
  const char *cs = [s cString];
  int i, l = [s length];
  
  for (i = 0; i < len; i++)
    if (memchr (cs, c_string[i], l))
      return (i);
  return (-1);
} /* -indexOfCharacterMatchFrom: */

-(int) length
{
  return (len);
} /* -length */

-(id <TLRange>) rangeOfString: (id <TLString>) s
{
  return ([self rangeOfString: s range: tll_full_range options: 0]);
} /* -rangeOfString: */

-(id <TLRange>) rangeOfString: (id <TLString>) s options: (unsigned int) options
{
  return ([self rangeOfString: s range: tll_full_range options: options]);
} /* -rangeOfString:options: */

-(id <TLRange>) rangeOfString: (id <TLString>) s range: (id <TLRange>) r
{
  return ([self rangeOfString: s range: r options: 0]);
} /* -rangeOfString:range: */

-(id <TLRange>) rangeOfString: (id <TLString>) s
			range: (id <TLRange>) r
		      options: (unsigned int) options
{
  int i, start, length, end, sl, fold_case;
  const char *ss;
  TLRange *ret = nil;

  start = [r _start];
  if (start > len)
    return (nil);

  length = [r length];
  if (length < 0)
    length = len - start;

  if (start < 0)
    length += start, start = 0;
  if (start + length > len)
    length = len - start;

  ss = [s cString];
  sl = [s length];

  fold_case = options & TLSEARCH_FOLD_CASE;

  /* If this must be a full match, the lengths must be equal.  */
  if ((options & TLSEARCH_FULL_MATCH)
      && sl != length)
    return (nil);

  if (!sl)
    {
      /* The empty string always matched at the start of the search range.  */
      ret = [TLRange rangeWithStart: ((options & TLSEARCH_BACKWARD)
				      ? start + length : start) length: 0];
    }
  else if (options & TLSEARCH_BACKWARD)
    {
      for (i = start + length - sl; i >= start; i--)
	if (fold_case ? !memcasecmp (c_string + i, ss, sl)
	    : (c_string[i] == *ss
	       && (!memcmp (c_string + i, ss, sl))))
	  {
	    ret = [TLRange rangeWithStart: i length: sl];
	    break;
	  }
    }
  else
    {
      for (i = start, end = start + length - sl; i <= end; i++)
	if (fold_case ? !memcasecmp (c_string + i, ss, sl)
	    : (c_string[i] == *ss
	       && (!memcmp (c_string + i, ss, sl))))
	  {
	    ret = [TLRange rangeWithStart: i length: sl];
	    break;
	  }
    }
    
  return (ret);
} /* -rangeOfString:range: */

-(id <TLRange>) strcmp: (id <TLString>) s
{
  return ([self rangeOfString: s options: TLSEARCH_FULL_MATCH]);
} /* -strcmp: */

-(id <TLRange>) strcasecmp: (id <TLString>) s
{
  return ([self rangeOfString: s
	   options: TLSEARCH_FULL_MATCH | TLSEARCH_FOLD_CASE]);
} /* -strcasecmp: */

-(id <TLRange>) strstr: (id <TLString>) s
{
  return ([self rangeOfString: s]);
} /* -strstr: */

-(id <TLRange>) strcasestr: (id <TLString>) s
{
  return ([self rangeOfString: s options: TLSEARCH_FOLD_CASE]);
} /* -strcasestr: */

-(id <TLRange>) strrstr: (id <TLString>) s
{
  return ([self rangeOfString: s options: TLSEARCH_BACKWARD]);
} /* -strrstr: */

-(id <TLRange>) strcaserstr: (id <TLString>) s
{
  return ([self rangeOfString: s
	   options: TLSEARCH_FOLD_CASE | TLSEARCH_BACKWARD]);
} /* -strcaserstr: */

-(id <TLInputStream>) stream
{
  return ([TLStringStream streamWithString: self]);
} /* -stream */

-(TLString *) string
{
  return self;
} /* -string */

-(TLString *) stringWithRange: (id <TLRange>) r
{
  return ([self stringWithStart: [r _start] length: [r length]]);
} /* -stringWithRange: */

-(TLString *) stringWithStart: (int) start length: (int) length
{
  if (start > len)
    return (nil);
  if (length < 0)
    length = len - start;
  if (start < 0)
    length += start, start = 0;
  if (start + length > len)
    length = len - start;
  return ([isa stringWithCString: c_string + start length: length]);
} /* -stringWithStart:length: */

-(id <TLString>) stringWithUppercase
{
  char *nb = xmalloc (1 + len);
  int i;

  memcpy (nb, c_string, len + 1);

  for (i = 0; i < len; i++)
    if (nb[i] >= 'a' && nb[i] <= 'z')
      nb[i] = nb[i] - 'a' + 'A';
  return ([(TLString *) [isa gcAlloc] initWithCStringNoCopy: nb length: len]);
} /* -stringWithUppercase */

-(id <TLString>) stringWithLowercase
{
  char *nb = xmalloc (1 + len);
  int i;

  memcpy (nb, c_string, len + 1);

  for (i = 0; i < len; i++)
    if (nb[i] >= 'A' && nb[i] <= 'Z')
      nb[i] = nb[i] - 'A' + 'a';
  return ([(TLString *) [isa gcAlloc] initWithCStringNoCopy: nb length: len]);
} /* -stringWithLowercase */

-(id <TLString>) stringWithCapitals
{
  return (UNIMPLEMENTED_POINTER_METHOD);
} /* -stringWithCapitals */

-(id <TLNumber>) writeRange: (id <TLRange>) r
 toStream: (id <TLOutputStream>) stream
{
  int start = [r _start];
  int length = [r length];
  int n;

  if (start > len)
    return (tll_small_int[0]);
  if (length < 0)
    length = len - start;
  if (start < 0)
    length += start, start = 0;
  if (start + length > len)
    length = len - start;
  n = [stream writeBytes: length fromBuffer: c_string + start];
  if (n < 0)
    {
      [self warning: "%#: failed to write (%d, %d) to %#: %s",
       self, start, length, stream, ERRMSG];
      n = 0;
    }
  return ((n || !length) ? [CO_TLNumber numberWithInt: n] : nil);
} /* -writeRange:toStream: */

/******************** garbage collection ********************/

-(void) dealloc
{
  xfree (c_string);
} /* -dealloc */

@end