-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstring.zig
497 lines (416 loc) · 15.5 KB
/
string.zig
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const std = @import("std");
const mem = std.mem;
const ascii = std.ascii;
const Allocator = mem.Allocator;
const Buffer = std.Buffer;
const ArrayList = std.ArrayList;
const testing = std.testing;
pub const String = struct {
buffer: Buffer,
pub fn init(allocator: *Allocator, m: []const u8) !String {
return String{ .buffer = try Buffer.init(allocator, m) };
}
pub fn deinit(self: *String) void {
self.buffer.deinit();
}
pub fn startsWith(self: *const String, m: []const u8) bool {
return self.buffer.startsWith(m);
}
pub fn endsWith(self: *const String, m: []const u8) bool {
return self.buffer.endsWith(m);
}
pub fn isEmpty(self: *const String) bool {
// Can't use Buffer.isNull because Buffer maintains a null byte at the
// end. (e.g., string of "" in a Buffer is not null)
return self.buffer.len() == 0;
}
pub fn len(self: *const String) usize {
return self.buffer.len();
}
pub fn append(self: *String, m: []const u8) !void {
try self.buffer.append(m);
}
pub fn eql(self: *const String, m: []const u8) bool {
return self.buffer.eql(m);
}
pub fn reverse(self: *String) void {
if (self.len() <= 1) {
return;
}
var i: usize = 0;
var j: usize = self.len() - 1;
while (i < j) {
var temp = self.at(i);
self.buffer.list.set(i, self.buffer.list.at(j));
self.buffer.list.set(j, temp);
i += 1;
j -= 1;
}
}
pub fn at(self: *const String, i: usize) u8 {
return self.buffer.list.at(i);
}
/// Caller owns the returned memory
fn computeLongestPrefixSuffixArray(self: *const String, allocator: *Allocator, pattern: []const u8) ![]usize {
var m = pattern.len;
var lps = ArrayList(usize).init(allocator);
defer lps.deinit();
var size: usize = 0;
while (size < m) : (size += 1) {
try lps.append(0);
}
// Left and right positions going through the pattern
var left: usize = 0;
var right: usize = 1;
while (right < m) {
if (pattern[right] == pattern[left]) {
lps.set(right, left + 1);
left += 1;
right += 1;
} else {
if (left != 0) {
left = lps.at(left - 1);
} else {
lps.set(right, 0);
right += 1;
}
}
}
return lps.toOwnedSlice();
}
/// Return an array of indices containing substring matches for a given pattern
/// Uses Knuth-Morris-Pratt Algorithm for string searching
/// https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm
/// Caller owns the returned memory
pub fn findSubstringIndices(self: *const String, allocator: *Allocator, pattern: []const u8) ![]usize {
var indices = ArrayList(usize).init(allocator);
defer indices.deinit();
if (self.isEmpty() or pattern.len < 1 or pattern.len > self.len()) {
return indices.toSlice();
}
var lps = try self.computeLongestPrefixSuffixArray(allocator, pattern);
defer allocator.free(lps);
var str_index: usize = 0;
var pat_index: usize = 0;
while (str_index < self.len() and pat_index < pattern.len) {
if (self.at(str_index) == pattern[pat_index]) {
str_index += 1;
pat_index += 1;
} else {
if (pat_index != 0) {
pat_index = lps[pat_index - 1];
} else {
str_index += 1;
}
}
if (pat_index == pattern.len) {
try indices.append(str_index - pattern.len);
pat_index = 0;
}
}
return indices.toOwnedSlice();
}
pub fn contains(self: *const String, allocator: *Allocator, pattern: []const u8) !bool {
var matches = try self.findSubstringIndices(allocator, pattern);
defer allocator.free(matches);
return matches.len > 0;
}
pub fn toSlice(self: *const String) []u8 {
return self.buffer.toSlice();
}
pub fn toSliceConst(self: *const String) []const u8 {
return self.buffer.toSliceConst();
}
pub fn trim(self: *String, trim_pattern: []const u8) !void {
var trimmed_str = mem.trim(u8, self.toSliceConst(), trim_pattern);
try self.setTrimmedStr(trimmed_str);
}
pub fn trimLeft(self: *String, trim_pattern: []const u8) !void {
const trimmed_str = mem.trimLeft(u8, self.toSliceConst(), trim_pattern);
try self.setTrimmedStr(trimmed_str);
}
pub fn trimRight(self: *String, trim_pattern: []const u8) !void {
const trimmed_str = mem.trimRight(u8, self.toSliceConst(), trim_pattern);
try self.setTrimmedStr(trimmed_str);
}
fn setTrimmedStr(self: *String, trimmed_str: []const u8) !void {
const m = trimmed_str.len;
std.debug.assert(self.len() >= m); // this should always be true
for (trimmed_str) |v, i| {
self.buffer.list.set(i, v);
}
try self.buffer.resize(m);
}
pub fn split(self: *const String, delimiter: []const u8) mem.SplitIterator {
return mem.separate(self.toSliceConst(), delimiter);
}
/// Replaces all occurrences of substring `old` replaced with `new` in place
pub fn replace(self: *String, allocator: *Allocator, old: []const u8, new: []const u8) !void {
if (self.len() < 1 or old.len < 1) {
return;
}
var matches = try self.findSubstringIndices(allocator, old);
defer allocator.free(matches);
if (matches.len < 1) {
return;
}
var new_contents = ArrayList(u8).init(allocator);
defer new_contents.deinit();
var orig_index: usize = 0;
for (matches) |match_index| {
while (orig_index < match_index) {
try new_contents.append(self.at(orig_index));
orig_index += 1;
}
orig_index = match_index + old.len;
for (new) |val| {
try new_contents.append(val);
}
}
// Append end of string if match does not end original string
while (orig_index < self.len()) {
try new_contents.append(self.at(orig_index));
orig_index += 1;
}
try self.buffer.replaceContents(new_contents.toSliceConst());
}
pub fn count(self: *const String, allocator: *Allocator, pattern: []const u8) !usize {
var matches = try self.findSubstringIndices(allocator, pattern);
return matches.len;
}
/// Only makes ASCII characters lowercase
pub fn toLower(self: *String) void {
for (self.toSlice()) |*c| {
c.* = ascii.toLower(c.*);
}
}
/// Only makes ASCII characters uppercase
pub fn toUpper(self: *String) void {
for (self.toSlice()) |*c| {
c.* = ascii.toUpper(c.*);
}
}
pub fn ptr(self: *const String) [*]u8 {
return self.buffer.ptr();
}
};
test ".startsWith" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "hello world");
defer s.deinit();
testing.expect(s.startsWith("hel"));
}
test ".endsWith" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "hello world");
defer s.deinit();
testing.expect(s.endsWith("orld"));
}
test ".isEmpty" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "");
defer s.deinit();
testing.expect(s.isEmpty());
try s.append("hello");
std.testing.expect(!s.isEmpty());
}
test ".len" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "");
defer s.deinit();
testing.expect(s.len() == 0);
try s.append("hello");
std.testing.expect(s.len() == 5);
}
test ".eql" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "hello world");
defer s.deinit();
testing.expect(s.eql("hello world"));
}
test ".reverse" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "");
defer s.deinit();
s.reverse();
testing.expect(s.eql(""));
try s.append("h");
s.reverse();
testing.expect(s.eql("h"));
try s.append("e");
s.reverse();
testing.expect(s.eql("eh"));
try s.buffer.replaceContents("hello");
s.reverse();
testing.expect(s.eql("olleh"));
}
test ".findSubstringIndices" {
var buf: [1024]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "Mississippi");
defer s.deinit();
const m1 = try s.findSubstringIndices(allocator, "i");
testing.expect(mem.eql(usize, m1, [_]usize{ 1, 4, 7, 10 }));
const m2 = try s.findSubstringIndices(allocator, "iss");
testing.expect(mem.eql(usize, m2, [_]usize{ 1, 4 }));
const m3 = try s.findSubstringIndices(allocator, "z");
testing.expect(mem.eql(usize, m3, [_]usize{}));
const m4 = try s.findSubstringIndices(allocator, "Mississippi");
testing.expect(mem.eql(usize, m4, [_]usize{0}));
var s2 = try String.init(allocator, "的中对不起我的中文不好");
defer s2.deinit();
const m5 = try s2.findSubstringIndices(allocator, "的中");
testing.expect(mem.eql(usize, m5, [_]usize{ 0, 18 }));
}
test ".contains" {
var buf: [1024]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "Mississippi");
defer s.deinit();
const m1 = try s.contains(allocator, "i");
testing.expect(m1 == true);
const m2 = try s.contains(allocator, "iss");
testing.expect(m2 == true);
const m3 = try s.contains(allocator, "z");
testing.expect(m3 == false);
const m4 = try s.contains(allocator, "Mississippi");
testing.expect(m4 == true);
}
test ".toSlice" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "hello world");
defer s.deinit();
testing.expect(mem.eql(u8, "hello world", s.toSlice()));
}
test ".toSliceConst" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "hello world");
defer s.deinit();
testing.expect(mem.eql(u8, "hello world", s.toSliceConst()));
}
test ".trim" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, " foo\n ");
defer s.deinit();
try s.trim(" \n");
testing.expectEqualSlices(u8, "foo", s.toSliceConst());
testing.expect(3 == s.len());
try s.trim(" \n");
testing.expectEqualSlices(u8, "foo", s.toSliceConst());
}
test ".trimLeft" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, " foo\n ");
defer s.deinit();
try s.trimLeft(" \n");
testing.expectEqualSlices(u8, "foo\n ", s.toSliceConst());
}
test ".trimRight" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, " foo\n ");
defer s.deinit();
try s.trimRight(" \n");
testing.expectEqualSlices(u8, " foo", s.toSliceConst());
}
test ".split" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "abc|def||ghi");
defer s.deinit();
// All of these tests are from std/mem.zig
var it = s.split("|");
testing.expect(mem.eql(u8, it.next().?, "abc"));
testing.expect(mem.eql(u8, it.next().?, "def"));
testing.expect(mem.eql(u8, it.next().?, ""));
testing.expect(mem.eql(u8, it.next().?, "ghi"));
testing.expect(it.next() == null);
try s.buffer.replaceContents("");
it = s.split("|");
testing.expect(mem.eql(u8, it.next().?, ""));
testing.expect(it.next() == null);
try s.buffer.replaceContents("|");
it = s.split("|");
testing.expect(mem.eql(u8, it.next().?, ""));
testing.expect(mem.eql(u8, it.next().?, ""));
testing.expect(it.next() == null);
}
test ".replace" {
var buf: [1024]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "Mississippi");
defer s.deinit();
try s.replace(allocator, "iss", "e");
testing.expectEqualSlices(u8, "Meeippi", s.toSliceConst());
try s.buffer.replaceContents("Mississippi");
try s.replace(allocator, "iss", "issi");
testing.expectEqualSlices(u8, "Missiissiippi", s.toSliceConst());
try s.buffer.replaceContents("Mississippi");
try s.replace(allocator, "i", "a");
testing.expectEqualSlices(u8, "Massassappa", s.toSliceConst());
try s.buffer.replaceContents("Mississippi");
try s.replace(allocator, "iss", "");
testing.expectEqualSlices(u8, "Mippi", s.toSliceConst());
try s.buffer.replaceContents("Mississippi");
try s.replace(allocator, s.toSliceConst(), "Foo");
testing.expectEqualSlices(u8, "Foo", s.toSliceConst());
}
test ".count" {
var buf: [1024]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "Mississippi");
defer s.deinit();
const c1 = try s.count(allocator, "i");
testing.expect(c1 == 4);
const c2 = try s.count(allocator, "M");
testing.expect(c2 == 1);
const c3 = try s.count(allocator, "abc");
testing.expect(c3 == 0);
const c4 = try s.count(allocator, "iss");
testing.expect(c4 == 2);
}
test ".toLower" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "ABCDEF");
defer s.deinit();
s.toLower();
testing.expectEqualSlices(u8, "abcdef", s.toSliceConst());
try s.buffer.replaceContents("的ABcdEF中");
s.toLower();
testing.expectEqualSlices(u8, "的abcdef中", s.toSliceConst());
try s.buffer.replaceContents("AB的cd中EF");
s.toLower();
testing.expectEqualSlices(u8, "ab的cd中ef", s.toSliceConst());
}
test ".toUpper" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "abcdef");
defer s.deinit();
s.toUpper();
testing.expectEqualSlices(u8, "ABCDEF", s.toSliceConst());
try s.buffer.replaceContents("的abCDef中");
s.toUpper();
testing.expectEqualSlices(u8, "的ABCDEF中", s.toSliceConst());
try s.buffer.replaceContents("ab的CD中ef");
s.toUpper();
testing.expectEqualSlices(u8, "AB的CD中EF", s.toSliceConst());
}
test ".ptr" {
var buf: [256]u8 = undefined;
const allocator = &std.heap.FixedBufferAllocator.init(&buf).allocator;
var s = try String.init(allocator, "abcdef");
defer s.deinit();
testing.expect(mem.eql(u8, mem.toSliceConst(u8, s.ptr()), s.toSliceConst()));
}