digitalmars.D - 2.067 should modify writeln function, make it display correctly
- FrankLike (40/40) Jan 25 2015 Many people want to display their string(unicode),but always to
- FrankLike (44/44) Jan 25 2015 Many people want to display their string(ANSI),but always to
- AndyC (9/15) Jan 25 2015 But .. some people switch cmd.exe to utf8, so you'll break them.
- FrankLike (10/29) Jan 25 2015 Why not switch by Version:
- FrankLike (4/21) Jan 25 2015 writeln(to!(string)(toMBSz("中文1")," ",to!(string)(toMBSz(中文2));
- FrankLike (4/5) Jan 25 2015 We can use 'chcp 65001',but for exe'consumer,it's difficult to
- Martin Krejcirik (2/2) Jan 25 2015 Windows console is broken, I recommend using API functions
- Kagamin (5/8) Jan 25 2015 https://issues.dlang.org/show_bug.cgi?id=2742
Many people want to display their string(unicode),but always to convert string by 'toMBSz',and it can't convert to immutable char[],it's difficult to display their unicode string in CMD.EXE. Now,I find a function,if add it in 'writeln' function,D will very be popular! string toMBS(string s, uint codePage = 0) { // Only need to do this if any chars have the high bit set foreach (char c; s) { if (c >= 0x80) { char[] result; int readLen; const wchar* ws = std.utf.toUTF16z(s); result.length = WideCharToMultiByte(codePage, 0, ws, -1, null, 0,null, null); if (result.length) { readLen = WideCharToMultiByte(codePage, 0, ws, -1, result.ptr,result.length, null, null); } if (!readLen || readLen != result.length) { throw new Exception("Couldn't convert string: " ~ sysErrorString(GetLastError())); } return cast(string)result[0..$-1]; } } return s; } Now ,I use it : string toStr(string s) { return toMBS(s,0); } writeln(toStr("中文"));// chcp: 936 So difficult! What do you think?
Jan 25 2015
Many people want to display their string(ANSI),but always to convert string by 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI string in CMD.EXE. We can use 'chcp 65001',but for exe'consumer,it's difficult to use. Now,I find a function,if add it in 'writeln' function,D will very be popular! string toMBS(string s, uint codePage = 0) { // Only need to do this if any chars have the high bit set foreach (char c; s) { if (c >= 0x80) { char[] result; int readLen; const wchar* ws = std.utf.toUTF16z(s); result.length = WideCharToMultiByte(codePage, 0, ws, -1, null, 0,null, null); if (result.length) { readLen = WideCharToMultiByte(codePage, 0, ws, -1, result.ptr,result.length, null, null); } if (!readLen || readLen != result.length) { throw new Exception("Couldn't convert string: " ~ sysErrorString(GetLastError())); } return cast(string)result[0..$-1]; } } return s; } Now ,I use it : string toStr(string s) { return toMBS(s,0); } writeln(toStr("中文")); So difficult! What do you think?
Jan 25 2015
On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote:Many people want to display their string(ANSI),but always to convert string by 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI string in CMD.EXE. What do you think?But .. some people switch cmd.exe to utf8, so you'll break them. http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8 not to mention all the linux people it'll break. Perhaps: import std.windows.charset; import std.conv; then: writeln(to!(string)(toMBSz(utf8)));
Jan 25 2015
On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote:On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote:Why not switch by Version: version(Windows) { writeln(to!(string)(toMBSz(utf8))); } else version(linux) { ... }Many people want to display their string(ANSI),but always to convert string by 'toMBSz',and it can't convert to immutable char[],it's difficult to display their ANSI string in CMD.EXE. What do you think?But .. some people switch cmd.exe to utf8, so you'll break them. http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8 not to mention all the linux people it'll break. Perhaps: import std.windows.charset; import std.conv; then: writeln(to!(string)(toMBSz(utf8)));
Jan 25 2015
On Sunday, 25 January 2015 at 16:33:51 UTC, FrankLike wrote:writeln(to!(string)(toMBSz("中文1")," ",to!(string)(toMBSz(中文2)); Can't easy to do: writeln("中文1"," ","中文2");Perhaps: import std.windows.charset; import std.conv; then: writeln(to!(string)(toMBSz(utf8)));Why not switch by Version: version(Windows) { writeln(to!(string)(toMBSz(utf8))); } else version(linux) { ... }
Jan 25 2015
On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote:http://superuser.com/questions/269818/change-default-code-page-of-windows-console-to-utf-8We can use 'chcp 65001',but for exe'consumer,it's difficult to use. Exe'Consumer! they are difficult to use!
Jan 25 2015
Why we can not simply automatically switch CMD to UTF-8 before app start? I do not see any minuses in this solution.
Jan 25 2015
On Sunday, 25 January 2015 at 18:23:03 UTC, Suliman wrote:Why we can not simply automatically switch CMD to UTF-8 before app start? I do not see any minuses in this solution.+1. I use import std.stdio; import std.c.windows.windows; void main() { SetConsoleOutputCP(65001); writeln(utf-8 text); }
Jan 25 2015
On Sunday, 25 January 2015 at 20:24:30 UTC, Dmitry wrote:On Sunday, 25 January 2015 at 18:23:03 UTC, Suliman wrote:If you don't modify the font,display will be bad.Why we can not simply automatically switch CMD to UTF-8 before app start? I do not see any minuses in this solution.+1. I use import std.stdio; import std.c.windows.windows; void main() { SetConsoleOutputCP(65001); writeln(utf-8 text); }
Jan 25 2015
On Sunday, 25 January 2015 at 21:59:41 UTC, FrankLike wrote:If you don't modify the font,display will be bad.Indeed. Although in my case it is not necessary (it shows the localized texts in the localized os - so, default fonts supports it language).
Jan 25 2015
On Sun, 25 Jan 2015 16:55:29 +0000, FrankLike wrote:On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote: =20windows-console-to-utf-8http://superuser.com/questions/269818/change-default-code-page-of-=20 We can use 'chcp 65001',but for exe'consumer,it's difficult to use. =20 Exe'Consumer! they are difficult to use!you can provide .bat file that will change codepage and run your exe.=
Jan 25 2015
Windows console is broken, I recommend using API functions (WriteConsole) instead.
Jan 25 2015
On Sunday, 25 January 2015 at 16:09:59 UTC, FrankLike wrote:What do you think?https://issues.dlang.org/show_bug.cgi?id=2742 On Sunday, 25 January 2015 at 18:47:31 UTC, Martin Krejcirik wrote:Windows console is broken, I recommend using API functions (WriteConsole) instead.Well, that function writes to the windows console.
Jan 25 2015