File: StringMethods.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 (114 lines) | stat: -rw-r--r-- 2,626 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
/* String methods common to NXConstantString and TLString.
   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: StringMethods.m,v 1.2 1998/04/19 07:42:19 tiggr Exp $  */

-(int) compare: (id <TLString>) o
{
  const char *ocs;
  int r, l;

  if (!o)
    return (1);

  l = [o length];
  ocs = [o cString];
  
  if (l == len)
    r = memcmp (c_string, ocs, len);
  else if (l > len)
    {
      r = memcmp (c_string, ocs, len);
      if (!r)
	r = -1;
    }
  else
    {
      r = memcmp (c_string, ocs, l);
      if (!r)
	r = 1;
    }
  return (r);
} /* -compare */

-(id <TLString>) description
{
  return (self);
} /* -description */

-(unsigned int) hash
{
  return (tol_hash_string (c_string, len));
} /* -hash */

-(TLNumber *) objectLength
{
  return ([CO_TLNumber numberWithInt: len]);
}

-(void) print: (id <TLOutputStream>) stream quoted: (BOOL) qp
 withQuotes: (BOOL) wq
{
  if (!qp)
    [stream writeBytes: len fromBuffer: c_string];
  else
    {
      int i, force_next_digit_octal;

      if (wq)
	[stream writeByte: '"'];

      for (i = force_next_digit_octal = 0; i < len; i++)
	switch (c_string[i])
	  {
	  case '"':
	    if (wq)
	      [stream writeBytes: 2 fromBuffer: "\\\""];
	    force_next_digit_octal = 0; break;
	  case '\f': [stream writeBytes: 2 fromBuffer: "\\f"];
	    force_next_digit_octal = 0; break;
	  case '\n': [stream writeBytes: 2 fromBuffer: "\\n"];
	    force_next_digit_octal = 0; break;
	  case '\r': [stream writeBytes: 2 fromBuffer: "\\r"];
	    force_next_digit_octal = 0; break;
	  case '\t': [stream writeBytes: 2 fromBuffer: "\\t"];
	    force_next_digit_octal = 0; break;
	  case '\\': [stream writeBytes: 2 fromBuffer: "\\\\"];
	    force_next_digit_octal = 0; break;
	  default:
	    if (c_string[i] < ' ' || c_string[i] >= 127
		|| (force_next_digit_octal
		    && (c_string[i] >= '0' && c_string[i] <= '7')))
	      {
		int s, done, v = (unsigned char) c_string[i];

		[stream writeByte: '\\'];
		for (s = 64; s; s /= 8)
		  [stream writeByte: '0' + ((v / s) & 7)];
		force_next_digit_octal = 1;
	      }
	    else
	      {
		[stream writeByte: c_string[i]];
		force_next_digit_octal = 0;
	      }
	    break;
	  }
      if (wq)
	[stream writeByte: '"'];
    }
} /* -print:quoted:withQuotes: */

-(void) print: (id <TLOutputStream>) stream quoted: (BOOL) qp
{
  [self print: stream quoted: qp withQuotes: YES];
} /* -print:quoted: */

-stringp
{
  return (Qt);
} /* -stringp */