summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-05-14 11:56:57 +0200
committerEugen Wissner <belka@caraus.de>2017-05-14 11:56:57 +0200
commit32e19c8b582e27d097004abe5386f45d4b558ea4 (patch)
tree60baafa62ec2da3953d2c4190323842cc4c84b30 /source
parentf5c6c5b483b4412153d2bee7483fda4a3bbe44da (diff)
downloadtanya-32e19c8b582e27d097004abe5386f45d4b558ea4.tar.gz
Rename String.get into toString. Add String.toStringz
Diffstat (limited to 'source')
-rw-r--r--source/tanya/container/string.d40
1 files changed, 36 insertions, 4 deletions
diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d
index f262066..3c89f0f 100644
--- a/source/tanya/container/string.d
+++ b/source/tanya/container/string.d
@@ -199,7 +199,12 @@ struct ByCodeUnit(E)
return typeof(return)(*this.container, this.begin + i, this.begin + j);
}
- inout(E[]) get() inout @trusted
+ const(E)[] toString() const @trusted
+ {
+ return this.begin[0 .. length];
+ }
+
+ E[] toString() @trusted
{
return this.begin[0 .. length];
}
@@ -370,14 +375,14 @@ struct String
@safe @nogc unittest
{
auto s = String("\u10437"w);
- assert("\u10437" == s.get());
+ assert(s == "\u10437");
}
///
@safe @nogc unittest
{
auto s = String("Отказаться от вина - в этом страшная вина."d);
- assert("Отказаться от вина - в этом страшная вина." == s.get());
+ assert(s == "Отказаться от вина - в этом страшная вина.");
}
/**
@@ -942,11 +947,38 @@ struct String
*
* Returns: The array representing the string.
*/
- inout(char[]) get() inout pure nothrow @trusted @nogc
+ const(char)[] toString() const pure nothrow @trusted @nogc
{
return this.data[0 .. this.length_];
}
+ /// Ditto.
+ char[] toString() pure nothrow @trusted @nogc
+ {
+ return this.data[0 .. this.length_];
+ }
+
+ /**
+ * Returns null-terminated string. The returned string is managed by this
+ * object and shouldn't be freed.
+ *
+ * Returns: Null-terminated string.
+ */
+ const(char)[] toStringz() nothrow @trusted @nogc
+ {
+ reserve(length + 1);
+ this.data[length] = '\0';
+ return this.data[0 .. length + 1];
+ }
+
+ ///
+ @safe @nogc unittest
+ {
+ auto s = String("C string.");
+ assert(s.toStringz().length == 10);
+ assert(s.toStringz()[$ - 1] == '\0');
+ }
+
/**
* Returns: The number of code units that are required to encode the string.
*/