twchartoutf8.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. discard """
  2. output: '''OK'''
  3. """
  4. import std/[syncio, assertions]
  5. #assume WideCharToMultiByte always produce correct result
  6. #windows only
  7. when not defined(windows):
  8. echo "OK"
  9. else:
  10. import std/widestrs
  11. {.push gcsafe.}
  12. const CP_UTF8 = 65001'i32
  13. type
  14. LPBOOL = ptr int32
  15. LPWCSTR = ptr uint16
  16. proc WideCharToMultiByte*(CodePage: int32, dwFlags: int32,
  17. lpWideCharStr: LPWCSTR, cchWideChar: int32,
  18. lpMultiByteStr: cstring, cchMultiByte: int32,
  19. lpDefaultChar: cstring, lpUsedDefaultChar: LPBOOL): int32{.
  20. stdcall, dynlib: "kernel32", importc: "WideCharToMultiByte".}
  21. {.pop.}
  22. proc convertToUTF8(wc: WideCString, wclen: int32): string =
  23. let size = WideCharToMultiByte(CP_UTF8, 0'i32, cast[LPWCSTR](addr(wc[0])), wclen,
  24. cstring(nil), 0'i32, cstring(nil), LPBOOL(nil))
  25. result = newString(size)
  26. let res = WideCharToMultiByte(CP_UTF8, 0'i32, cast[LPWCSTR](addr(wc[0])), wclen,
  27. cstring(result), size, cstring(nil), LPBOOL(nil))
  28. doAssert size == res
  29. proc testCP(wc: WideCString, lo, hi: int) =
  30. var x = 0
  31. let chunk = 1024
  32. for i in lo..hi:
  33. wc[x] = cast[Utf16Char](i)
  34. if (x >= chunk) or (i >= hi):
  35. wc[x] = Utf16Char(0)
  36. var a = convertToUTF8(wc, int32(x))
  37. var b = wc $ chunk
  38. doAssert a == b
  39. x = 0
  40. inc x
  41. proc testCP2(wc: WideCString, lo, hi: int) =
  42. doAssert((lo >= 0x10000) and (hi <= 0x10FFFF))
  43. var x = 0
  44. let chunk = 1024
  45. for i in lo..hi:
  46. let ch = i - 0x10000
  47. let W1 = 0xD800 or (ch shr 10)
  48. let W2 = 0xDC00 or (0x3FF and ch)
  49. wc[x] = cast[Utf16Char](W1)
  50. wc[x+1] = cast[Utf16Char](W2)
  51. inc(x, 2)
  52. if (x >= chunk) or (i >= hi):
  53. wc[x] = Utf16Char(0)
  54. var a = convertToUTF8(wc, int32(x))
  55. var b = wc $ chunk
  56. doAssert a == b
  57. x = 0
  58. #RFC-2781 "UTF-16, an encoding of ISO 10646"
  59. var wc: WideCString = newWideCString(1024 * 2)
  60. #U+0000 to U+D7FF
  61. #skip the U+0000
  62. wc.testCP(1, 0xD7FF)
  63. #U+E000 to U+FFFF
  64. wc.testCP(0xE000, 0xFFFF)
  65. #U+10000 to U+10FFFF
  66. wc.testCP2(0x10000, 0x10FFFF)
  67. #invalid UTF-16
  68. const
  69. b = "\xEF\xBF\xBD"
  70. c = "\xEF\xBF\xBF"
  71. wc[0] = cast[Utf16Char](0xDC00)
  72. wc[1] = Utf16Char(0)
  73. var a = $wc
  74. doAssert a == b
  75. wc[0] = cast[Utf16Char](0xFFFF)
  76. wc[1] = cast[Utf16Char](0xDC00)
  77. wc[2] = Utf16Char(0)
  78. a = $wc
  79. doAssert a == c & b
  80. wc[0] = cast[Utf16Char](0xD800)
  81. wc[1] = Utf16Char(0)
  82. a = $wc
  83. doAssert a == b
  84. wc[0] = cast[Utf16Char](0xD800)
  85. wc[1] = cast[Utf16Char](0xFFFF)
  86. wc[2] = Utf16Char(0)
  87. a = $wc
  88. doAssert a == b & c
  89. echo "OK"