widestrs.nim 5.1 KB

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