Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 22, 2019 How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Hi,everyone, In C++, _T can guarantee that when converting from ascii encoding type to unicode encoding type, the program does not need to be modified. What do I need to do in D? Thanks. |
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Tuesday, 22 January 2019 at 13:55:30 UTC, FrankLike wrote:
> Hi,everyone,
> In C++, _T can guarantee that when converting from ascii encoding type to unicode encoding type, the program does not need to be modified. What do I need to do in D?
>
> Thanks.
Hi,
_T is not relevant to C++, but to Windows programming.
In D, there is only Unicode. The language doesn't manipulate strings encoded in Windows local code-pages.
char means UTF-8 (Unicode encoded in 8bit units).
wchar means UTF-16 (Unicode encoded in 16bit units, what Windows documentation calls "Unicode").
dchar means UTF-32 (Unicode encoded in 32bit units).
When manipulating data encoded in Windows local code page, use the ubyte[] type.
|
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Olivier Pisano | On Tuesday, 22 January 2019 at 14:07:48 UTC, Olivier Pisano wrote: > On Tuesday, 22 January 2019 at 13:55:30 UTC, FrankLike wrote: > In D, there is only Unicode. The language doesn't manipulate strings encoded in Windows local code-pages. For example: std::wstring strTest(_T("d://")); UINT nRes = ::GetDriveType(strTest.c_str()); It can work in C++. But: //////////////////////////here is work ok/////////////////////////////////////// import std.stdio; import std.string; import std.conv; import win32.winbase; void main() { string strA_Z ="CD"; auto type = GetDriveType((to!string(strA_Z[0])~":\\").toStringz); writeln(to!string(strA_Z[0])~" is ",type); } //////////////////////////here is work error////////////////////////////////////////// import core.sys.windows.windows; import std.stdio; import std.string; import std.conv; void main() { string strA_Z ="CD"; auto type = GetDriveType((to!string(strA_Z[0])~":\\").toStringz); writeln(to!string(strA_Z[0])~" is ",type); } //////////////////////////////////////////////////////////////////// //---------------Error Info--------------------// slicea2.d(9): Error: function core.sys.windows.winbase.GetDriveTypeW(const(wchar )*) is not callable using argument types (immutable(char)*) slicea2.d(9): cannot pass argument toStringz(to(strA_Z[0]) ~ ":\\") of ty pe immutable(char)* to parameter const(wchar)* Some error is "core.sys.windows.windows"? Thank you. |
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Tuesday, 22 January 2019 at 16:13:57 UTC, FrankLike wrote:
> On Tuesday, 22 January 2019 at 14:07:48 UTC, Olivier Pisano wrote:
Some error is in "core.sys.windows.windows"?
Thank you.
|
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | Use "mystring"w, notice the w after the closing quote. |
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe wrote:
> Use "mystring"w, notice the w after the closing quote.
"GetDriveType" Function is auto work by "_T" in C++,but how to do in D?
|
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe wrote:
> Use "mystring"w, notice the w after the closing quote.
Or toStringz is not work like c_str() in C++?
|
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to FrankLike | On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
> On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe wrote:
>> Use "mystring"w, notice the w after the closing quote.
>
> Or toStringz is not work like c_str() in C++?
stringz creates a char*
but you need a wchar*
|
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Tuesday, January 22, 2019 12:05:32 PM MST Stefan Koch via Digitalmars-d- learn wrote: > On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote: > > On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe > > > > wrote: > >> Use "mystring"w, notice the w after the closing quote. > > > > Or toStringz is not work like c_str() in C++? > > stringz creates a char* > but you need a wchar* std.utf.toUTF16z or toUTFz can do that for you, though if your string is already a wstring, then you can also just concatenate '\0' to it. the big advantage toUTF16z is that it will also convert strings of other character types rather than just wstrings. So, you can write your program using proper UTF-8 strings and then only convert to UTF-16 for the Windows stuff when you have to. https://dlang.org/phobos/std_utf.html#toUTF16z https://dlang.org/phobos/std_utf.html#toUTFz - Jonathan M Davis |
January 22, 2019 Re: How to ensure string compatibility In D? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Tuesday, 22 January 2019 at 19:14:43 UTC, Jonathan M Davis wrote:
> On Tuesday, January 22, 2019 12:05:32 PM MST Stefan Koch via Digitalmars-d- learn wrote:
>> On Tuesday, 22 January 2019 at 16:47:45 UTC, FrankLike wrote:
>> > On Tuesday, 22 January 2019 at 16:18:17 UTC, Adam D. Ruppe
>> >
>> > wrote:
>> >> Use "mystring"w, notice the w after the closing quote.
>> >
>> > Or toStringz is not work like c_str() in C++?
>>
>> stringz creates a char*
>> but you need a wchar*
>
> std.utf.toUTF16z or toUTFz can do that for you, though if your string is already a wstring, then you can also just concatenate '\0' to it. the big advantage toUTF16z is that it will also convert strings of other character types rather than just wstrings. So, you can write your program using proper UTF-8 strings and then only convert to UTF-16 for the Windows stuff when you have to.
>
> https://dlang.org/phobos/std_utf.html#toUTF16z https://dlang.org/phobos/std_utf.html#toUTFz
>
> - Jonathan M Davis
Is there a reason we cannot implement toStringz like:
immutable(TChar)* toStringz(TChar = char)(scope const(TChar)[] s) @trusted pure nothrow;
// Couldn't find a way to get the char type of a string, so couldn't make the following generic:
immutable(char)* toStringz(return scope string s) @trusted pure nothrow;
immutable(wchar)* toStringz(return scope wstring s) @trusted pure nothrow;
immutable(dchar)* toStringz(return scope dstring s) @trusted pure nothrow;
|
Copyright © 1999-2021 by the D Language Foundation