Skip to content

Commit

Permalink
Merge pull request #5229 from schveiguy/fixtextwriter
Browse files Browse the repository at this point in the history
Fix issue 17229 - File.byChunk (ubyte) w/ stdout.lockingTextWriter corrupts utf-8 data (and is very slow)
merged-on-behalf-of: Jack Stouffer <jack@jackstouffer.com>
  • Loading branch information
dlang-bot committed Mar 17, 2017
2 parents 77578e5 + ca20f0b commit 3568cb3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -2704,15 +2704,16 @@ $(D Range) that locks the file and allows fast writing to it.

/// Range primitive implementations.
void put(A)(A writeme)
if (is(ElementType!A : const(dchar)) &&
if ((isSomeChar!(Unqual!(ElementType!A)) ||
is(ElementType!A : const(ubyte))) &&
isInputRange!A &&
!isInfinite!A)
{
import std.exception : errnoEnforce;

alias C = ElementEncodingType!A;
static assert(!is(C == void));
static if (isSomeString!A && C.sizeof == 1)
static if (isSomeString!A && C.sizeof == 1 || is(A : const(ubyte)[]))
{
if (orientation_ <= 0)
{
Expand All @@ -2724,15 +2725,16 @@ $(D Range) that locks the file and allows fast writing to it.
}
}

// put each character in turn
foreach (dchar c; writeme)
// put each element in turn.
alias Elem = Unqual!(ElementType!A);
foreach (Elem c; writeme)
{
put(c);
}
}

/// ditto
void put(C)(C c) @safe if (is(C : const(dchar)))
void put(C)(C c) @safe if (isSomeChar!C || is(C : const(ubyte)))
{
import std.traits : Parameters;
static auto trustedFPUTC(int ch, _iobuf* h) @trusted
Expand Down Expand Up @@ -3225,8 +3227,9 @@ void main()
writer.put('');
writer.put(chain(only(''), only('')));
writer.put(repeat('#', 12)); // BUG 11945
writer.put(cast(immutable(ubyte)[])"日本語"); // Bug 17229
}
assert(File(deleteme).readln() == "日本語日本語日本語日本語############");
assert(File(deleteme).readln() == "日本語日本語日本語日本語############日本語");
}

@safe unittest
Expand Down

0 comments on commit 3568cb3

Please sign in to comment.