widestrs.nim 6.9 KB

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