" Smalltalk/Character.st -*- Smalltalk -*- " Character : Magnitude ( value ) Character basicValue: anInteger [ self := self _clone. value := anInteger. ] CharacterArray : Array () Character value: anInteger [ ^CharacterArray at: anInteger ] Character asciiValue: anInteger [ ^CharacterArray at: anInteger ] Character _value: _value [ | anInteger | { v_anInteger= _integerObject((int)v__value); }. ^self value: anInteger ] Character value [ ^value ] Character asciiValue [ ^value ] CharacterArray initialise [ CharacterArray := self new: 256. 0 to: 255 do: [ :value | CharacterArray at: value put: (Character basicValue: value) ] ] [CharacterArray initialise] Character digitValue: anInteger [ ^self value: (anInteger < 10 ifTrue: [ 48 ] "ascii '0'" ifFalse: [ 87 ]) "ascii 'a' - 10" + anInteger ] Character digitValue [ " Answer 0-9 if the receiver is $0-$9, 10-35 if it is $A-$Z, and < 0 otherwise. This is used to parse literal numbers of radix 2-36. " ^#( " 0 1 2 3 4 5 6 7 8 9 A B C D E F" "0" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "1" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "2" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "3" 0 1 2 3 4 5 6 7 8 9 -1 -1 -1 -1 -1 -1 "4" -1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 "5" 25 26 27 28 29 30 31 32 33 34 35 -1 -1 -1 -1 -1 "6" -1 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 "7" 25 26 27 28 29 30 31 32 33 34 35 -1 -1 -1 -1 -1 "8" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "9" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "A" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "B" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "C" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "D" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "E" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 "F" -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ) at: self asciiValue ] Character tab [ ^$\t ] Character nl [ ^$\n ] Character ff [ ^$\f ] Character cr [ ^$\r ] Character space [ ^$ ] Character isLetter [ ^(self between: $a and: $z) or: [self between: $A and: $Z] ] Character isDigit [ ^self between: $0 and: $9 ] Character hash [ ^value ] Character < aCharacter [ ^value < aCharacter asciiValue ] Character = aCharacter [ ^self == aCharacter ] Character asInteger [ ^value ] Character asString [ ^String with: self ] Character printOn: aStream [ aStream nextPut: $$. self < $\040 ifTrue: [ aStream nextPut: $\\; nextPut: (Character digitValue: self asciiValue // 64 \\ 8); nextPut: (Character digitValue: self asciiValue // 8 \\ 8); nextPut: (Character digitValue: self asciiValue // 1 \\ 8) ] ifFalse: [ aStream nextPut: self ] ] Character print { struct t_Character *this= (struct t_Character *)self; int value= _integerValue(this->value); switch (value) { case 0: printf("$\\0"); break; case 7: printf("$\\a"); break; case 8: printf("$\\b"); break; case 9: printf("$\\t"); break; case 10: printf("$\\n"); break; case 11: printf("$\\v"); break; case 12: printf("$\\f"); break; case 13: printf("$\\r"); break; case 27: printf("$\\e"); break; case 92: printf("$\\\\"); break; default: if (isprint(value)) printf("$%c", value); else printf("$\\%03o", value); break; } } " CharacterEscapes : Object () [(CharacterEscapes := IdentityDictionary new) at: $a put: 0x07; at: $b put: 0x08; at: $e put: 0x1b; at: $f put: 0x0c; at: $n put: 0x0a; at: $r put: 0x0d; at: $t put: 0x09; at: $v put: 0x0b ] Character readFrom: aStream [ | char value | aStream next = $$ ifFalse: [^nil]. (char := aStream next) = $\\ ifFalse: [^c]. value := CharacterEscapes at: (char := aStream next) ifAbsent: []. value notNil ifTrue: [^value]. char isNil ifTrue: [^nil]. (char isDigit and: [char < $8]) ifFalse: [^char]. value := char digitValue. 2 timesRepeat: [char := aStream next. char isNil ifTrue: [self error: 'premature end of octal Character']. char := char digitValue. (char < 0 or: [char > 7]) ifTrue: [self error: 'illegal digit in octal Character']. value := value * 8 + char]. ^Character value: value ] " " ---------------------------------------------------------------- " Integer printDigitOn: aStream [ self = 0 ifFalse: [self // 10 printDigitOn: aStream. aStream nextPut: (Character digitValue: self \\ 10)] ] Integer printOn: aStream [ self < 0 ifTrue: [aStream nextPut: $-. self negated printOn: aStream] ifFalse: [(self // 10) printDigitOn: aStream. aStream nextPut: (Character digitValue: self \\ 10)] ] Integer readFrom: aStream base: base [ | negative value | negative := false. value := 0. aStream peek = $+ ifTrue: [aStream skip: 1] ifFalse: [aStream peek = $- ifTrue: [negative := negative not. aStream skip: 1]]. [aStream atEnd] whileFalse: [| digit | digit := aStream next. digit = $r ifTrue: [value < 2 ifTrue: [self error: 'illegal radix in integer constant']. base := value. value := 0] ifFalse: [digit := digit digitValue. (digit < 0 or: [digit >= base]) ifTrue: [self error: 'illegal digit in integer constant']. value := value * base + digit]]. ^negative ifTrue: [value negated] ifFalse: [value] ]