1  package frost.core
  2  
  3  uses frost.unsafe.Pointer
  4  
  5  ====================================================================================================
  6  Represents a single 2-byte word of a UTF-16 encoded string.
  7  
  8  A single-codepoint string literal whose single codepoint fits into a `Char16` (that is, Unicode
  9  codepoints 0 to 65535) may be used wherever a `Char16` is expected. That is, it is legal to write:
 10  
 11      def c:Char16 := "A"
 12  ====================================================================================================
 13  class Char16 : Value, HashKey<Char16>, Comparable<Char16> {
 14      @private
 15      def value:UInt16
 16  
 17      ================================================================================================
 18      Creates a `Char16` with the specified codepoint.
 19      ================================================================================================
 20      init(value:UInt16) {
 21          self.value := value
 22      }
 23  
 24      ================================================================================================
 25      Returns the difference between the codepoints of two `Char16`s.
 26      ================================================================================================
 27      function -(other:Char16):UInt16 {
 28          return value - other.value
 29      }
 30  
 31      ================================================================================================
 32      Returns the difference between the codepoints of two `Char16`s.
 33      ================================================================================================
 34      @priority(1)
 35      function -(other:Char16):Int {
 36          return (value - other.value).asInt
 37      }
 38  
 39      @override
 40      function =(other:Char16):Bit {
 41          return value = other.value
 42      }
 43  
 44      @override
 45      function !=(other:Char16):Bit {
 46          return value != other.value
 47      }
 48  
 49      @override
 50      function <(other:Char16):Bit {
 51          return value < other.value
 52      }< other.value
 53      }
 54  
 55      @override
 56      function >(other:Char16):Bit {
 57          return value > other.value
 58      }
 59  
 60      @override
 61      function <=(other:Char16):Bit {
 62          return value <= other.value
 63      }
 64  
 65      @override
 66      function >=(other:Char16):Bit {
 67          return value >= other.value
 68      }
 69  
 70      ================================================================================================
 71      `true` if this character is whitespace.
 72      ================================================================================================
 73      property isWhitespace:Bit
 74      function get_isWhitespace():Bit {
 75          -- FIXME respect all Unicode whitespace
 76          match self {
 77              when "\n", "\r", "\t", " " {
 78                  return true
 79              }
 80              otherwise {
 81                  return false
 82              }
 83          }
 84      }
 85  
 86      ================================================================================================
 87      `true` if this character is a numeric digit.
 88      ================================================================================================
 89      property isDigit:Bit
 90      function get_isDigit():Bit {
 91          -- FIXME respect all Unicode digits
 92          return self >= "0" & self <= "9"
 93      }
 94  
 95      ================================================================================================
 96      Returns a string consisting of `count` copies of this character.
 97      ================================================================================================
 98      function *(count:Int):String {
 99          def result := MutableString()
100          for i in 0 .. count {
101              result.append(self)
102          }
103          return result.finish()
104      }
105  
106      ================================================================================================
107      Returns a string consisting of `count` copies of the given character.
108      ================================================================================================
109      @class
110      function *(count:Int, char:Char16):String {
111          return char * count
112      }
113  
114      @override
115      function get_hash():Int {
116          return asInt
117      }
118  
119      ================================================================================================
120      This character converted to a `Char8`. If this number is not in the range of a `Char8`, a safety
121      violation occurs.
122      ================================================================================================
123      property asChar8:Char8
124      function get_asChar8():Char8 {
125          return Char8(asUInt8)
126      }
127  
128      ================================================================================================
129      This character converted to a `Char32`.
130      ================================================================================================
131      property asChar32:Char32
132      function get_asChar32():Char32 {
133          return Char32(asInt32)
134      }
135  
136      ================================================================================================
137      This character's codepoint converted to an 8 bit signed number. If this number is not in the
138      range of an 8 bit signed number, a safety violation occurs.
139      ================================================================================================
140      property asInt8:Int8
141      function get_asInt8():Int8 {
142          return value.asInt8
143      }
144  
145      ================================================================================================
146      This character's codepoint converted to a 16 bit signed number. If this number is not in the
147      range of a 16 bit signed number, a safety violation occurs.
148      ================================================================================================
149      property asInt16:Int16
150      function get_asInt16():Int16 {
151          return value.asInt16
152      }
153  
154      ================================================================================================
155      This character's codepoint converted to a 32 bit signed number.
156      ================================================================================================
157      property asInt32:Int32
158      function get_asInt32():Int32 {
159          return value.asInt32
160      }
161  
162      ================================================================================================
163      This character's codepoint converted to a 64 bit signed number.
164      ================================================================================================
165      property asInt64:Int64
166      function get_asInt64():Int64 {
167          return value.asInt64
168      }
169  
170      ================================================================================================
171      This character's codepoint converted to an `Int`.
172      ================================================================================================
173      property asInt:Int
174      function get_asInt():Int {
175          return value.asInt
176      }
177  
178      ================================================================================================
179      This character's codepoint converted to an 8 bit unsigned number. If this number is not in the
180      range of an 8 bit unsigned number, a safety violation occurs.
181      ================================================================================================
182      property asUInt8:UInt8
183      function get_asUInt8():UInt8 {
184          return value.asUInt8
185      }
186  
187      ================================================================================================
188      This character's codepoint converted to a 16 bit unsigned number.
189      ================================================================================================
190      property asUInt16:UInt16
191      function get_asUInt16():UInt16 {
192          return value
193      }
194  
195      ================================================================================================
196      This character's codepoint converted to a 32 bit unsigned number.
197      ================================================================================================
198      property asUInt32:UInt32
199      function get_asUInt32():UInt32 {
200          return value.asUInt32
201      }
202  
203      ================================================================================================
204      This character's codepoint converted to a 64 bit unsigned number.
205      ================================================================================================
206      property asUInt64:UInt64
207      function get_asUInt64():UInt64 {
208          return value.asUInt64
209      }
210  
211      ================================================================================================
212      This character's codepoint converted to a `UInt`.
213      ================================================================================================
214      property asUInt:UInt
215      function get_asUInt():UInt {
216          return value.asUInt
217      }
218  
219      ================================================================================================
220      This character converted to a `Char8`. If the character's codepoint does not fit into a `Char8`,
221      it will silently overflow.
222      ================================================================================================
223      property toChar8:Char8
224      function get_toChar8():Char8 {
225          return Char8(toUInt8)
226      }
227  
228      ================================================================================================
229      This character converted to a `Char32`.
230      ================================================================================================
231      property toChar32:Char32
232      function get_toChar32():Char32 {
233          return Char32(toInt32)
234      }
235  
236      ================================================================================================
237      This character's codepoint converted to an 8 bit signed number. If the character's codepoint
238      does not fit into an 8 bit signed number, it will silently overflow.
239      ================================================================================================
240      property toInt8:Int8
241      function get_toInt8():Int8 {
242          return value.toInt8
243      }
244  
245      ================================================================================================
246      This character's codepoint converted to a 16 bit signed number. If the character's codepoint
247      does not fit into a 16 bit signed number, it will silently overflow.
248      ================================================================================================
249      property toInt16:Int16
250      function get_toInt16():Int16 {
251          return value.toInt16
252      }
253  
254      ================================================================================================
255      This character's codepoint converted to a 32 bit signed number.
256      ================================================================================================
257      property toInt32:Int32
258      function get_toInt32():Int32 {
259          return value.toInt32
260      }
261  
262      ================================================================================================
263      This character's codepoint converted to a 64 bit signed number.
264      ================================================================================================
265      property toInt64:Int64
266      function get_toInt64():Int64 {
267          return value.toInt64
268      }
269  
270      ================================================================================================
271      This character's codepoint converted to an `Int`.
272      ================================================================================================
273      property toInt:Int
274      function get_toInt():Int {
275          return value.toInt
276      }
277  
278      ================================================================================================
279      This character's codepoint converted to an 8 bit unsigned number. If the character's codepoint
280      does not fit into an 8 bit unsigned number, it will silently overflow.
281      ================================================================================================
282      property toUInt8:UInt8
283      function get_toUInt8():UInt8 {
284          return value.toUInt8
285      }
286  
287      ================================================================================================
288      This character's codepoint converted to a 16 bit unsigned number.
289      ================================================================================================
290      property toUInt16:UInt16
291      function get_toUInt16():UInt16 {
292          return value
293      }
294  
295      ================================================================================================
296      This character's codepoint converted to a 32 bit unsigned number.
297      ================================================================================================
298      property toUInt32:UInt32
299      function get_toUInt32():UInt32 {
300          return value.toUInt32
301      }
302  
303      ================================================================================================
304      This character's codepoint converted to a 64 bit unsigned number.
305      ================================================================================================
306      property toUInt64:UInt64
307      function get_toUInt64():UInt64 {
308          return value.toUInt64
309      }
310  
311      ================================================================================================
312      This character's codepoint converted to a `UInt`.
313      ================================================================================================
314      property toUInt:UInt
315      function get_toUInt():UInt {
316          return value.toUInt
317      }
318  
319      ================================================================================================
320      Returns a string containing this character. Note that separately encoding high and low surrogate
321      pairs may not produce the intended result; they should generally be decoded into a single
322      `Char32` before being reencoded into a `String`.
323      ================================================================================================
324      @override
325      function get_toString():String {
326          if value < 0x80< 0x80 {
327              def data := Pointer<Char8>.alloc(1)
328              data[0] := Char8(asUInt8)
329              return String(data, 1)
330          }
331          if value < 0x800< 0x800 {
332              def data := Pointer<Char8>.alloc(2)
333              data[0] := Char8((value >> 6 || 0b11000000).asUInt8)
334              data[1] := Char8((value && 0b111111 || 0b10000000).asUInt8)
335              return String(data, 2)
336          }
337          def data := Pointer<Char8>.alloc(3)
338          data[0] := Char8((value >> 12 || 0b11100000).asUInt8)
339          data[1] := Char8((value >> 6 && 0b111111 || 0b10000000).asUInt8)
340          data[2] := Char8((value && 0b111111 || 0b10000000).asUInt8)
341          return String(data, 3)
342      }
343  }