author | Claus Gittinger <cg@exept.de> |
Tue, 30 Apr 1996 17:39:28 +0200 | |
changeset 1321 | 52e043fb7eaf |
parent 1295 | 83f594f05c52 |
child 1491 | a42ae3fbb756 |
permissions | -rw-r--r-- |
1 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1988 by Claus Gittinger |
154 | 3 |
All Rights Reserved |
1 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
13 |
Magnitude subclass:#Character |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
14 |
instanceVariableNames:'asciivalue' |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
15 |
classVariableNames:'CharacterTable' |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
16 |
poolDictionaries:'' |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
17 |
category:'Magnitude-General' |
1 | 18 |
! |
19 |
||
54 | 20 |
!Character class methodsFor:'documentation'! |
21 |
||
88 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1988 by Claus Gittinger |
|
154 | 25 |
All Rights Reserved |
88 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
54 | 36 |
documentation |
37 |
" |
|
1229 | 38 |
Single byte Characters are unique; |
39 |
i.e. for every asciiValue (0..255) there exists exactly one instance of Character, |
|
40 |
which is shared. |
|
68 | 41 |
Other characters (i.e. asciivalue > 255) are not shared. |
1 | 42 |
|
1229 | 43 |
This means: you may compare characters using #== ONLY IFF you are certain, |
357 | 44 |
that the characters ranges is 0..255. Otherwise, you HAVE TO compare |
45 |
using #=. (if in doubt, always compare using #=). |
|
46 |
Sorry for this inconvenience. |
|
47 |
||
68 | 48 |
Methods marked as (JS) come from the manchester Character goody |
49 |
(CharacterComparing) by Jan Steinman, which allow Characters to be used as |
|
1229 | 50 |
Interval elements (i.e. ($a to:$z) do:[...] ); |
51 |
They are not a big deal, but convenient add-ons. |
|
52 |
Some of these have been modified a bit. |
|
1 | 53 |
|
68 | 54 |
WARNING: characters are known by compiler and runtime system - |
1229 | 55 |
do not change the instance layout. |
357 | 56 |
|
57 |
Also, although you can create subclasses of Character, the compiler always |
|
58 |
creates instances of Character for literals ... |
|
814
d4d28ca7afcd
made the global CharacterTable a classVar of Character
Claus Gittinger <cg@exept.de>
parents:
760
diff
changeset
|
59 |
... and other classes are hard-wired to always return instances of characters |
357 | 60 |
in some cases (i.e. String>>at:, Symbol>>at: etc.). |
61 |
Therefore, it may not make sense to create a character-subclass. |
|
1295 | 62 |
|
63 |
[author:] |
|
64 |
Claus Gittinger |
|
65 |
||
66 |
[see also:] |
|
67 |
String TwoByteString |
|
68 |
StringCollection |
|
54 | 69 |
" |
70 |
! ! |
|
1 | 71 |
|
72 |
!Character class methodsFor:'instance creation'! |
|
73 |
||
74 |
basicNew |
|
75 |
"catch new - Characters cannot be created with new" |
|
76 |
||
77 |
^ self error:'Characters cannot be created with new' |
|
78 |
! |
|
79 |
||
699 | 80 |
digitValue:anInteger |
81 |
"return a character that corresponds to anInteger. |
|
82 |
0-9 map to $0-$9, 10-35 map to $A-$Z" |
|
83 |
||
84 |
|val "{ Class: SmallInteger }" | |
|
85 |
||
86 |
val := anInteger. |
|
87 |
(val between:0 and:9) ifTrue:[ |
|
88 |
^ Character value:(val + ($0 asciiValue)) |
|
89 |
]. |
|
90 |
(val between:10 and:35) ifTrue:[ |
|
91 |
^ Character value:(val + ($A asciiValue - 10)) |
|
92 |
]. |
|
93 |
^self error:'value not in range 0 to 35' |
|
94 |
! |
|
95 |
||
1 | 96 |
value:anInteger |
97 |
"return a character with asciivalue anInteger" |
|
98 |
||
99 |
%{ /* NOCONTEXT */ |
|
100 |
||
54 | 101 |
INT ascii; |
1 | 102 |
|
249 | 103 |
if (__isSmallInteger(anInteger)) { |
104 |
ascii = __intVal(anInteger); |
|
154 | 105 |
if ((ascii & ~0xFF) == 0 /* (ascii >= 0) && (ascii <= 255) */) |
1133 | 106 |
RETURN ( __MKCHARACTER(ascii) ); |
1 | 107 |
} |
819 | 108 |
%}. |
1 | 109 |
(anInteger between:0 and:16rFF) ifTrue:[ |
154 | 110 |
^ CharacterTable at:(anInteger + 1) |
1 | 111 |
]. |
112 |
(anInteger between:16r100 and:16rFFFF) ifTrue:[ |
|
154 | 113 |
^ super basicNew setAsciiValue:anInteger |
1 | 114 |
]. |
357 | 115 |
" |
116 |
a characters ascii-code must be 0..16rFFFF. |
|
117 |
(i.e. only single-byte and twoByte characters are allowed.) |
|
118 |
" |
|
1 | 119 |
self error:'invalid ascii code for character' |
699 | 120 |
! ! |
121 |
||
122 |
!Character class methodsFor:'constants'! |
|
123 |
||
124 |
backspace |
|
125 |
"return the backspace character" |
|
126 |
||
127 |
^ Character value:8 |
|
128 |
! |
|
129 |
||
130 |
bell |
|
131 |
"return the bell character" |
|
132 |
||
133 |
^ Character value:7 |
|
134 |
! |
|
135 |
||
136 |
cr |
|
137 |
"return the lineEnd character |
|
138 |
- actually (in unix) this is a newline character" |
|
139 |
||
140 |
^ Character value:10 |
|
141 |
! |
|
142 |
||
143 |
del |
|
144 |
"return the delete character" |
|
145 |
||
146 |
^ Character value:16r7F |
|
147 |
! |
|
148 |
||
149 |
doubleQuote |
|
150 |
"return the double-quote character" |
|
151 |
||
152 |
^ Character value:34 |
|
153 |
! |
|
154 |
||
155 |
esc |
|
156 |
"return the escape character" |
|
157 |
||
158 |
^ Character value:27 |
|
159 |
! |
|
160 |
||
161 |
excla |
|
162 |
"return the exclamation-mark character" |
|
163 |
^ $!! |
|
1 | 164 |
! |
165 |
||
699 | 166 |
ff |
167 |
"return the form-feed character" |
|
168 |
||
169 |
^ Character value:12 |
|
170 |
! |
|
171 |
||
172 |
lf |
|
173 |
"return the newline/linefeed character" |
|
174 |
||
175 |
^ Character value:10 |
|
176 |
! |
|
1 | 177 |
|
699 | 178 |
maxValue |
179 |
"return the maximum asciiValue a character can have" |
|
180 |
||
181 |
^ 16rFFFF |
|
182 |
! |
|
183 |
||
184 |
newPage |
|
185 |
"return the form-feed character" |
|
186 |
||
187 |
^ Character value:12 |
|
188 |
! |
|
189 |
||
190 |
nl |
|
191 |
"return the newline character" |
|
68 | 192 |
|
699 | 193 |
^ Character value:10 |
194 |
! |
|
195 |
||
196 |
quote |
|
197 |
"return the single-quote character" |
|
198 |
||
199 |
^ Character value:39 |
|
200 |
! |
|
201 |
||
202 |
return |
|
203 |
"return the return character. |
|
204 |
In ST/X, this is different from cr - for Unix reasons." |
|
205 |
||
206 |
^ Character value:13 |
|
207 |
! |
|
208 |
||
209 |
space |
|
210 |
"return the blank character" |
|
211 |
||
212 |
^ Character value:32 |
|
213 |
! |
|
214 |
||
215 |
tab |
|
216 |
"return the tabulator character" |
|
217 |
||
218 |
^ Character value:9 |
|
1 | 219 |
! ! |
220 |
||
221 |
!Character class methodsFor:'primitive input'! |
|
222 |
||
223 |
fromUser |
|
357 | 224 |
"return a character from the keyboard (C's standard input stream) |
1 | 225 |
- this should only be used for emergency evaluators and the like." |
226 |
||
227 |
%{ /* NOCONTEXT */ |
|
228 |
int c; |
|
229 |
||
230 |
c = getchar(); |
|
231 |
if (c < 0) { |
|
154 | 232 |
RETURN (nil); |
1 | 233 |
} |
1133 | 234 |
RETURN ( __MKCHARACTER(c & 0xFF) ); |
1 | 235 |
%} |
236 |
! ! |
|
237 |
||
3 | 238 |
!Character class methodsFor:'queries'! |
239 |
||
240 |
isBuiltInClass |
|
1271 | 241 |
"return true if this class is known by the run-time-system. |
242 |
Here, true is returned for myself, false for subclasses." |
|
3 | 243 |
|
244 |
^ self == Character |
|
1271 | 245 |
|
246 |
"Modified: 23.4.1996 / 15:56:39 / cg" |
|
3 | 247 |
! ! |
248 |
||
1 | 249 |
!Character methodsFor:'accessing'! |
250 |
||
251 |
asciiValue |
|
54 | 252 |
"return the asciivalue of myself. |
253 |
PP has removed this methhod with 4.1 and providing |
|
254 |
asInteger instead." |
|
1 | 255 |
|
256 |
^asciivalue |
|
257 |
! |
|
258 |
||
259 |
instVarAt:index put:anObject |
|
54 | 260 |
"catch instvar access - asciivalue may not be changed" |
1 | 261 |
|
262 |
self error:'Characters may not be modified' |
|
263 |
! ! |
|
264 |
||
699 | 265 |
!Character methodsFor:'arithmetic'! |
1 | 266 |
|
267 |
+ aMagnitude |
|
268 |
"Return the Character that is <aMagnitude> higher than the receiver. |
|
269 |
Wrap if the resulting value is not a legal Character value. (JS)" |
|
270 |
||
2 | 271 |
^ Character value:(asciivalue + aMagnitude asInteger \\ 256) |
1 | 272 |
! |
273 |
||
274 |
- aMagnitude |
|
275 |
"Return the Character that is <aMagnitude> lower than the receiver. |
|
68 | 276 |
Wrap if the resulting value is not a legal Character value. (JS) |
277 |
claus: modified to return the difference as integer, if the argument |
|
154 | 278 |
is another character" |
1 | 279 |
|
68 | 280 |
aMagnitude isCharacter ifTrue:[ |
154 | 281 |
^ self asciiValue - aMagnitude asciiValue |
68 | 282 |
]. |
2 | 283 |
^ Character value:(asciivalue - aMagnitude asInteger \\ 256) |
68 | 284 |
|
285 |
" |
|
286 |
$z - $a |
|
287 |
$d - 3 |
|
288 |
" |
|
1 | 289 |
! |
290 |
||
291 |
// aMagnitude |
|
292 |
"Return the Character who's value is the receiver divided by <aMagnitude>. |
|
293 |
Wrap if the resulting value is not a legal Character value. (JS)" |
|
294 |
||
2 | 295 |
^ Character value:(asciivalue // aMagnitude asInteger \\ 256) |
1 | 296 |
! |
297 |
||
298 |
\\ aMagnitude |
|
299 |
"Return the Character who's value is the receiver modulo <aMagnitude>. |
|
300 |
Wrap if the resulting value is not a legal Character value. (JS)" |
|
301 |
||
2 | 302 |
^ Character value:(asciivalue \\ aMagnitude asInteger \\ 256) |
1 | 303 |
! ! |
304 |
||
699 | 305 |
!Character methodsFor:'binary storage'! |
306 |
||
307 |
hasSpecialBinaryRepresentation |
|
308 |
"return true, if the receiver has a special binary representation" |
|
309 |
||
310 |
^ true |
|
311 |
! |
|
312 |
||
313 |
storeBinaryOn:stream manager:manager |
|
314 |
"store a binary representation of the receiver on stream; |
|
315 |
redefined, since single-byte characters are stored more compact |
|
316 |
with a special type-code followed by the asciiValue." |
|
317 |
||
318 |
(asciivalue < 256) ifTrue:[ |
|
319 |
stream nextPut:manager codeForCharacter. |
|
320 |
stream nextPut:asciivalue |
|
321 |
] ifFalse:[ |
|
322 |
stream nextPut:manager codeForTwoByteCharacter. |
|
323 |
stream nextPutShort:asciivalue MSB:true |
|
324 |
] |
|
325 |
! ! |
|
326 |
||
327 |
!Character methodsFor:'comparing'! |
|
328 |
||
329 |
< aCharacter |
|
330 |
"return true, if the arguments asciiValue is greater than mine" |
|
331 |
||
332 |
^ (asciivalue < aCharacter asciiValue) |
|
333 |
! |
|
334 |
||
335 |
<= aCharacter |
|
336 |
"return true, if the arguments asciiValue is greater or equal to mine" |
|
337 |
||
338 |
^ (asciivalue <= aCharacter asciiValue) |
|
339 |
! |
|
340 |
||
341 |
= aCharacter |
|
342 |
"return true, if the argument, aCharacter is the same character |
|
343 |
Redefined to take care of 16bit characters." |
|
344 |
||
345 |
self == aCharacter ifTrue:[^ true]. |
|
346 |
aCharacter isCharacter ifFalse:[^ false]. |
|
347 |
^ (asciivalue = aCharacter asciiValue) |
|
348 |
! |
|
349 |
||
350 |
> aCharacter |
|
351 |
"return true, if the arguments asciiValue is less than mine" |
|
352 |
||
353 |
^ (asciivalue > aCharacter asciiValue) |
|
354 |
! |
|
355 |
||
356 |
>= aCharacter |
|
357 |
"return true, if the arguments asciiValue is less or equal to mine" |
|
358 |
||
359 |
^ (asciivalue >= aCharacter asciiValue) |
|
360 |
! |
|
361 |
||
362 |
identityHash |
|
363 |
"return an integer useful for hashing on identity" |
|
364 |
||
365 |
asciivalue <= 255 ifTrue:[ |
|
366 |
^ 4096 + asciivalue |
|
367 |
]. |
|
368 |
^ super identityHash |
|
369 |
! |
|
370 |
||
371 |
sameAs:aCharacter |
|
372 |
"return true, if the argument, aCharacter is the same character, |
|
373 |
ignoring case differences." |
|
374 |
||
375 |
self == aCharacter ifTrue:[^ true]. |
|
376 |
^ self asLowercase = aCharacter asLowercase |
|
377 |
! |
|
378 |
||
379 |
~= aCharacter |
|
380 |
"return true, if the argument, aCharacter is not the same character |
|
381 |
Redefined to take care of 16bit characters." |
|
382 |
||
383 |
self == aCharacter ifTrue:[^ false]. |
|
384 |
aCharacter isCharacter ifFalse:[^ true]. |
|
385 |
^ (asciivalue ~~ aCharacter asciiValue) |
|
386 |
! ! |
|
387 |
||
388 |
!Character methodsFor:'converting'! |
|
389 |
||
390 |
asCharacter |
|
391 |
"usually sent to integers, but redefined here to allow integers |
|
392 |
and characters to be used commonly without a need for a test." |
|
393 |
||
394 |
^ self |
|
395 |
||
396 |
" |
|
397 |
32 asCharacter |
|
398 |
" |
|
399 |
! |
|
400 |
||
401 |
asInteger |
|
402 |
"return an Integer with my ascii-value. |
|
403 |
OWST4.2 compatibility (sigh)" |
|
404 |
||
405 |
^ asciivalue |
|
406 |
! |
|
407 |
||
408 |
asLowercase |
|
409 |
"return a character with same letter as the receiver, |
|
410 |
but lowercase (the receiver if its lowercase or nonLetter)" |
|
411 |
||
412 |
self isUppercase ifFalse:[^ self]. |
|
413 |
^ Character value:(asciivalue + 32) |
|
414 |
! |
|
415 |
||
416 |
asString |
|
417 |
"return a string of len 1 with myself as contents" |
|
418 |
||
419 |
%{ /* NOCONTEXT */ |
|
420 |
char buffer[2]; |
|
421 |
OBJ s; |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
422 |
unsigned val; |
699 | 423 |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
424 |
val = __intVal(_characterVal(self)); |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
425 |
if (val <= 0xFF) { |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
426 |
buffer[0] = (char) val; |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
427 |
buffer[1] = '\0'; |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
428 |
s = __MKSTRING_L(buffer, 1 COMMA_SND); |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
429 |
if (s != nil) { |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
430 |
RETURN (s); |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
431 |
} |
699 | 432 |
} |
433 |
%}. |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
434 |
asciivalue > 255 ifTrue:[ |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
435 |
^ (TwoByteString new:1) at:1 put:self; yourself |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
436 |
]. |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
437 |
|
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
438 |
"/ |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
439 |
"/ ^ (String new:1) at:1 put:self; yourself. |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
440 |
"/ |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
441 |
|
699 | 442 |
" |
443 |
memory allocation (for the new string) failed. |
|
444 |
When we arrive here, there was no memory, even after a garbage collect. |
|
445 |
This means, that the VM wanted to get some more memory from the |
|
446 |
OS, which was not kind enough to give it. |
|
447 |
Bad luck - you should increase the swap space on your machine. |
|
448 |
" |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
449 |
|
699 | 450 |
^ ObjectMemory allocationFailureSignal raise. |
451 |
! |
|
452 |
||
453 |
asSymbol |
|
454 |
"return a unique symbol which prints like I print" |
|
455 |
||
456 |
^ Symbol internCharacter:self |
|
457 |
! |
|
458 |
||
459 |
asUppercase |
|
460 |
"return a character with same letter as the receiver, |
|
461 |
but uppercase (the receiver if its uppercase or nonLetter)" |
|
462 |
||
463 |
self isLowercase ifFalse:[^ self]. |
|
464 |
^ Character value:(asciivalue - 32) |
|
465 |
! |
|
466 |
||
467 |
digitValue |
|
468 |
"return my digitValue for any base" |
|
469 |
||
470 |
|ascii "{ Class: SmallInteger }" | |
|
471 |
||
472 |
ascii := asciivalue. |
|
473 |
(ascii between:($0 asciiValue) and:($9 asciiValue)) ifTrue:[ |
|
474 |
^ ascii - $0 asciiValue |
|
475 |
]. |
|
476 |
(ascii between:($a asciiValue) and:($z asciiValue)) ifTrue:[ |
|
477 |
^ ascii + (10 - $a asciiValue) |
|
478 |
]. |
|
479 |
(ascii between:($A asciiValue) and:($Z asciiValue)) ifTrue:[ |
|
480 |
^ ascii + (10 - $A asciiValue) |
|
481 |
]. |
|
482 |
||
483 |
"remove error below for X3J20 conformance ... " |
|
484 |
self error:'bad character'. |
|
485 |
" " |
|
486 |
^ -1 |
|
487 |
! |
|
488 |
||
489 |
to:aMagnitude |
|
490 |
"Return an Interval over the characters from the receiver to <aMagnitude>. |
|
491 |
Wrap <aMagnitude> if it is not a legal Character value. (JS)" |
|
492 |
||
493 |
^ Interval from:self to:(aMagnitude \\ 256) |
|
494 |
! ! |
|
495 |
||
496 |
!Character methodsFor:'copying'! |
|
497 |
||
498 |
copy |
|
499 |
"return a copy of myself |
|
500 |
reimplemented since characters are unique" |
|
501 |
||
502 |
^ self |
|
503 |
! |
|
504 |
||
505 |
deepCopy |
|
506 |
"return a deep copy of myself |
|
507 |
reimplemented since characters are unique" |
|
508 |
||
509 |
^ self |
|
510 |
! |
|
511 |
||
512 |
deepCopyUsing:aDictionary |
|
513 |
"return a deep copy of myself |
|
514 |
reimplemented since characters are unique" |
|
515 |
||
516 |
^ self |
|
517 |
! |
|
518 |
||
519 |
shallowCopy |
|
520 |
"return a shallow copy of myself |
|
521 |
reimplemented since characters are unique" |
|
522 |
||
523 |
^ self |
|
524 |
! |
|
525 |
||
526 |
simpleDeepCopy |
|
527 |
"return a deep copy of myself |
|
528 |
reimplemented since characters are unique" |
|
529 |
||
530 |
^ self |
|
531 |
! ! |
|
532 |
||
533 |
!Character methodsFor:'enumerating'! |
|
534 |
||
535 |
to:stopCharacter do:aBlock |
|
536 |
"evaluate aBlock for each character in self .. stopCharacter. |
|
537 |
This is somewhat stupid, since it depends on the ascii encoding |
|
538 |
(370-users watch out :-)" |
|
539 |
||
540 |
|runChar| |
|
541 |
||
542 |
runChar := self. |
|
543 |
[runChar <= stopCharacter] whileTrue:[ |
|
544 |
aBlock value:runChar. |
|
545 |
runChar := runChar + 1 |
|
546 |
] |
|
547 |
||
548 |
" |
|
549 |
($a to:$z) do:[:char | char printNL] |
|
550 |
$a to:$z do:[:char | char printNL]. |
|
551 |
" |
|
552 |
! ! |
|
553 |
||
554 |
!Character methodsFor:'national testing'! |
|
555 |
||
556 |
isNationalAlphaNumeric |
|
557 |
"return true, if the receiver is a letter in the |
|
558 |
current language (Language variable)" |
|
559 |
||
560 |
"stupid - should be configurable from a table ... |
|
561 |
... good thing is, that iso8859 puts all national |
|
562 |
characters above 16rC0" |
|
563 |
||
564 |
self isLetterOrDigit ifTrue:[^ true]. |
|
565 |
^ self asciiValue between:16rC0 and:16rFF |
|
566 |
! |
|
567 |
||
568 |
isNationalLetter |
|
569 |
"return true, if the receiver is a letter in the |
|
570 |
current language (Language variable)" |
|
571 |
||
572 |
"stupid - should be configurable from a table ... |
|
573 |
... good thing is, that iso8859 puts all national |
|
574 |
characters above 16rC0" |
|
575 |
||
576 |
self isLetter ifTrue:[^ true]. |
|
577 |
^ self asciiValue between:16rC0 and:16rFF |
|
578 |
! ! |
|
579 |
||
580 |
!Character methodsFor:'printing & storing'! |
|
581 |
||
582 |
displayString |
|
583 |
"return a string used when the receiver is to be displayed |
|
584 |
in an inspector kind-of-thing" |
|
585 |
||
586 |
^ self storeString |
|
587 |
! |
|
588 |
||
589 |
isLiteral |
|
590 |
"return true, if the receiver can be used as a literal |
|
591 |
(i.e. can be used in constant arrays)" |
|
592 |
||
593 |
^ true |
|
594 |
! |
|
595 |
||
596 |
||
597 |
"print myself on stdout" |
|
598 |
||
599 |
%{ /* NOCONTEXT */ |
|
600 |
||
1133 | 601 |
putchar(__intVal(__INST(asciivalue))); |
699 | 602 |
%} |
603 |
! |
|
604 |
||
605 |
printOn:aStream |
|
606 |
"print myself on aStream" |
|
607 |
||
608 |
aStream nextPut:self |
|
609 |
! |
|
610 |
||
611 |
printString |
|
612 |
"return a string to print me" |
|
613 |
||
614 |
^ self asString |
|
615 |
! |
|
616 |
||
617 |
storeOn:aStream |
|
618 |
"store myself on aStream" |
|
619 |
||
620 |
|special| |
|
621 |
||
622 |
(asciivalue between:33 and:127) ifFalse:[ |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
623 |
(self == Character space) ifTrue:[ |
1007 | 624 |
special := 'space' |
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
625 |
] ifFalse:[ |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
626 |
(self == Character cr) ifTrue:[ |
1007 | 627 |
special := 'cr'. |
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
628 |
] ifFalse:[ |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
629 |
(self == Character tab) ifTrue:[ |
1007 | 630 |
special := 'tab'. |
631 |
] ifFalse:[ |
|
632 |
(self == Character esc) ifTrue:[ |
|
633 |
special := 'esc'. |
|
634 |
] |
|
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
635 |
] |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
636 |
] |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
637 |
]. |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
638 |
special notNil ifTrue:[ |
1007 | 639 |
aStream nextPutAll:'(Character '; nextPutAll:special; nextPutAll:')'. |
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
640 |
^ self |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
641 |
]. |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
642 |
aStream nextPutAll:'(Character value:16r'; |
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
643 |
nextPutAll:(asciivalue printStringRadix:16); nextPutAll:')' |
699 | 644 |
] ifTrue:[ |
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
645 |
aStream nextPut:$$; nextPut:self |
699 | 646 |
] |
995
b018368b3a94
asString to 16-bit char should return a twoByteString
Claus Gittinger <cg@exept.de>
parents:
819
diff
changeset
|
647 |
|
1007 | 648 |
"Modified: 23.2.1996 / 23:27:32 / cg" |
699 | 649 |
! ! |
650 |
||
651 |
!Character methodsFor:'private accessing'! |
|
652 |
||
653 |
setAsciiValue:anInteger |
|
654 |
"very private - set the ascii value. |
|
655 |
- use this only for characters with codes > 16rFF. |
|
656 |
DANGER alert: funny things happen, if this is applied to |
|
657 |
one of the fixed-characters 0..255." |
|
658 |
||
659 |
asciivalue := anInteger |
|
660 |
! ! |
|
661 |
||
1 | 662 |
!Character methodsFor:'testing'! |
663 |
||
699 | 664 |
isAlphaNumeric |
665 |
"return true, if I am a letter or a digit |
|
666 |
- same as isAlphaNumeric for compatibility reasons." |
|
667 |
||
668 |
^ self isLetterOrDigit |
|
669 |
! |
|
670 |
||
54 | 671 |
isCharacter |
672 |
"return true, if the receiver is some kind of character" |
|
673 |
||
674 |
^ true |
|
675 |
! |
|
676 |
||
1 | 677 |
isDigit |
678 |
"return true, if I am a digit (i.e. $0 .. $9)" |
|
679 |
||
680 |
^ asciivalue between:($0 asciiValue) and:($9 asciiValue) |
|
681 |
! |
|
682 |
||
683 |
isDigitRadix:r |
|
684 |
"return true, if I am a digit of a base r number" |
|
685 |
||
686 |
(asciivalue < $0 asciiValue) ifTrue:[^ false]. |
|
687 |
(r > 10) ifTrue:[ |
|
154 | 688 |
(asciivalue between:($0 asciiValue) and:($9 asciiValue)) ifTrue:[ |
689 |
^ true |
|
690 |
]. |
|
691 |
((asciivalue - $a asciiValue) between:0 and:(r - 10)) ifTrue:[ |
|
692 |
^ true |
|
693 |
]. |
|
694 |
^ (asciivalue - $A asciiValue) between:0 and:(r - 10) |
|
1 | 695 |
]. |
696 |
(asciivalue - $0 asciiValue) < r ifTrue:[^ true]. |
|
697 |
^ false |
|
698 |
! |
|
699 |
||
699 | 700 |
isEndOfLineCharacter |
701 |
"return true if I am a line delimitting character" |
|
1 | 702 |
|
703 |
%{ /* NOCONTEXT */ |
|
704 |
||
705 |
REGISTER int val; |
|
706 |
||
1133 | 707 |
val = __intVal(__INST(asciivalue)); |
699 | 708 |
if ((val == '\n') |
709 |
|| (val == '\r') |
|
710 |
|| (val == '\f')) { |
|
711 |
RETURN ( true ); |
|
54 | 712 |
} |
1 | 713 |
%} |
699 | 714 |
. |
715 |
^ false |
|
1 | 716 |
! |
717 |
||
718 |
isLetter |
|
54 | 719 |
"return true, if I am a letter |
760
7eeb5f12e7fe
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
700
diff
changeset
|
720 |
- use isNationalLetter, if you are interested in those." |
1 | 721 |
|
722 |
%{ /*NOCONTEXT */ |
|
723 |
||
724 |
REGISTER int val; |
|
725 |
||
1133 | 726 |
val = __intVal(__INST(asciivalue)); |
1 | 727 |
RETURN ( (((val >= 'a') && (val <= 'z')) || |
154 | 728 |
((val >= 'A') && (val <= 'Z'))) ? true : false ); |
1 | 729 |
%} |
730 |
! |
|
731 |
||
154 | 732 |
isLetterOrDigit |
54 | 733 |
"return true, if I am a letter or a digit |
760
7eeb5f12e7fe
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
700
diff
changeset
|
734 |
- use isNationalAlphaNumeric, if you are interested in those." |
1 | 735 |
|
736 |
%{ /* NOCONTEXT */ |
|
737 |
||
738 |
REGISTER int val; |
|
739 |
||
1133 | 740 |
val = __intVal(__INST(asciivalue)); |
1 | 741 |
if ((val >= 'a') && (val <= 'z')) { |
154 | 742 |
RETURN ( true ); |
1 | 743 |
} |
744 |
if ((val >= 'A') && (val <= 'Z')) { |
|
154 | 745 |
RETURN ( true ); |
1 | 746 |
} |
747 |
if ((val >= '0') && (val <= '9')) { |
|
154 | 748 |
RETURN ( true ); |
1 | 749 |
} |
750 |
RETURN ( false ); |
|
751 |
%} |
|
752 |
! |
|
753 |
||
699 | 754 |
isLowercase |
755 |
"return true, if I am a lower-case letter" |
|
756 |
||
757 |
%{ /* NOCONTEXT */ |
|
758 |
||
759 |
REGISTER int val; |
|
154 | 760 |
|
1133 | 761 |
val = __intVal(__INST(asciivalue)); |
699 | 762 |
#ifndef OLD |
763 |
/* iso8859 puts national lower case characters at e0 .. ff */ |
|
764 |
if ((val >= 0xE0) && (val <= 0xFF)) { |
|
765 |
RETURN(true); |
|
766 |
} |
|
767 |
#endif |
|
768 |
RETURN ( ((val >= 'a') && (val <= 'z')) ? true : false ); |
|
769 |
%} |
|
154 | 770 |
! |
771 |
||
699 | 772 |
isPrintable |
773 |
"return true, if the receiver is a useful printable character |
|
774 |
(see fileBrowsers showFile:-method on how it can be used)" |
|
1 | 775 |
|
699 | 776 |
(asciivalue between:32 and:127) ifTrue:[^ true]. |
777 |
asciivalue == 13 ifTrue:[^ true]. |
|
778 |
asciivalue == 9 ifTrue:[^ true]. |
|
779 |
asciivalue == 10 ifTrue:[^ true]. |
|
780 |
^ self isNationalLetter |
|
1 | 781 |
! |
782 |
||
783 |
isSeparator |
|
784 |
"return true if I am a space, cr, tab, nl, or newPage" |
|
785 |
||
786 |
%{ /* NOCONTEXT */ |
|
787 |
||
788 |
REGISTER int val; |
|
789 |
||
1133 | 790 |
val = __intVal(__INST(asciivalue)); |
328 | 791 |
#ifndef NON_ASCII /* i.e. EBCDIC ;-) */ |
792 |
if (val <= ' ') |
|
793 |
#endif |
|
154 | 794 |
if ((val == ' ') |
795 |
|| (val == '\n') |
|
796 |
|| (val == '\t') |
|
797 |
|| (val == '\r') |
|
798 |
|| (val == '\f')) { |
|
799 |
RETURN ( true ); |
|
800 |
} |
|
1 | 801 |
%} |
802 |
. |
|
803 |
^ false |
|
804 |
! |
|
805 |
||
699 | 806 |
isUppercase |
807 |
"return true, if I am an upper-case letter" |
|
1 | 808 |
|
809 |
%{ /* NOCONTEXT */ |
|
810 |
||
811 |
REGISTER int val; |
|
812 |
||
1133 | 813 |
val = __intVal(__INST(asciivalue)); |
699 | 814 |
#ifndef OLD |
815 |
/* iso8859 puts national upper case characters at c0 .. df */ |
|
816 |
if ((val >= 0xC0) && (val <= 0xDF)) { |
|
817 |
RETURN(true); |
|
314 | 818 |
} |
699 | 819 |
#endif |
820 |
RETURN ( ((val >= 'A') && (val <= 'Z')) ? true : false ); |
|
1 | 821 |
%} |
822 |
! |
|
823 |
||
699 | 824 |
isVowel |
825 |
"return true, if I am a vowel (lower- or uppercase)" |
|
333 | 826 |
|
699 | 827 |
(self == $a) ifTrue:[^ true]. |
828 |
(self == $e) ifTrue:[^ true]. |
|
829 |
(self == $i) ifTrue:[^ true]. |
|
830 |
(self == $o) ifTrue:[^ true]. |
|
831 |
(self == $u) ifTrue:[^ true]. |
|
832 |
(self == $A) ifTrue:[^ true]. |
|
833 |
(self == $E) ifTrue:[^ true]. |
|
834 |
(self == $I) ifTrue:[^ true]. |
|
835 |
(self == $O) ifTrue:[^ true]. |
|
836 |
(self == $U) ifTrue:[^ true]. |
|
837 |
^ false |
|
1 | 838 |
! ! |
699 | 839 |
|
840 |
!Character class methodsFor:'documentation'! |
|
841 |
||
842 |
version |
|
1295 | 843 |
^ '$Header: /cvs/stx/stx/libbasic/Character.st,v 1.38 1996-04-25 16:57:23 cg Exp $' |
699 | 844 |
! ! |