Created
January 22, 2012 18:48
-
-
Save Araq/1658174 to your computer and use it in GitHub Desktop.
Windows' wide strings to Nimrod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
type | |
TUtf16Char = distinct int16 | |
WideCString = ptr array[0.. 1_000_000, TUtf16Char] | |
const | |
utf8Encoding = 65001 | |
proc len(w: WideCString): int = | |
while int16(w[result]) != 0'i16: inc result | |
proc MultiByteToWideChar( | |
CodePage: int32, | |
dwFlags: int32, | |
lpMultiByteStr: cstring, | |
cbMultiByte: cint, | |
lpWideCharStr: WideCString, | |
cchWideChar: cint): cint {. | |
stdcall, importc: "MultiByteToWideChar", dynlib: "kernel32".} | |
proc WideCharToMultiByte( | |
CodePage: in32, | |
dwFlags: int32, | |
lpWideCharStr: WideCString, | |
cchWideChar: cint, | |
lpMultiByteStr: cstring, | |
cbMultiByte: cint, | |
lpDefaultChar: cstring=nil, | |
lpUsedDefaultChar: pointer=nil): cint {. | |
stdcall, importc: "WideCharToMultiByte", dynlib: "kernel32".} | |
proc `$`*(s: WideCString, len: int): string = | |
# special case: empty string: needed because MultiByteToWideChar | |
# return 0 in case of error: | |
if len == 0: return "" | |
# educated guess of capacity: | |
var cap = len + len shr 2 | |
result = newStringOfCap(cap) | |
let m = WideCharToMultiByte( | |
CodePage = utf8Encoding, | |
dwFlags = 0'i32, | |
lpWideCharStr = s, | |
cchWideChar = cint(len), | |
lpMultiByteStr = cstring(result), | |
cbMultiByte = cap) | |
if m == 0: | |
# try again; ask for capacity: | |
cap = WideCharToMultiByte( | |
CodePage = utf8Encoding, | |
dwFlags = 0'i32, | |
lpWideCharStr = s, | |
cchWideChar = cint(len), | |
lpMultiByteStr = nil, | |
cbMultiByte = cint(0)) | |
# and do the conversion properly: | |
result = newStringOfCap(cap) | |
let m = WideCharToMultiByte( | |
CodePage = utf8Encoding, | |
dwFlags = 0'i32, | |
lpWideCharStr = s, | |
cchWideChar = cint(len), | |
lpMultiByteStr = cstring(result), | |
cbMultiByte = cap) | |
if m == 0: OSError() | |
setLen(result, m) | |
elif m <= cap: | |
setLen(result, m) | |
else: | |
assert(false) # cannot happen | |
proc `$`*(s: WideCString): string = | |
result = s $ s.len | |
proc toWideCString*(s: string): WideCString = | |
## free after usage with `dealloc`. | |
let cap = s.len+1 | |
result = cast[wideCString](alloc0(cap * 2)) | |
# special case: empty string: needed because MultiByteToWideChar | |
# return 0 in case of error: | |
if s.len == 0: return | |
# convert to utf-16 LE | |
let m = MultiByteToWideChar(CodePage = utf8Encoding, dwFlags = 0'i32, | |
lpMultiByteStr = cstring(s), | |
cbMultiByte = cint(s.len), | |
lpWideCharStr = result, | |
cchWideChar = cint(cap)) | |
if m == 0: OSError() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment