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

Commit 1705cc1

Browse files
Merge pull request #2449 from TurkeyMan/string_view_tidy
Tidied up core.stdcpp.string_view a bit.
2 parents c7fe109 + d7837a5 commit 1705cc1

File tree

1 file changed

+32
-51
lines changed

1 file changed

+32
-51
lines changed

src/core/stdcpp/string_view.d

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ else
3434
}
3535

3636
extern(C++, (StdNamespace)):
37+
@nogc:
3738

3839
///
3940
alias string_view = basic_string_view!char;
40-
//alias u16string_view = basic_string_view!wchar; // TODO: can't mangle these yet either...
41-
//alias u32string_view = basic_string_view!dchar;
42-
//alias wstring_view = basic_string_view!wchar_t; // TODO: we can't mangle wchar_t properly (yet?)
41+
///
42+
alias u16string_view = basic_string_view!wchar;
43+
///
44+
alias u32string_view = basic_string_view!dchar;
45+
///
46+
alias wstring_view = basic_string_view!wchar_t;
4347

4448

4549
/**
@@ -58,6 +62,7 @@ extern(C++, class) struct basic_string_view(T, Traits = char_traits!T)
5862
{
5963
extern(D):
6064
pragma(inline, true):
65+
pure nothrow @nogc:
6166

6267
///
6368
enum size_type npos = size_type.max;
@@ -76,75 +81,51 @@ pragma(inline, true):
7681
///
7782
alias as_array this;
7883

84+
///
85+
this(const(T)[] str) @trusted { __data = str.ptr; __size = str.length; }
86+
7987
///
8088
alias length = size;
8189
///
8290
alias opDollar = length;
8391
///
84-
bool empty() const nothrow @safe @nogc { return size() == 0; }
92+
size_type size() const @safe { return __size; }
93+
///
94+
bool empty() const @safe { return __size == 0; }
8595

8696
///
87-
ref const(T) front() const nothrow @safe @nogc { return this[0]; }
97+
const(T)* data() const @safe { return __data; }
8898
///
89-
ref const(T) back() const nothrow @safe @nogc { return this[$-1]; }
99+
const(T)[] as_array() const @trusted { return __data[0 .. __size]; }
90100

101+
///
102+
ref const(T) at(size_type i) const @trusted { return __data[0 .. __size][i]; }
103+
104+
///
105+
ref const(T) front() const @safe { return this[0]; }
106+
///
107+
ref const(T) back() const @safe { return this[$-1]; }
108+
109+
private:
110+
// use the proper field names from C++ so debugging doesn't get weird
91111
version (CppRuntime_Windows)
92112
{
93-
///
94-
this(const(T)[] str) nothrow @trusted @nogc { _Mydata = str.ptr; _Mysize = str.length; }
95-
96-
///
97-
size_type size() const nothrow @safe @nogc { return _Mysize; }
98-
///
99-
const(T)* data() const nothrow @safe @nogc { return _Mydata; }
100-
///
101-
const(T)[] as_array() const inout @trusted @nogc { return _Mydata[0 .. _Mysize]; }
102-
///
103-
ref const(T) at(size_type i) const nothrow @trusted @nogc { return _Mydata[0 .. _Mysize][i]; }
104-
105-
version (CppRuntime_Microsoft)
106-
{
107-
import core.stdcpp.xutility : MSVCLinkDirectives;
108-
mixin MSVCLinkDirectives!false;
109-
}
110-
111-
private:
112113
const_pointer _Mydata;
113114
size_type _Mysize;
115+
116+
alias __data = _Mydata;
117+
alias __size = _Mysize;
114118
}
115119
else version (CppRuntime_Gcc)
116120
{
117-
///
118-
this(const(T)[] str) nothrow @trusted @nogc { _M_str = str.ptr; _M_len = str.length; }
119-
120-
///
121-
size_type size() const nothrow @safe @nogc { return _M_len; }
122-
///
123-
const(T)* data() const nothrow @safe @nogc { return _M_str; }
124-
///
125-
const(T)[] as_array() const nothrow @trusted @nogc { return _M_str[0 .. _M_len]; }
126-
///
127-
ref const(T) at(size_type i) const nothrow @trusted @nogc { return _M_str[0 .. _M_len][i]; }
128-
129-
private:
130121
size_t _M_len;
131122
const(T)* _M_str;
123+
124+
alias __data = _M_str;
125+
alias __size = _M_len;
132126
}
133127
else version (CppRuntime_Clang)
134128
{
135-
///
136-
this(const(T)[] str) nothrow @trusted @nogc { __data = str.ptr; __size = str.length; }
137-
138-
///
139-
size_type size() const nothrow @safe @nogc { return __size; }
140-
///
141-
const(T)* data() const nothrow @safe @nogc { return __data; }
142-
///
143-
const(T)[] as_array() const nothrow @trusted @nogc { return __data[0 .. __size]; }
144-
///
145-
ref const(T) at(size_type i) const nothrow @trusted @nogc { return __data[0 .. __size][i]; }
146-
147-
private:
148129
const value_type* __data;
149130
size_type __size;
150131
}

0 commit comments

Comments
 (0)