widestrs.nim 7.1 KB

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