D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15846 - Windows console cannot read properly UTF-8 lines
Summary: Windows console cannot read properly UTF-8 lines
Status: RESOLVED DUPLICATE of issue 15845
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Windows
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-29 00:08 UTC by JVortex
Modified: 2016-03-29 00:22 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description JVortex 2016-03-29 00:08:21 UTC
module runnable;

import std.stdio;
import std.string : chomp;
import std.experimental.logger;

void doSomethingElse(char[] data)
{
    writeln("hello!");
}

int main(string[] args)
{
    /* Some fix I found in UTF-8 related problems, I'm using Windows 10 */
    version(Windows)
    {
        import core.sys.windows.windows;
        if (SetConsoleCP(65001) == 0)
            throw new Exception("failure");
        if (SetConsoleOutputCP(65001) == 0)
            throw new Exception("failure");
    }
    FileLogger fl = new FileLogger("log.log");
    char[] readerBuffer;

    readln(readerBuffer);
    readerBuffer = chomp(readerBuffer);

    fl.info(readerBuffer.length); /* <- if the readed string contains at least one UTF-8
                                        char this gets 0, else it prints its length
                                   */

    if (readerBuffer != "exit")
        doSomethingElse(readerBuffer);

    /* Also, all the following code doesn't run as expected, the program doesn't wait for
       you, it executes readln() even without pressing/sending a key */
    readln(readerBuffer);
    fl.info(readerBuffer.length);
    readln(readerBuffer);
    fl.info(readerBuffer.length);
    readln(readerBuffer);
    fl.info(readerBuffer.length);
    readln(readerBuffer);
    fl.info(readerBuffer.length);
    readln(readerBuffer);
    fl.info(readerBuffer.length);

    return 0;
}

The code above doesn't work properly on windows if you input at least one of the following chars: á, é, í, ó, ú, ñ, à, è, ì, ò, ù (I haven't tried with others).

This behaviour is reproducible ONLY using O.S. Windows. It has been tested in Debian and Mac OS X and it works correctly.

Also is different for each mode: 32-bit (DMC stdlib) and 64-bit (MVSC stdlib).In both, the line is not read properly (I get a length of 0). On 32-bit, the program exits immediately, indicating it cannot read any more data.
On 64-bit, the program continues to allow input.
Comment 1 Steven Schveighoffer 2016-03-29 00:22:28 UTC

*** This issue has been marked as a duplicate of issue 15845 ***