widestrs.nim 5.0 KB

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