twchartoutf8.nim 2.6 KB

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