widestrs.nim 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Nim support for C/C++'s `wide strings`:idx:. This is part of the system
  10. # module! Do not import it directly!
  11. #when not declared(ThisIsSystem):
  12. # {.error: "You must not import this module explicitly".}
  13. type
  14. Utf16Char* = distinct int16
  15. when defined(nimv2):
  16. type
  17. WideCString* = ptr UncheckedArray[Utf16Char]
  18. WideCStringObj* = object
  19. bytes: int
  20. data: WideCString
  21. proc `=destroy`(a: var WideCStringObj) =
  22. if a.data != nil:
  23. deallocShared(a.data)
  24. a.data = nil
  25. proc `=`(a: var WideCStringObj; b: WideCStringObj) {.error.}
  26. proc `=sink`(a: var WideCStringObj; b: WideCStringObj) =
  27. a.bytes = b.bytes
  28. a.data = b.data
  29. proc createWide(a: var WideCStringObj; bytes: int) =
  30. a.bytes = bytes
  31. a.data = cast[typeof(a.data)](allocShared0(bytes))
  32. template `[]`*(a: WideCStringObj; idx: int): Utf16Char = a.data[idx]
  33. template `[]=`*(a: WideCStringObj; idx: int; val: Utf16Char) = a.data[idx] = val
  34. template nullWide(): untyped = WideCStringObj(bytes: 0, data: nil)
  35. converter toWideCString*(x: WideCStringObj): WideCString {.inline.} =
  36. result = x.data
  37. else:
  38. template nullWide(): untyped = nil
  39. type
  40. WideCString* = ref UncheckedArray[Utf16Char]
  41. WideCStringObj* = WideCString
  42. template createWide(a; L) =
  43. unsafeNew(a, L * 4 + 2)
  44. proc ord(arg: Utf16Char): int = int(cast[uint16](arg))
  45. proc len*(w: WideCString): int =
  46. ## returns the length of a widestring. This traverses the whole string to
  47. ## find the binary zero end marker!
  48. while int16(w[result]) != 0'i16: inc result
  49. const
  50. UNI_REPLACEMENT_CHAR = Utf16Char(0xFFFD'i16)
  51. UNI_MAX_BMP = 0x0000FFFF
  52. UNI_MAX_UTF16 = 0x0010FFFF
  53. UNI_MAX_UTF32 = 0x7FFFFFFF
  54. UNI_MAX_LEGAL_UTF32 = 0x0010FFFF
  55. halfShift = 10
  56. halfBase = 0x0010000
  57. halfMask = 0x3FF
  58. UNI_SUR_HIGH_START = 0xD800
  59. UNI_SUR_HIGH_END = 0xDBFF
  60. UNI_SUR_LOW_START = 0xDC00
  61. UNI_SUR_LOW_END = 0xDFFF
  62. UNI_REPL = 0xFFFD
  63. template ones(n: untyped): untyped = ((1 shl n)-1)
  64. template fastRuneAt(s: cstring, i, L: int, result: untyped, doInc = true) =
  65. ## Returns the unicode character ``s[i]`` in `result`. If ``doInc == true``
  66. ## `i` is incremented by the number of bytes that have been processed.
  67. bind ones
  68. if ord(s[i]) <= 127:
  69. result = ord(s[i])
  70. when doInc: inc(i)
  71. elif ord(s[i]) shr 5 == 0b110:
  72. #assert(ord(s[i+1]) shr 6 == 0b10)
  73. if i <= L - 2:
  74. result = (ord(s[i]) and (ones(5))) shl 6 or (ord(s[i+1]) and ones(6))
  75. when doInc: inc(i, 2)
  76. else:
  77. result = UNI_REPL
  78. when doInc: inc(i)
  79. elif ord(s[i]) shr 4 == 0b1110:
  80. if i <= L - 3:
  81. #assert(ord(s[i+1]) shr 6 == 0b10)
  82. #assert(ord(s[i+2]) shr 6 == 0b10)
  83. result = (ord(s[i]) and ones(4)) shl 12 or
  84. (ord(s[i+1]) and ones(6)) shl 6 or
  85. (ord(s[i+2]) and ones(6))
  86. when doInc: inc(i, 3)
  87. else:
  88. result = UNI_REPL
  89. when doInc: inc(i)
  90. elif ord(s[i]) shr 3 == 0b11110:
  91. if i <= L - 4:
  92. #assert(ord(s[i+1]) shr 6 == 0b10)
  93. #assert(ord(s[i+2]) shr 6 == 0b10)
  94. #assert(ord(s[i+3]) shr 6 == 0b10)
  95. result = (ord(s[i]) and ones(3)) shl 18 or
  96. (ord(s[i+1]) and ones(6)) shl 12 or
  97. (ord(s[i+2]) and ones(6)) shl 6 or
  98. (ord(s[i+3]) and ones(6))
  99. when doInc: inc(i, 4)
  100. else:
  101. result = UNI_REPL
  102. when doInc: inc(i)
  103. else:
  104. result = 0xFFFD
  105. when doInc: inc(i)
  106. iterator runes(s: cstring, L: int): int =
  107. var
  108. i = 0
  109. result: int
  110. while i < L:
  111. fastRuneAt(s, i, L, result, true)
  112. yield result
  113. proc newWideCString*(source: cstring, L: int): WideCStringObj =
  114. createWide(result, L * 4 + 2)
  115. #result = cast[wideCString](alloc(L * 4 + 2))
  116. var d = 0
  117. for ch in runes(source, L):
  118. if ch <= UNI_MAX_BMP:
  119. if ch >= UNI_SUR_HIGH_START and ch <= UNI_SUR_LOW_END:
  120. result[d] = UNI_REPLACEMENT_CHAR
  121. else:
  122. result[d] = cast[Utf16Char](uint16(ch))
  123. elif ch > UNI_MAX_UTF16:
  124. result[d] = UNI_REPLACEMENT_CHAR
  125. else:
  126. let ch = ch - halfBase
  127. result[d] = cast[Utf16Char](uint16((ch shr halfShift) + UNI_SUR_HIGH_START))
  128. inc d
  129. result[d] = cast[Utf16Char](uint16((ch and halfMask) + UNI_SUR_LOW_START))
  130. inc d
  131. result[d] = Utf16Char(0)
  132. proc newWideCString*(s: cstring): WideCStringObj =
  133. if s.isNil: return nullWide
  134. result = newWideCString(s, s.len)
  135. proc newWideCString*(s: string): WideCStringObj =
  136. result = newWideCString(s, s.len)
  137. proc `$`*(w: WideCString, estimate: int, replacement: int = 0xFFFD): string =
  138. result = newStringOfCap(estimate + estimate shr 2)
  139. var i = 0
  140. while w[i].int16 != 0'i16:
  141. var ch = ord(w[i])
  142. inc i
  143. if ch >= UNI_SUR_HIGH_START and ch <= UNI_SUR_HIGH_END:
  144. # If the 16 bits following the high surrogate are in the source buffer...
  145. let ch2 = ord(w[i])
  146. # If it's a low surrogate, convert to UTF32:
  147. if ch2 >= UNI_SUR_LOW_START and ch2 <= UNI_SUR_LOW_END:
  148. ch = (((ch and halfMask) shl halfShift) + (ch2 and halfMask)) + halfBase
  149. inc i
  150. else:
  151. #invalid UTF-16
  152. ch = replacement
  153. elif ch >= UNI_SUR_LOW_START and ch <= UNI_SUR_LOW_END:
  154. #invalid UTF-16
  155. ch = replacement
  156. if ch < 0x80:
  157. result.add chr(ch)
  158. elif ch < 0x800:
  159. result.add chr((ch shr 6) or 0xc0)
  160. result.add chr((ch and 0x3f) or 0x80)
  161. elif ch < 0x10000:
  162. result.add chr((ch shr 12) or 0xe0)
  163. result.add chr(((ch shr 6) and 0x3f) or 0x80)
  164. result.add chr((ch and 0x3f) or 0x80)
  165. elif ch <= 0x10FFFF:
  166. result.add chr((ch shr 18) or 0xf0)
  167. result.add chr(((ch shr 12) and 0x3f) or 0x80)
  168. result.add chr(((ch shr 6) and 0x3f) or 0x80)
  169. result.add chr((ch and 0x3f) or 0x80)
  170. else:
  171. # replacement char(in case user give very large number):
  172. result.add chr(0xFFFD shr 12 or 0b1110_0000)
  173. result.add chr(0xFFFD shr 6 and ones(6) or 0b10_0000_00)
  174. result.add chr(0xFFFD and ones(6) or 0b10_0000_00)
  175. proc `$`*(s: WideCString): string =
  176. result = s $ 80