192 changes: 96 additions & 96 deletions std/encoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -378,28 +378,28 @@ template EncoderFunctions()
template ReadFromString()
{
@property bool canRead() { return s.length != 0; }
E peek() { return s[0]; }
E read() { E t = s[0]; s = s[1..$]; return t; }
E peek() @safe pure @nogc nothrow { return s[0]; }
E read() @safe pure @nogc nothrow { E t = s[0]; s = s[1..$]; return t; }
}

template ReverseReadFromString()
{
@property bool canRead() { return s.length != 0; }
E peek() { return s[$-1]; }
E read() { E t = s[$-1]; s = s[0..$-1]; return t; }
E peek() @safe pure @nogc nothrow { return s[$-1]; }
E read() @safe pure @nogc nothrow { E t = s[$-1]; s = s[0..$-1]; return t; }
}

// Various forms of Write

template WriteToString()
{
E[] s;
void write(E c) { s ~= c; }
void write(E c) @safe pure nothrow { s ~= c; }
}

template WriteToArray()
{
void write(E c) { array[0] = c; array = array[1..$]; }
void write(E c) @safe pure @nogc nothrow { array[0] = c; array = array[1..$]; }
}

template WriteToDelegate()
Expand All @@ -418,25 +418,25 @@ template EncoderFunctions()
template SkipViaRead()
{
mixin skipViaRead;
void skip() { skipViaRead(); }
void skip() @safe pure @nogc nothrow { skipViaRead(); }
}

template DecodeViaRead()
{
mixin decodeViaRead;
dchar decode() { return decodeViaRead(); }
dchar decode() @safe pure @nogc nothrow { return decodeViaRead(); }
}

template SafeDecodeViaRead()
{
mixin safeDecodeViaRead;
dchar safeDecode() { return safeDecodeViaRead(); }
dchar safeDecode() @safe pure @nogc nothrow { return safeDecodeViaRead(); }
}

template DecodeReverseViaRead()
{
mixin decodeReverseViaRead;
dchar decodeReverse() { return decodeReverseViaRead(); }
dchar decodeReverse() @safe pure @nogc nothrow { return decodeReverseViaRead(); }
}

// Encoding to different destinations
Expand Down Expand Up @@ -489,14 +489,14 @@ template EncoderFunctions()

// Below are the functions we will ultimately expose to the user

E[] encode(dchar c)
E[] encode(dchar c) @safe pure nothrow
{
mixin EncodeToString e;
e.encode(c);
return e.s;
}

void encode(dchar c, ref E[] array)
void encode(dchar c, ref E[] array) @safe pure nothrow
{
mixin EncodeToArray e;
e.encode(c);
Expand All @@ -508,7 +508,7 @@ template EncoderFunctions()
e.encode(c);
}

void skip(ref const(E)[] s)
void skip(ref const(E)[] s) @safe pure nothrow
{
mixin SkipFromString e;
e.skip();
Expand All @@ -526,7 +526,7 @@ template EncoderFunctions()
return e.safeDecode();
}

dchar decodeReverse(ref const(E)[] s)
dchar decodeReverse(ref const(E)[] s) @safe pure nothrow
{
mixin DecodeReverseFromString e;
return e.decodeReverse();
Expand Down Expand Up @@ -650,7 +650,7 @@ template EncoderInstance(E)

private template GenericEncoder()
{
bool canEncode(dchar c)
bool canEncode(dchar c) @safe pure @nogc nothrow
{
if (c < m_charMapStart || (c > m_charMapEnd && c < 0x100)) return true;
if (c >= 0xFFFD) return false;
Expand All @@ -665,13 +665,13 @@ private template GenericEncoder()
return false;
}

bool isValidCodeUnit(E c)
bool isValidCodeUnit(E c) @safe pure @nogc nothrow
{
if (c < m_charMapStart || c > m_charMapEnd) return true;
return charMap[c-m_charMapStart] != 0xFFFD;
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure @nogc nothrow
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -726,7 +726,7 @@ private template GenericEncoder()
return (c >= m_charMapStart && c <= m_charMapEnd) ? charMap[c-m_charMapStart] : c;
}

@property EString replacementSequence()
@property EString replacementSequence() @safe pure @nogc nothrow
{
return cast(EString)("?");
}
Expand Down Expand Up @@ -763,7 +763,7 @@ template EncoderInstance(CharType : AsciiChar)
return c < 0x80;
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure nothrow @nogc
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -806,7 +806,7 @@ template EncoderInstance(CharType : AsciiChar)
return read();
}

@property EString replacementSequence()
@property EString replacementSequence() @safe pure nothrow @nogc
{
return cast(EString)("?");
}
Expand Down Expand Up @@ -846,7 +846,7 @@ template EncoderInstance(CharType : Latin1Char)
return true;
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure nothrow @nogc
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -882,7 +882,7 @@ template EncoderInstance(CharType : Latin1Char)
return read();
}

@property EString replacementSequence()
@property EString replacementSequence() @safe pure nothrow @nogc
{
return cast(EString)("?");
}
Expand Down Expand Up @@ -915,8 +915,8 @@ private template EncoderInstance(CharType : Latin2Char)
return "ISO-8859-2";
}

private dchar m_charMapStart = 0xa1;
private dchar m_charMapEnd = 0xff;
private static immutable dchar m_charMapStart = 0xa1;
private static immutable dchar m_charMapEnd = 0xff;

private immutable wstring charMap =
"\u0104\u02D8\u0141\u00A4\u013D\u015A\u00A7\u00A8"~
Expand Down Expand Up @@ -995,8 +995,8 @@ private template EncoderInstance(CharType : Windows1250Char)
return "windows-1250";
}

private dchar m_charMapStart = 0x80;
private dchar m_charMapEnd = 0xff;
private static immutable dchar m_charMapStart = 0x80;
private static immutable dchar m_charMapEnd = 0xff;

private immutable wstring charMap =
"\u20AC\uFFFD\u201A\uFFFD\u201E\u2026\u2020\u2021"~
Expand Down Expand Up @@ -1088,8 +1088,8 @@ template EncoderInstance(CharType : Windows1252Char)
return "windows-1252";
}

private dchar m_charMapStart = 0x80;
private dchar m_charMapEnd = 0x9f;
private static immutable dchar m_charMapStart = 0x80;
private static immutable dchar m_charMapEnd = 0x9f;

private immutable wstring charMap =
"\u20AC\uFFFD\u201A\u0192\u201E\u2026\u2020\u2021"~
Expand Down Expand Up @@ -1148,7 +1148,7 @@ template EncoderInstance(CharType : char)
3,3,3,3,3,3,3,3,4,4,4,4,5,5,6,0,
];

private int tails(char c)
private int tails(char c) @safe pure nothrow @nogc
in
{
assert(c >= 0x80);
Expand All @@ -1158,7 +1158,7 @@ template EncoderInstance(CharType : char)
return tailTable[c-0x80];
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure nothrow @nogc
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -1302,7 +1302,7 @@ template EncoderInstance(CharType : wchar)
return true;
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure nothrow @nogc
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -1389,17 +1389,17 @@ template EncoderInstance(CharType : dchar)
return "UTF-32";
}

bool canEncode(dchar c)
bool canEncode(dchar c) @safe pure @nogc nothrow
{
return isValidCodePoint(c);
}

bool isValidCodeUnit(dchar c)
bool isValidCodeUnit(dchar c) @safe pure @nogc nothrow
{
return isValidCodePoint(c);
}

size_t encodedLength(dchar c)
size_t encodedLength(dchar c) @safe pure @nogc nothrow
in
{
assert(canEncode(c));
Expand Down Expand Up @@ -1510,7 +1510,7 @@ bool canEncode(E)(dchar c)
}

///
@safe unittest
@safe pure unittest
{
assert( canEncode!(Latin1Char)('A'));
assert( canEncode!(Latin2Char)('A'));
Expand Down Expand Up @@ -1555,7 +1555,7 @@ bool isValidCodeUnit(E)(E c)
}

///
@system unittest
@system pure unittest
{
assert(!isValidCodeUnit(cast(char)0xC0));
assert(!isValidCodeUnit(cast(char)0xFF));
Expand Down Expand Up @@ -1588,7 +1588,7 @@ bool isValid(E)(const(E)[] s)
}

///
@system unittest
@system pure unittest
{
assert( isValid("\u20AC100"));
assert(!isValid(cast(char[3])[167, 133, 175]));
Expand Down Expand Up @@ -1673,7 +1673,7 @@ immutable(E)[] sanitize(E)(immutable(E)[] s)
}

///
@system unittest
@system pure unittest
{
assert(sanitize("hello \xF0\x80world") == "hello \xEF\xBF\xBDworld");
}
Expand Down Expand Up @@ -1705,7 +1705,7 @@ body
}

///
@system unittest
@system pure unittest
{
assert(firstSequence("\u20AC1000") == "\u20AC".length);
assert(firstSequence("hel") == "h".length);
Expand Down Expand Up @@ -1737,7 +1737,7 @@ body
}

///
@system unittest
@system pure unittest
{
assert(lastSequence("1000\u20AC") == "\u20AC".length);
assert(lastSequence("hellö") == "ö".length);
Expand Down Expand Up @@ -1773,7 +1773,7 @@ body
}

///
@system unittest
@system pure unittest
{
assert(index("\u20AC100",1) == 3);
assert(index("hällo",2) == 3);
Expand Down Expand Up @@ -2248,7 +2248,7 @@ body
}

///
@system unittest
@system pure unittest
{
wstring ws;
// transcode from UTF-8 to UTF-16
Expand All @@ -2261,7 +2261,7 @@ body
assert(ws == "hello world");
}

@system unittest
@system pure unittest
{
import std.meta;
import std.range;
Expand Down Expand Up @@ -2516,7 +2516,7 @@ abstract class EncodingScheme
* Params:
* s = the array to be tested
*/
size_t validLength(const(ubyte)[] s)
size_t validLength()(const(ubyte)[] s)
{
const(ubyte)[] r = s;
const(ubyte)[] t = s;
Expand All @@ -2540,7 +2540,7 @@ abstract class EncodingScheme
* Params:
* s = the string to be sanitized
*/
immutable(ubyte)[] sanitize(immutable(ubyte)[] s)
immutable(ubyte)[] sanitize()(immutable(ubyte)[] s)
{
auto n = validLength(s);
if (n == s.length) return s;
Expand Down Expand Up @@ -2588,7 +2588,7 @@ abstract class EncodingScheme
* Params:
* s = the array to be sliced
*/
size_t firstSequence(const(ubyte)[] s)
size_t firstSequence()(const(ubyte)[] s)
in
{
assert(s.length != 0);
Expand All @@ -2611,7 +2611,7 @@ abstract class EncodingScheme
* Params:
* s = the string to be counted
*/
size_t count(const(ubyte)[] s)
size_t count()(const(ubyte)[] s)
in
{
assert(isValid(s));
Expand All @@ -2637,7 +2637,7 @@ abstract class EncodingScheme
* s = the string to be counted
* n = the current code point index
*/
ptrdiff_t index(const(ubyte)[] s, size_t n)
ptrdiff_t index()(const(ubyte)[] s, size_t n)
in
{
assert(isValid(s));
Expand Down Expand Up @@ -2702,39 +2702,39 @@ class EncodingSchemeASCII : EncodingScheme
return "ASCII";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(AsciiChar)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(AsciiChar)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(AsciiChar[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(AsciiChar)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(AsciiChar)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
Expand Down Expand Up @@ -2786,39 +2786,39 @@ class EncodingSchemeLatin1 : EncodingScheme
return "ISO-8859-1";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(Latin1Char)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(Latin1Char)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(Latin1Char[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Latin1Char)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Latin1Char)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
Expand Down Expand Up @@ -2862,39 +2862,39 @@ class EncodingSchemeLatin2 : EncodingScheme
return "ISO-8859-2";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(Latin2Char)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(Latin2Char)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(Latin2Char[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Latin2Char)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Latin2Char)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
Expand Down Expand Up @@ -2930,39 +2930,39 @@ class EncodingSchemeWindows1250 : EncodingScheme
return "windows-1250";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(Windows1250Char)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(Windows1250Char)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(Windows1250Char[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Windows1250Char)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Windows1250Char)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
Expand Down Expand Up @@ -2998,39 +2998,39 @@ class EncodingSchemeWindows1252 : EncodingScheme
return "windows-1252";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(Windows1252Char)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(Windows1252Char)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(Windows1252Char[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Windows1252Char)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(Windows1252Char)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
Expand Down Expand Up @@ -3066,39 +3066,39 @@ class EncodingSchemeUtf8 : EncodingScheme
return "UTF-8";
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(char)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(char)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(char[])buffer;
return std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(char)[]) s;
dchar c = std.encoding.decode(t);
s = s[$-t.length..$];
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
{
auto t = cast(const(char)[]) s;
dchar c = std.encoding.safeDecode(t);
s = s[$-t.length..$];
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"\uFFFD";
}
Expand Down Expand Up @@ -3135,23 +3135,23 @@ class EncodingSchemeUtf16Native : EncodingScheme
return NAME;
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(wchar)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(wchar)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(wchar[])buffer;
return wchar.sizeof * std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
in
{
assert((s.length & 1) == 0);
Expand All @@ -3164,7 +3164,7 @@ class EncodingSchemeUtf16Native : EncodingScheme
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
in
{
assert((s.length & 1) == 0);
Expand All @@ -3177,7 +3177,7 @@ class EncodingSchemeUtf16Native : EncodingScheme
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"\uFFFD"w;
}
Expand Down Expand Up @@ -3231,23 +3231,23 @@ class EncodingSchemeUtf32Native : EncodingScheme
return NAME;
}

override bool canEncode(dchar c)
override bool canEncode(dchar c) @safe pure nothrow @nogc
{
return std.encoding.canEncode!(dchar)(c);
}

override size_t encodedLength(dchar c)
override size_t encodedLength(dchar c) @safe pure nothrow @nogc
{
return std.encoding.encodedLength!(dchar)(c);
}

override size_t encode(dchar c, ubyte[] buffer)
override size_t encode(dchar c, ubyte[] buffer) @safe pure nothrow @nogc
{
auto r = cast(dchar[])buffer;
return dchar.sizeof * std.encoding.encode(c,r);
}

override dchar decode(ref const(ubyte)[] s)
override dchar decode(ref const(ubyte)[] s) @safe pure nothrow @nogc
in
{
assert((s.length & 3) == 0);
Expand All @@ -3260,7 +3260,7 @@ class EncodingSchemeUtf32Native : EncodingScheme
return c;
}

override dchar safeDecode(ref const(ubyte)[] s)
override dchar safeDecode(ref const(ubyte)[] s) @safe pure nothrow @nogc
in
{
assert((s.length & 3) == 0);
Expand All @@ -3273,7 +3273,7 @@ class EncodingSchemeUtf32Native : EncodingScheme
return c;
}

override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"\uFFFD"d;
}
Expand Down