widestrs.nim 7.3 KB

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