2 "This isn't really Latin1, it's actually ASCII. This should become Latin1.
3 This is just an adaptation of string.slate in slate distribution. This
4 isn't really tested, either, consider this as more like an example."
6 prototypes define: #Latin1Character &parents: {EncodedCharacter}.
7 Latin1Character traits define: #Whitespace -> #{32. 9. 10. 11. 12. 13. 0}.
8 Latin1Character traits define: #Vowels -> #{65. 69. 73. 79. 85. 97. 101. 105. 111. 117}.
9 Latin1Character traits define: #Delimiters -> #{40. 41. 91. 93. 123. 125. 39. 44. 32. 34}.
11 prototypes define: #Latin1String &parents: {EncodedString}
12 &slots: {#contents -> #{}}.
13 Latin1String elementType: Latin1Character.
15 s@(Latin1String traits) new &capacity: n
16 [s copy `>> [contents: s contents new &capacity: (n ifNil: [0]). ]].
18 s@(Latin1String traits) size
23 s@(Latin1String traits) at: n
25 (s contents at: n) as: s elementType
28 s@(Latin1String traits) at: n put: c@(EncodedCharacter traits)
30 s contents at: n put: c code
33 s@(Latin1String traits) isEmpty
38 code@(Integer traits) as: l1@(Latin1Character traits)
40 newL1: (l1 newCode: code).
44 c@(Latin1Character traits) as: i@(Integer traits)
49 c1@(Latin1Character traits) = c2@(Latin1Character traits)
54 c@(Latin1Character traits) cr [10 as: Latin1Character].
56 c@(Latin1Character traits) tab [9 as: Latin1Character].
58 c@(Latin1Character traits) space [32 as: Latin1Character].
60 c@(Latin1Character traits) stringEscape [92 as: Latin1Character].
62 c@(Latin1Character traits) codeRange
67 a@(Latin1Character traits) allCharacters
69 a codeRange collect: [| :code | code as: a]
72 c@(Latin1Character traits) isAlphanumeric
74 c isLetter or: [c isDigit]
77 c@(Latin1Character traits) isWhitespace
79 c Whitespace includes: c code
82 c@(Latin1Character traits) isDelimiter
84 c Delimiters includes: c code
87 c@(Latin1Character traits) isDigit &radix: radix
89 radix `defaultsTo: 10.
91 (value >= 48 and: [value <= 57])
92 ifTrue: [^ (value - 48 < radix)].
93 (value >= 65 and: [value <= 90])
94 ifTrue: [^ (value - 65 < (radix - 10))].
95 (value >= 97 and: [value <= 122])
96 ifTrue: [^ (value - 97 < (radix - 10))].
100 c@(Latin1Character traits) toDigit &radix: radix
102 radix `defaultsTo: 10.
103 (c isDigit &radix: radix) ifFalse: [^ Nil].
105 value >= 97 ifTrue: [^ (value - 97 + 10)].
106 value >= 65 ifTrue: [^ (value - 65 + 10)].
107 value >= 48 ifTrue: [^ (value - 48)].
111 c@(Latin1Character traits) isLetter
112 [c isUppercase \/ [c isLowercase]].
114 c@(Latin1Character traits) isLowercase
115 [c code between: 97 and: 122].
117 c@(Latin1Character traits) isUppercase
118 [c code between: 65 and: 90].
120 c@(Latin1Character traits) toLowercase
123 ifTrue: [c code + 32 as: c]
127 c@(Latin1Character traits) toUppercase
130 ifTrue: [c code - 32 as: c]
134 c@(Latin1Character traits) isVowel
135 [c Vowels includes: c code].
137 c@(Latin1Character traits) isQuote
138 [#{34. 39} includes: c code].
140 c@(Latin1Character traits) isPrintable
141 [c code between: 32 and: 126].
143 _@(Latin1String traits) accepts: _@(Root traits)
146 _@(Latin1String traits) accepts: _@(Latin1Character traits)
149 s@(Latin1String traits) first
152 s@(Latin1String traits) capitalize
153 "Modifies the first Character to be uppercase."
156 ifFalse: [s at: 0 put: s first toUppercase].
160 s@(Latin1String traits) toUppercase
161 "Modifies the Characters to be uppercase."
162 [s infect: #toUppercase `er ].
164 s@(Latin1String traits) toLowercase
165 "Modifies the Characters to be lowercase."
166 [s infect: #toLowercase `er ].
168 s@(Latin1String traits) toSwapCase
169 "Modifies the Characters to have swapped case."
171 s doWithIndex: [| :each :index | each isLowercase
172 ifTrue: [s at: index put: each toUppercase]
173 ifFalse: [s at: index put: each toLowercase]]
176 s1@(Latin1String traits) lexicographicallyCompare: s2@(Latin1String traits)
177 "Answer a sign of comparing the two Strings' Characters in order."
181 c1 code < c2 code ifTrue: [^ -1].
182 c1 code > c2 code ifTrue: [^ 1]].
190 ch@(Latin1Character traits) rot13
192 upper: ch isUppercase.
193 value: (ch toLowercase as: Integer).
194 (value >= 97) /\ (value < 110)
195 ifTrue: [value: value + 13]
196 ifFalse: [(value > 109) /\ (value <= 122)
197 ifTrue: [value: value - 13]].
199 ifTrue: [(value as: Latin1Character) toUppercase]
200 ifFalse: [value as: Latin1Character]
203 s@(Latin1String traits) rot13
205 result: s newSameSize.
206 s doWithIndex: [| :each :index |
207 result at: index put: each rot13].
211 c@(Latin1Character traits) printOn: w@(WriteStream traits)
213 w nextPut: (c code as: ASCIIString Character)