Moved "Shell" code into a "Glob" type/namespace, and removed FormatSpecification...
[cslatevm.git] / src / i18n / encodings-latin1.slate
blob2aae661b274872649014f8bf627ce0dc24035391
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
20   s contents size
23 s@(Latin1String traits) at: n 
24
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
35   s contents isEmpty
38 code@(Integer traits) as: l1@(Latin1Character traits)
39 [| proto newL1 |
40  newL1: (l1 newCode: code).
41  newL1
44 c@(Latin1Character traits) as: i@(Integer traits)
46   c code
49 c1@(Latin1Character traits) = c2@(Latin1Character traits)
51   c1 code = c2 code
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
64   0 to: 255
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
88 [| value |
89   radix `defaultsTo: 10.
90   value: c code.
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))].
97   False
100 c@(Latin1Character traits) toDigit &radix: radix
101 [| value |
102   radix `defaultsTo: 10.
103   (c isDigit &radix: radix) ifFalse: [^ Nil].
104   value: c code.
105   value >= 97 ifTrue: [^ (value - 97 + 10)].
106   value >= 65 ifTrue: [^ (value - 65 + 10)].
107   value >= 48 ifTrue: [^ (value - 48)].
108   Nil
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
122   c isUppercase
123     ifTrue: [c code + 32 as: c]
124     ifFalse: [c]
127 c@(Latin1Character traits) toUppercase
129   c isLowercase
130     ifTrue: [c code - 32 as: c]
131     ifFalse: [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)
144 [False].
146 _@(Latin1String traits) accepts: _@(Latin1Character traits)
147 [True].
149 s@(Latin1String traits) first
150 [s at: 0].
152 s@(Latin1String traits) capitalize
153 "Modifies the first Character to be uppercase."
155   s isEmpty
156     ifFalse: [s at: 0 put: s first toUppercase].
157   s
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."
179   s1 with: s2 do:
180     [| :c1 :c2 |
181      c1 code < c2 code ifTrue: [^ -1].
182      c1 code > c2 code ifTrue: [^ 1]].
183   s1 size < s2 size
184     ifTrue: [^ -1].
185   s1 size > s2 size
186     ifTrue: [^ 1].
187   0
190 ch@(Latin1Character traits) rot13
191 [| value upper |
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]].
198   upper
199     ifTrue: [(value as: Latin1Character) toUppercase]
200     ifFalse: [value as: Latin1Character]
203 s@(Latin1String traits) rot13
204 [| result |
205   result: s newSameSize.
206   s doWithIndex: [| :each :index |
207     result at: index put: each rot13].
208   result
211 c@(Latin1Character traits) printOn: w@(WriteStream traits)
213   w nextPut: (c code as: ASCIIString Character)