mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 14:40:30 +03:00
Change toUTF16z to use toUTFz.
This commit is contained in:
parent
bc13258c8f
commit
a58e19bc27
1 changed files with 24 additions and 31 deletions
55
std/utf.d
55
std/utf.d
|
@ -1362,37 +1362,6 @@ pure wstring toUTF16(in dchar[] s) @trusted
|
||||||
return r.assumeUnique(); // ok because r is unique
|
return r.assumeUnique(); // ok because r is unique
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
|
||||||
Encodes string $(D s) into UTF-16 and returns the encoded string.
|
|
||||||
$(D toUTF16z) is suitable for calling the 'W' functions in the Win32 API
|
|
||||||
that take an $(D LPWSTR) or $(D LPCWSTR) argument.
|
|
||||||
+/
|
|
||||||
const(wchar)* toUTF16z(in char[] s) @trusted
|
|
||||||
{
|
|
||||||
wchar[] r;
|
|
||||||
size_t slen = s.length;
|
|
||||||
|
|
||||||
r.length = slen + 1;
|
|
||||||
r.length = 0;
|
|
||||||
for (size_t i = 0; i < slen; )
|
|
||||||
{
|
|
||||||
dchar c = s[i];
|
|
||||||
if (c <= 0x7F)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
r ~= cast(wchar)c;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
c = decode(s, i);
|
|
||||||
encode(r, c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
r ~= '\000';
|
|
||||||
|
|
||||||
return r.ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/* =================== Conversion to UTF32 ======================= */
|
/* =================== Conversion to UTF32 ======================= */
|
||||||
|
|
||||||
|
@ -1682,6 +1651,30 @@ unittest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/++
|
||||||
|
$(D toUTF16z) is a convenience function for $(D toUTFz!(const(wchar)*)).
|
||||||
|
|
||||||
|
Encodes string $(D s) into UTF-16 and returns the encoded string.
|
||||||
|
$(D toUTF16z) is suitable for calling the 'W' functions in the Win32 API
|
||||||
|
that take an $(D LPWSTR) or $(D LPCWSTR) argument.
|
||||||
|
+/
|
||||||
|
const(wchar)* toUTF16z(C)(const(C)[] str)
|
||||||
|
if(isSomeChar!C)
|
||||||
|
{
|
||||||
|
return toUTFz!(const(wchar)*)(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.typetuple;
|
||||||
|
|
||||||
|
//toUTFz is already thoroughly tested, so this will just verify that
|
||||||
|
//toUTF16z compiles properly for the various string types.
|
||||||
|
foreach(S; TypeTuple!(string, wstring, dstring))
|
||||||
|
static assert(__traits(compiles, toUTF16z(to!S("hello world"))));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ================================ tests ================================== */
|
/* ================================ tests ================================== */
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue