Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit d835e18

Browse files
authored
Merge pull request #2712 from TurkeyMan/std_string_improvements
std::string improvements merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents 6dd113d + 2f6a39f commit d835e18

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

changelog/std_string.dd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
Added `core.stdcpp.string`.
22

33
Added `core.stdcpp.string`, which links against C++ `std::string`
4+
5+
Currently only supported and tested for the Visual Studio C++ runtime library. Other runtimes coming soon(tm)!

src/core/stdcpp/string.d

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ extern(D):
8585
///
8686
alias const_pointer = const(value_type)*;
8787

88-
///
89-
alias as_array this;
9088
///
9189
alias toString = as_array;
9290

@@ -100,6 +98,16 @@ extern(D):
10098
///
10199
bool empty() const nothrow @safe { return size() == 0; }
102100

101+
///
102+
size_t[2] opSlice(size_t dim : 0)(size_t start, size_t end) const pure nothrow @safe @nogc { return [start, end]; }
103+
104+
///
105+
ref inout(T) opIndex(size_t index) inout pure nothrow @safe @nogc { return as_array[index]; }
106+
///
107+
inout(T)[] opIndex(size_t[2] slice) inout pure nothrow @safe @nogc { return as_array[slice[0] .. slice[1]]; }
108+
///
109+
inout(T)[] opIndex() inout pure nothrow @safe @nogc { return as_array(); }
110+
103111
///
104112
void clear() { eos(0); } // TODO: bounds-check
105113
///
@@ -123,12 +131,36 @@ extern(D):
123131
const(T)* c_str() const nothrow @safe { return data(); }
124132

125133
// Modifiers
134+
///
135+
ref basic_string opAssign()(auto ref basic_string str) { return assign(str.as_array); }
126136
// ref basic_string assign(size_type n, T c);
127137
///
128138
ref basic_string opAssign(const(T)[] str) { return assign(str); }
129139
///
130140
ref basic_string opAssign(T c) { return assign((&c)[0 .. 1]); }
131141

142+
///
143+
ref basic_string opIndexAssign(T c, size_t index) { as_array[index] = c; return this; }
144+
///
145+
ref basic_string opIndexAssign(T c, size_t[2] slice) { as_array[slice[0] .. slice[1]] = c; return this; }
146+
///
147+
ref basic_string opIndexAssign(const(T)[] str, size_t[2] slice) { as_array[slice[0] .. slice[1]] = str[]; return this; }
148+
///
149+
ref basic_string opIndexAssign(T c) { as_array[] = c; return this; }
150+
///
151+
ref basic_string opIndexAssign(const(T)[] str) { as_array[] = str[]; return this; }
152+
153+
///
154+
ref basic_string opIndexOpAssign(string op)(T c, size_t index) { mixin("as_array[index] " ~ op ~ "= c;"); return this; }
155+
///
156+
ref basic_string opIndexOpAssign(string op)(T c, size_t[2] slice) { mixin("as_array[slice[0] .. slice[1]] " ~ op ~ "= c;"); return this; }
157+
///
158+
ref basic_string opIndexOpAssign(string op)(const(T)[] str, size_t[2] slice) { mixin("as_array[slice[0] .. slice[1]] " ~ op ~ "= str[];"); return this; }
159+
///
160+
ref basic_string opIndexOpAssign(string op)(T c) { mixin("as_array[] " ~ op ~ "= c;"); return this; }
161+
///
162+
ref basic_string opIndexOpAssign(string op)(const(T)[] str) { mixin("as_array[] " ~ op ~ "= str[];"); return this; }
163+
132164
// ref basic_string append(size_type n, T c);
133165
///
134166
ref basic_string append(T c) { return append((&c)[0 .. 1]); }

test/stdcpp/src/string_test.d

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,36 @@ unittest
1818
assert(sumOfElements_val(str) == 1500);
1919
assert(sumOfElements_ref(str) == 500);
2020

21+
str = "Hello again with a long long string woo";
22+
assert(sumOfElements_val(str) == 10935);
23+
assert(sumOfElements_ref(str) == 3645);
24+
2125
std_string str2 = std_string(Default);
2226
assert(str2.size == 0);
2327
assert(str2.length == 0);
2428
assert(str2.empty == true);
2529
assert(str2[] == []);
2630

31+
str2 = std_string("World");
32+
assert(str2[] == "World");
33+
str = str2;
34+
assert(str[] == "World");
35+
str2 = "Direct";
36+
assert(str2[] == "Direct");
37+
assert(str2[2] == 'r');
38+
assert(str2[2 .. 5] == "rec");
39+
str2[] = "Plonk!";
40+
assert(str2[] == "Plonk!");
41+
str2[2] = 'a';
42+
str2[3 .. 5] = "ne";
43+
assert(str2[] == "Plane!");
44+
str2[] = 'a';
45+
str2[1 .. 5] = 'b';
46+
str2[] += 1;
47+
str2[1] += 1;
48+
str2[2 .. 4] += 2;
49+
assert(str2[] == "bdeecb");
50+
2751
// test local instantiations...
2852
// there's no basic_string<char16_t> instantiation in C++
2953
std_wstring str3 = std_wstring("Hello"w);
@@ -48,7 +72,7 @@ int sumOfElements_ref(ref const(std_string));
4872
// test the ABI for calls from C++
4973
int fromC_val(std_string str)
5074
{
51-
assert(str[] == "Hello");
75+
assert(str[0 .. 5] == "Hello");
5276
assert(str.front == 'H');
5377
assert(str.back == 'o');
5478
assert(str.at(2) == 'l');
@@ -58,8 +82,6 @@ int fromC_val(std_string str)
5882
int r;
5983
foreach (e; str[])
6084
r += e;
61-
62-
assert(r == 500);
6385
return r;
6486
}
6587

0 commit comments

Comments
 (0)