widestrs.nim 6.5 KB

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