Merge pull request #4599 from JackStouffer/format

Make std.encoding mostly @safe pure nothrow @nogc
This commit is contained in:
Andrei Alexandrescu 2016-09-16 21:54:55 -04:00 committed by GitHub
commit 4889e48a90

View file

@ -378,15 +378,15 @@ 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
@ -394,12 +394,12 @@ template EncoderFunctions()
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()
@ -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
@ -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);
@ -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();
@ -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();
@ -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;
@ -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));
@ -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)("?");
}
@ -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));
@ -806,7 +806,7 @@ template EncoderInstance(CharType : AsciiChar)
return read();
}
@property EString replacementSequence()
@property EString replacementSequence() @safe pure nothrow @nogc
{
return cast(EString)("?");
}
@ -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));
@ -882,7 +882,7 @@ template EncoderInstance(CharType : Latin1Char)
return read();
}
@property EString replacementSequence()
@property EString replacementSequence() @safe pure nothrow @nogc
{
return cast(EString)("?");
}
@ -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"~
@ -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"~
@ -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"~
@ -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);
@ -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));
@ -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));
@ -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));
@ -1510,7 +1510,7 @@ bool canEncode(E)(dchar c)
}
///
@safe unittest
@safe pure unittest
{
assert( canEncode!(Latin1Char)('A'));
assert( canEncode!(Latin2Char)('A'));
@ -1555,7 +1555,7 @@ bool isValidCodeUnit(E)(E c)
}
///
@system unittest
@system pure unittest
{
assert(!isValidCodeUnit(cast(char)0xC0));
assert(!isValidCodeUnit(cast(char)0xFF));
@ -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]));
@ -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");
}
@ -1705,7 +1705,7 @@ body
}
///
@system unittest
@system pure unittest
{
assert(firstSequence("\u20AC1000") == "\u20AC".length);
assert(firstSequence("hel") == "h".length);
@ -1737,7 +1737,7 @@ body
}
///
@system unittest
@system pure unittest
{
assert(lastSequence("1000\u20AC") == "\u20AC".length);
assert(lastSequence("hellö") == "ö".length);
@ -1773,7 +1773,7 @@ body
}
///
@system unittest
@system pure unittest
{
assert(index("\u20AC100",1) == 3);
assert(index("hällo",2) == 3);
@ -2248,7 +2248,7 @@ body
}
///
@system unittest
@system pure unittest
{
wstring ws;
// transcode from UTF-8 to UTF-16
@ -2261,7 +2261,7 @@ body
assert(ws == "hello world");
}
@system unittest
@system pure unittest
{
import std.meta;
import std.range;
@ -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;
@ -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;
@ -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);
@ -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));
@ -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));
@ -2702,23 +2702,23 @@ 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);
@ -2726,7 +2726,7 @@ class EncodingSchemeASCII : EncodingScheme
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);
@ -2734,7 +2734,7 @@ class EncodingSchemeASCII : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
@ -2786,23 +2786,23 @@ 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);
@ -2810,7 +2810,7 @@ class EncodingSchemeLatin1 : EncodingScheme
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);
@ -2818,7 +2818,7 @@ class EncodingSchemeLatin1 : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
@ -2862,23 +2862,23 @@ 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);
@ -2886,7 +2886,7 @@ class EncodingSchemeLatin2 : EncodingScheme
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);
@ -2894,7 +2894,7 @@ class EncodingSchemeLatin2 : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
@ -2930,23 +2930,23 @@ 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);
@ -2954,7 +2954,7 @@ class EncodingSchemeWindows1250 : EncodingScheme
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);
@ -2962,7 +2962,7 @@ class EncodingSchemeWindows1250 : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
@ -2998,23 +2998,23 @@ 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);
@ -3022,7 +3022,7 @@ class EncodingSchemeWindows1252 : EncodingScheme
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);
@ -3030,7 +3030,7 @@ class EncodingSchemeWindows1252 : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"?";
}
@ -3066,23 +3066,23 @@ 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);
@ -3090,7 +3090,7 @@ class EncodingSchemeUtf8 : EncodingScheme
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);
@ -3098,7 +3098,7 @@ class EncodingSchemeUtf8 : EncodingScheme
return c;
}
override @property immutable(ubyte)[] replacementSequence()
override @property immutable(ubyte)[] replacementSequence() @safe pure nothrow @nogc
{
return cast(immutable(ubyte)[])"\uFFFD";
}
@ -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);
@ -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);
@ -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;
}
@ -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);
@ -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);
@ -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;
}