Skip to content

Commit

Permalink
let transcode accept mutable/const characters
Browse files Browse the repository at this point in the history
  • Loading branch information
aG0aep6G committed Aug 8, 2016
1 parent 4a634ed commit bc250ab
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions std/encoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -2199,28 +2199,28 @@ body
See_Also:
$(REF to, std,conv)
*/
void transcode(Src,Dst)(immutable(Src)[] s,out immutable(Dst)[] r)
void transcode(Src, Dst)(Src[] s, out Dst[] r)
in
{
assert(isValid(s));
}
body
{
static if (is(Src==Dst))
static if (is(Src == Dst) && is(Src == immutable))
{
r = s;
}
else static if (is(Src==AsciiChar))
else static if (is(Unqual!Src == AsciiChar))
{
transcode!(char,Dst)(cast(string)s,r);
transcode(cast(const(char)[])s, r);
}
else
{
static if (is(Dst == wchar))
static if (is(Unqual!Dst == wchar))
{
immutable minReservePlace = 2;
}
else static if (is(Dst == dchar))
else static if (is(Unqual!Dst == dchar))
{
immutable minReservePlace = 1;
}
Expand All @@ -2229,8 +2229,8 @@ body
immutable minReservePlace = 6;
}

Dst[] buffer = new Dst[s.length];
Dst[] tmpBuffer = buffer;
auto buffer = new Unqual!Dst[s.length];
auto tmpBuffer = buffer;
const(Src)[] t = s;

while (t.length != 0)
Expand All @@ -2241,10 +2241,10 @@ body
buffer.length += t.length + minReservePlace;
tmpBuffer = buffer[prevLength - tmpBuffer.length .. $];
}
EncoderInstance!(Dst).encode(decode(t), tmpBuffer);
EncoderInstance!(Unqual!Dst).encode(decode(t), tmpBuffer);
}

r = cast(immutable)buffer[0 .. buffer.length - tmpBuffer.length];
r = cast(Dst[])buffer[0 .. buffer.length - tmpBuffer.length];
}
}

Expand Down Expand Up @@ -2302,6 +2302,44 @@ body
}
}

unittest // mutable/const input/output
{
import std.meta: AliasSeq;

foreach (O; AliasSeq!(Latin1Char, const Latin1Char, immutable Latin1Char))
{
O[] output;

char[] mutableInput = "äbc".dup;
transcode(mutableInput, output);
assert(output == [0xE4, 'b', 'c']);

const char[] constInput = "öbc";
transcode(constInput, output);
assert(output == [0xF6, 'b', 'c']);

immutable char[] immutInput = "übc";
transcode(immutInput, output);
assert(output == [0xFC, 'b', 'c']);
}

// Make sure that const/mutable input is copied.
foreach (C; AliasSeq!(char, const char))
{
C[] input = "foo".dup;
C[] output;
transcode(input, output);
assert(input == output);
assert(input !is output);
}

// But immutable input should not be copied.
string input = "foo";
string output;
transcode(input, output);
assert(input is output);
}

//=============================================================================

/** The base class for exceptions thrown by this module */
Expand Down

0 comments on commit bc250ab

Please sign in to comment.