widestrs.nim 6.2 KB

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