summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorandri lim <jangko128@gmail.com>2018-07-30 17:04:49 +0700
committerAndreas Rumpf <rumpf_a@web.de>2018-07-30 12:04:49 +0200
commit2e6d073be11b6d365739f1cccefbcfe3055d8da7 (patch)
treedbc84ff7607f23498cf399de42c3d31c9e5e8bdf
parent6b40114f21e681b0110a74add7a61b16df4d2fb8 (diff)
downloadNim-2e6d073be11b6d365739f1cccefbcfe3055d8da7.tar.gz
fixes #8468, stdlib.encodings.convert not crash anymore on windows (#8470)
-rw-r--r--lib/pure/encodings.nim8
-rw-r--r--tests/stdlib/tencoding.nim21
2 files changed, 25 insertions, 4 deletions
diff --git a/lib/pure/encodings.nim b/lib/pure/encodings.nim
index c67cd7579..3c1cf73f4 100644
--- a/lib/pure/encodings.nim
+++ b/lib/pure/encodings.nim
@@ -332,7 +332,7 @@ when defined(windows):
     if s.len == 0: return ""
     # educated guess of capacity:
     var cap = s.len + s.len shr 2
-    result = newStringOfCap(cap*2)
+    result = newString(cap*2)
     # convert to utf-16 LE
     var m = multiByteToWideChar(codePage = c.src, dwFlags = 0'i32,
                                 lpMultiByteStr = cstring(s),
@@ -347,7 +347,7 @@ when defined(windows):
                                 lpWideCharStr = nil,
                                 cchWideChar = cint(0))
       # and do the conversion properly:
-      result = newStringOfCap(cap*2)
+      result = newString(cap*2)
       m = multiByteToWideChar(codePage = c.src, dwFlags = 0'i32,
                               lpMultiByteStr = cstring(s),
                               cbMultiByte = cint(s.len),
@@ -364,7 +364,7 @@ when defined(windows):
     if int(c.dest) == 1200: return
     # otherwise the fun starts again:
     cap = s.len + s.len shr 2
-    var res = newStringOfCap(cap)
+    var res = newString(cap)
     m = wideCharToMultiByte(
       codePage = c.dest,
       dwFlags = 0'i32,
@@ -382,7 +382,7 @@ when defined(windows):
         lpMultiByteStr = nil,
         cbMultiByte = cint(0))
       # and do the conversion properly:
-      res = newStringOfCap(cap)
+      res = newString(cap)
       m = wideCharToMultiByte(
         codePage = c.dest,
         dwFlags = 0'i32,
diff --git a/tests/stdlib/tencoding.nim b/tests/stdlib/tencoding.nim
new file mode 100644
index 000000000..d6ff7ab32
--- /dev/null
+++ b/tests/stdlib/tencoding.nim
@@ -0,0 +1,21 @@
+discard """
+  output: '''OK'''
+"""
+
+#bug #8468
+
+import encodings, strutils
+
+when defined(windows):
+  var utf16to8 = open(destEncoding = "utf-16", srcEncoding = "utf-8")
+  var s = "some string"
+  var c = utf16to8.convert(s)
+
+  var z = newStringOfCap(s.len * 2)
+  for x in s:
+    z.add x
+    z.add chr(0)
+
+  doAssert z == c
+
+echo "OK"