Add dchar constructor

This commit is contained in:
Eugen Wissner 2017-02-26 22:40:27 +01:00
parent 885fca9b5e
commit 33dbf042c2

View File

@ -12,6 +12,7 @@
*/
module tanya.container.string;
import core.checkedint;
import core.exception;
import core.stdc.string;
import std.algorithm.comparison;
@ -58,6 +59,49 @@ struct String
{
this(allocator);
bool overflow;
auto size = mulu(str.length, 4, overflow);
assert(!overflow);
reserve(size);
auto s = data;
foreach (c; str)
{
if (c < 0x80)
{
*s++ = c & 0x7f;
length_ += 1;
}
else if (c < 0x800)
{
*s++ = 0xc0 | (c >> 6) & 0xff;
*s++ = 0x80 | (c & 0x3f);
length_ += 2;
}
else if (c < 0xd800 || c - 0xe000 < 0x2000)
{
*s++ = 0xe0 | (c >> 12) & 0xff;
*s++ = 0x80 | ((c >> 6) & 0x3f);
*s++ = 0x80 | (c & 0x3f);
length_ += 3;
}
else if (c - 0x10000 < 0x100000)
{
*s++ = 0xf0 | (c >> 18);
*s++ = 0x80 | ((c >> 12) & 0x3f);
*s++ = 0x80 | ((c >> 6) & 0x3f);
*s++ = 0x80 | (c & 0x3f);
length_ += 4;
}
}
}
///
@nogc @safe unittest
{
auto s = String("Отказаться от вина - в этом страшная вина."d);
assert("Отказаться от вина - в этом страшная вина." == s.get);
}
/// Ditto.
@ -175,5 +219,17 @@ struct String
assert(s.capacity == 38);
}
/**
* Returns an array used internally by the string.
* The length of the returned array may be smaller than the size of the
* reserved memory for the string.
*
* Returns: The array representing the string.
*/
inout(char[]) get() inout pure nothrow @trusted @nogc
{
return data[0 .. length_];
}
mixin DefaultAllocator;
}