/
string.d
329 lines (279 loc) · 7.43 KB
/
string.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/// Helper traits and functions to deal with strings
module std.io.internal.string;
import std.range : ElementEncodingType, isInputRange, isInfinite;
import std.traits : isSomeChar, isSomeString;
import std.bitmanip : bitfields;
/// trait for input ranges of chars
enum isStringLike(S) = (isInputRange!S && !isInfinite!S && isSomeChar!(ElementEncodingType!S))
|| isSomeString!S;
/// @nogc string with unique ownership
struct String
{
nothrow pure @safe @nogc:
this(const char[] s) @trusted
{
ptr = s.ptr;
len = cast(uint) s.length;
}
this(S)(scope S s) if (isStringLike!S)
{
this ~= s;
}
~this() scope @trusted
{
import core.memory : pureFree;
if (ptr && cap)
pureFree(cast(void * ) ptr);
}
/// https://issues.dlang.org/show_bug.cgi?id=17927
const(char)[] opSlice() const return scope @trusted
{
return ptr[0 .. len];
}
///
@property bool empty() const
{
return !len;
}
///
void opOpAssign(string op : "~")(char c) scope
{
reserve(1);
() @trusted
{
(cast(char * ) ptr)[len++] = c;
}
();
}
///
void opOpAssign(string op : "~", S)(scope S s) scope if (isStringLike!S)
{
import std.range : hasLength;
static if (is(ElementEncodingType!S == char) && (hasLength!S || isSomeString!S))
{
immutable nlen = s.length;
reserve(nlen);
() @trusted
{
(cast(char * ) ptr)[len .. len + nlen] = s[];
}
();
len += nlen;
}
else
{
import std.utf : byUTF;
foreach (c; s.byUTF!char)
this ~= c;
}
}
///
void put(S)(scope S s) scope if (isStringLike!S)
{
this ~= s;
}
/// non-copyable, use `move` or `clone`
@disable this(this);
///
String move() scope @trusted
{
auto optr = ptr;
auto olen = len;
auto ocap = cap;
ptr = null;
len = 0;
cap = 0;
return String(optr, olen, ocap);
}
///
String clone() scope
{
return String(this[]);
}
///
bool opEquals(in ref String s) scope const
{
return this[] == s[];
}
///
int opCmp(in ref String s) const
{
return __cmp(this[], s[]);
}
private:
this(const(char)* ptr, uint len, uint cap)
{
this.ptr = ptr;
this.len = len;
this.cap = cap;
}
void reserve(size_t n) scope @trusted
{
import core.exception : onOutOfMemoryError;
import core.memory : pureMalloc, pureRealloc;
if (len + n > cap)
{
immutable ncap = (len + cast(uint) n) * 3 / 2;
if (cap)
ptr = cast(char * ) pureRealloc(cast(char * ) ptr, ncap);
else if (!len)
ptr = cast(char * ) pureMalloc(ncap);
else
{
// copy non-owned string on append
auto nptr = cast(char * ) pureMalloc(ncap);
if (nptr)
nptr[0 .. len] = ptr[0 .. len];
ptr = nptr;
}
cap = ncap;
if (ptr is null)
onOutOfMemoryError();
}
}
const(char)* ptr;
uint len, cap;
}
///
nothrow pure @safe @nogc unittest
{
auto s = String("Hello");
assert(s[] == "Hello", s[]);
s ~= " String";
assert(s[] == "Hello String", s[]);
auto s2 = s.clone;
assert(s == s2);
}
nothrow @safe @nogc unittest
{
static void escape(const char[] s) nothrow @safe @nogc
{
static const(char)[] cache;
cache = s;
}
scope s = String("Hello");
static assert(!__traits(compiles, escape(s[])));
auto s2 = String("Hello");
// https://issues.dlang.org/show_bug.cgi?id=17927 :/
// static assert(!__traits(compiles, escape(s2[])));
}
package(std.io):
// FIXME: Using the imported std.internal.cstring.tempCStringW produces
// corrupted strings for unknow reasons, copying the definition here works.
auto _tempCString(To = char, From)(From str) if (isStringLike!From)
{
import std.range : hasLength;
import std.traits : Unqual;
alias CF = Unqual!(ElementEncodingType!From);
auto res = TempCStringBuffer!To.trustedVoidInit(); // expensive to fill _buff[]
// Note: res._ptr can't point to res._buff as structs are movable.
To[] p;
bool p_is_onstack = true;
size_t i;
size_t strLength;
static if (hasLength!From)
{
strLength = str.length;
}
import std.utf : byUTF;
static if (isSomeString!From)
{
auto r = cast(const(CF)[]) str; // because inout(CF) causes problems with byUTF
if (r is null) // Bugzilla 14980
{
res._ptr = null;
return res;
}
}
else
alias r = str;
To[] q = res._buff;
foreach (const c; byUTF!(Unqual!To)(r))
{
if (i + 1 == q.length)
{
p = trustedRealloc(p, i, res._buff, strLength, p_is_onstack);
p_is_onstack = false;
q = p;
}
q[i++] = c;
}
q[i] = 0;
res._length = i;
res._ptr = p_is_onstack ? res.useStack : &p[0];
return res;
}
version (Windows) alias tempCStringW = _tempCString!(wchar, const(char)[]);
private struct TempCStringBuffer(To = char)
{
@trusted pure nothrow @nogc:
@disable this();
@disable this(this);
alias ptr this; /// implicitly covert to raw pointer
@property inout(To)* buffPtr() inout
{
return _ptr == useStack ? _buff.ptr : _ptr;
}
@property const(To)* ptr() const
{
return buffPtr;
}
const(To)[] opIndex() const pure
{
return buffPtr[0 .. _length];
}
~this()
{
if (_ptr != useStack)
{
import core.memory : pureFree;
pureFree(_ptr);
}
}
private:
enum To* useStack = () @trusted{ return cast(To*) size_t.max; }();
To* _ptr;
size_t _length; // length of the string
version (unittest)
{
enum buffLength = 16 / To.sizeof; // smaller size to trigger reallocations
}
else
{
enum buffLength = 256 / To.sizeof; // production size
}
To[buffLength] _buff; // the 'small string optimization'
static TempCStringBuffer trustedVoidInit()
{
TempCStringBuffer res = void;
return res;
}
}
private To[] trustedRealloc(To)(To[] buf, size_t i, To[] res, size_t strLength, bool res_is_onstack) @trusted @nogc pure nothrow
{
pragma(inline, false); // because it's rarely called
import core.exception : onOutOfMemoryError;
import core.memory : pureMalloc, pureRealloc;
import core.stdc.string : memcpy;
if (res_is_onstack)
{
size_t newlen = res.length * 3 / 2;
if (newlen <= strLength)
newlen = strLength + 1; // +1 for terminating 0
auto ptr = cast(To*) pureMalloc(newlen * To.sizeof);
if (!ptr)
onOutOfMemoryError();
memcpy(ptr, res.ptr, i * To.sizeof);
return ptr[0 .. newlen];
}
else
{
if (buf.length >= size_t.max / (2 * To.sizeof))
onOutOfMemoryError();
const newlen = buf.length * 3 / 2;
auto ptr = cast(To*) pureRealloc(buf.ptr, newlen * To.sizeof);
if (!ptr)
onOutOfMemoryError();
return ptr[0 .. newlen];
}
}