diff options
| author | Eugen Wissner <belka@caraus.de> | 2017-02-10 19:22:46 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2017-02-10 19:22:46 +0100 |
| commit | f4b90d8b511c12324e6801386bd9f2d822a547dc (patch) | |
| tree | 2f5252bab2bfd79f850ea79c28c7e902c2324858 | |
| parent | b74e5aa4ee57af17450d7ef63547002d99dffdbe (diff) | |
| download | tanya-f4b90d8b511c12324e6801386bd9f2d822a547dc.tar.gz | |
Add string skeleton
| -rw-r--r-- | source/tanya/container/string.d | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/source/tanya/container/string.d b/source/tanya/container/string.d new file mode 100644 index 0000000..05f9e70 --- /dev/null +++ b/source/tanya/container/string.d @@ -0,0 +1,77 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Copyright: Eugene Wissner 2016-2017. + * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/, + * Mozilla Public License, v. 2.0). + * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner) + */ +module tanya.container.string; + +import core.exception; +import core.stdc.string; +import tanya.memory; + +/** + * UTF-8 string. + */ +struct String +{ + private char[] data; + private size_t length_; + + invariant + { + assert(length_ <= data.length); + } + + /// Ditto. + this(const(char)[] str, shared Allocator allocator = defaultAllocator) + nothrow @trusted @nogc + { + this(allocator); + + data = cast(char[]) allocator.allocate(str.length); + if (str.length > 0 && data is null) + { + onOutOfMemoryErrorNoGC(); + } + memcpy(data.ptr, str.ptr, str.length); + } + + /// Ditto. + this(const(wchar)[] str, shared Allocator allocator = defaultAllocator) + nothrow @trusted @nogc + { + this(allocator); + + } + + /// Ditto. + this(const(dchar)[] str, shared Allocator allocator = defaultAllocator) + nothrow @trusted @nogc + { + this(allocator); + + } + + /// Ditto. + this(shared Allocator allocator) pure nothrow @safe @nogc + in + { + assert(allocator !is null); + } + body + { + allocator_ = allocator; + } + + ~this() nothrow @trusted @nogc + { + allocator.deallocate(data); + } + + mixin DefaultAllocator; +} |
