Zig: Convert string literals to UTF-16 LE without any memory management via STD.

Created on 29 Jan 2020  路  1Comment  路  Source: ziglang/zig

From discussion in IRC. The specific issue that came up was how to convert a Zig String to a Windows API expecting a UTF-16 formatted string like [*c]c_ushort.

Usage would resemble:

const W = std.unicode.utf16leStringLiteral;
FooWindowsFunc(W("string literal"))
contributor friendly enhancement standard library

Most helpful comment

Should be closed by cf38ce970.

const std = @import("std");
const W = std.unicode.utf8ToUtf16LeStringLiteral;
const c = @cImport({
    @cDefine("_NO_CRT_STDIO_INLINE", "1");
    @cInclude("stdio.h");
});
usingnamespace std.os.windows;

extern "user32" fn MessageBoxW(hWnd: ?HANDLE, lpText: ?LPCWSTR, lpCaption: ?LPCWSTR, uType: UINT) callconv(.Stdcall) c_int;

pub fn main() void {
    _ = c.wprintf(W("Hello World\n"));
    _ = c.wprintf(W("Bonjour %s - %ls\n"), W("String"), W("Strange"));
    _ = c.wprintf(W("H\n"));
    _ = c.wprintf(W("H"));

    _ = MessageBoxW(null, W("饜惙Content饜惙"), W("Turtle"), 0);
}

>All comments

Should be closed by cf38ce970.

const std = @import("std");
const W = std.unicode.utf8ToUtf16LeStringLiteral;
const c = @cImport({
    @cDefine("_NO_CRT_STDIO_INLINE", "1");
    @cInclude("stdio.h");
});
usingnamespace std.os.windows;

extern "user32" fn MessageBoxW(hWnd: ?HANDLE, lpText: ?LPCWSTR, lpCaption: ?LPCWSTR, uType: UINT) callconv(.Stdcall) c_int;

pub fn main() void {
    _ = c.wprintf(W("Hello World\n"));
    _ = c.wprintf(W("Bonjour %s - %ls\n"), W("String"), W("Strange"));
    _ = c.wprintf(W("H\n"));
    _ = c.wprintf(W("H"));

    _ = MessageBoxW(null, W("饜惙Content饜惙"), W("Turtle"), 0);
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewrk picture andrewrk  路  3Comments

fengb picture fengb  路  3Comments

DavidYKay picture DavidYKay  路  3Comments

jayschwa picture jayschwa  路  3Comments

dobkeratops picture dobkeratops  路  3Comments