www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - 2.067 should modify writeln function, make it display correctly

reply "FrankLike" <1150015857 qq.com> writes:
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
parent reply "FrankLike" <1150015857 qq.com> writes:
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
next sibling parent reply "AndyC" <andy squeakycode.net> writes:
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
next sibling parent reply "FrankLike" <1150015857 qq.com> writes:
On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote:
 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)));
Why not switch by Version: version(Windows) { writeln(to!(string)(toMBSz(utf8))); } else version(linux) { ... }
Jan 25 2015
parent "FrankLike" <1150015857 qq.com> writes:
On Sunday, 25 January 2015 at 16:33:51 UTC, FrankLike wrote:

 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) { ... }
writeln(to!(string)(toMBSz("中文1")," ",to!(string)(toMBSz(中文2)); Can't easy to do: writeln("中文1"," ","中文2");
Jan 25 2015
prev sibling parent reply "FrankLike" <1150015857 qq.com> writes:
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-8
We can use 'chcp 65001',but for exe'consumer,it's difficult to use. Exe'Consumer! they are difficult to use!
Jan 25 2015
next sibling parent reply "Suliman" <evermind live.ru> writes:
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
parent reply "Dmitry" <dmitry indiedev.ru> writes:
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
parent reply "FrankLike" <1150015857 qq.com> writes:
On Sunday, 25 January 2015 at 20:24:30 UTC, Dmitry wrote:
 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); }
If you don't modify the font,display will be bad.
Jan 25 2015
parent "Dmitry" <dmitry indiedev.ru> writes:
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
prev sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
On Sun, 25 Jan 2015 16:55:29 +0000, FrankLike wrote:

 On Sunday, 25 January 2015 at 16:22:20 UTC, AndyC wrote:
=20
 http://superuser.com/questions/269818/change-default-code-page-of-
windows-console-to-utf-8
=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
prev sibling parent reply "Martin Krejcirik" <mk-junk i-line.cz> writes:
Windows console is broken, I recommend using API functions 
(WriteConsole) instead.
Jan 25 2015
parent "Kagamin" <spam here.lot> writes:
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