diff options
author | amzak <amzak@users.noreply.github.com> | 2019-08-07 20:56:42 +0300 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2019-08-07 19:56:42 +0200 |
commit | f34ae81971ada93fd4fda871c3da6cba9aab8bff (patch) | |
tree | 18fcf54c8fe5f105e37329efd5d93a265b768406 /lib | |
parent | 9321b33cd2aa82aded7019d197589e3d8d077963 (diff) | |
download | Nim-f34ae81971ada93fd4fda871c3da6cba9aab8bff.tar.gz |
Fixed handling of empty strings for encodings conversions (#11897)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/encodings.nim | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/pure/encodings.nim b/lib/pure/encodings.nim index 460ffbd4a..daf2ca0a2 100644 --- a/lib/pure/encodings.nim +++ b/lib/pure/encodings.nim @@ -325,10 +325,6 @@ proc close*(c: EncodingConverter) = when defined(windows): proc convertToWideString(codePage: CodePage, s: string): string = - # special case: empty string: needed because MultiByteToWideChar - # return 0 in case of error - if s.len == 0: return "" - # educated guess of capacity: var cap = s.len + s.len shr 2 result = newString(cap*2) @@ -396,6 +392,9 @@ when defined(windows): assert(false) # cannot happen proc convertWin(codePageFrom: CodePage, codePageTo: CodePage, s: string): string = + # special case: empty string: needed because MultiByteToWideChar, WideCharToMultiByte + # return 0 in case of error + if s.len == 0: return "" # multiByteToWideChar does not support encoding from code pages below let unsupported = [1201, 12000, 12001] @@ -529,4 +528,13 @@ when not defined(testing) and isMainModule and defined(windows): block should_convert_from_utf8_to_utf16: let original = "\xd1\x82\xd0\xb5\xd1\x81\xd1\x82" # utf-8 test string "ัะตัั" let result = convert(original, "utf-16", "utf-8") - doAssert(result == "\x42\x04\x35\x04\x41\x04\x42\x04") \ No newline at end of file + doAssert(result == "\x42\x04\x35\x04\x41\x04\x42\x04") + + block should_handle_empty_string_for_any_conversion: + let original = "" + var result = convert(original, "utf-16", "utf-8") + doAssert(result == "") + result = convert(original, "utf-8", "utf-16") + doAssert(result == "") + result = convert(original, "windows-1251", "koi8-r") + doAssert(result == "") \ No newline at end of file |