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