Debian Codegraph @

strings/sub.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use encoding::utf8;
5
6 // Passed to [[strings::sub]] to make the returned substring span to the end of
7 // the original string.
8 export type end = void;
9
10 fn utf8_byte_len_bounded(iter: *iterator, end: size) size = {
11 for (let i = 0z; i < end; i += 1) {
12 match (next(iter)) {
13 case let r: rune =>
14 continue;
15 case done =>
16 abort("index exceeds string length");
17 };
18 };
19 return iter.dec.offs;
20 };
21
22 // Returns a substring in the range [start, end - 1], where each argument is the
23 // index of the Nth rune. If the end argument is given as [[end]], the end of
24 // the substring is the end of the original string. The lifetime of the
25 // substring is the same as that of the original string.
26 //
27 // Note that substringing rune-wise is not always the correct thing to do, and
28 // it may cause unexpected linguistic errors to arise. You may want to use a
29 // third-party Unicode module instead.
30 export fn sub(s: str, start: size, end: (size | end) = end) str = {
31 let iter = iter(s);
32 let starti = utf8_byte_len_bounded(&iter, start);
33 let endi = match (end) {
34 case let sz: size =>
35 assert(start <= sz, "start is higher than end");
36 yield utf8_byte_len_bounded(&iter, sz - start);
37 case =>
38 yield len(s);
39 };
40 let bytes = toutf8(s);
41 return fromutf8_unsafe(bytes[starti..endi]);
42 };
43
44 @test fn sub() void = {
45 assert(sub("a string", 2) == "string");
46 assert(sub("a string", 2, end) == "string");
47 assert(sub("a string", 0, 1) == "a");
48 assert(sub("a string", 0, 3) == "a s");
49 assert(sub("a string", 2, 8) == "string");
50 assert(sub("a string", 4, 4) == "");
51 assert(sub("こんにちは", 1, 3) == "んに");
52 };
53
54 // Returns a substring in the range [start, end - 1], where each argument is the
55 // index of the rune at the byte-wise index N. [[encoding::utf8::invalid]] is
56 // returned if it would cut through the middle of a codepoint. An end argment of
57 // [[end]] is the same as len(s). The lifetime of the substring is the same as
58 // that of the original string.
59 export fn bytesub(
60 s: str,
61 start: size,
62 end: (size | end) = end,
63 ) (str | utf8::invalid) = {
64 const end = match (end) {
65 case let z: size =>
66 yield z;
67 case =>
68 yield len(s);
69 };
70
71 const s = toutf8(s);
72 if ((start < len(s) && s[start] & 0xc0 == 0x80)
73 || (end < len(s) && s[end] & 0xc0 == 0x80)) {
74 return utf8::invalid;
75 };
76 return fromutf8_unsafe(s[start..end]);
77 };
78
79 @test fn bytesub() void = {
80 assert(bytesub("a string", 2)! == "string");
81 assert(bytesub("a string", 2, end)! == "string");
82 assert(bytesub("a string", 0, 1)! == "a");
83 assert(bytesub("a string", 0, 3)! == "a s");
84 assert(bytesub("a string", 2, 8)! == "string");
85 assert(bytesub("a string", 4, 4)! == "");
86 assert(bytesub("こんにちは", 3, 9)! == "んに");
87 assert(bytesub("こんにちは", 1, 3) is utf8::invalid);
88 };