Debian Codegraph @

path/ext_stack.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use bytes;
5 use strings;
6
7 // Add extensions onto the end of the final path segment. The separating '.'
8 // will be inserted for you. If the final path segment consists entirely of dots
9 // or the path is root, this function will return [[cant_extend]].
10 export fn push_ext(buf: *buffer, exts: str...) (str | error) = {
11 match (peek(buf)) {
12 case void => return cant_extend;
13 case let s: str => if (strings::ltrim(s, '.') == "") return cant_extend;
14 };
15 for (let ext .. exts) {
16 const newend = buf.end + 1 + len(ext);
17 if (MAX < newend) return too_long;
18 buf.buf[buf.end] = '.';
19 buf.buf[buf.end+1..newend] = strings::toutf8(ext);
20 buf.end = newend;
21 };
22 return string(buf);
23 };
24
25 // Remove and return the final extension in a path. The result will not
26 // include the leading '.'. The result is borrowed from the buffer. Leading dots
27 // will be ignored when looking for extensions, such that ".ssh" isn't
28 // considered to have any extensions.
29 export fn pop_ext(buf: *buffer) (str | void) = {
30 const ext = split_ext(buf);
31 buf.end = ext.0;
32 return ext.1;
33 };
34
35 // Examine the final extension in a path. The result will not
36 // include the leading '.'. The result is borrowed from the buffer. Leading dots
37 // will be ignored when looking for extensions, such that ".ssh" isn't
38 // considered to have any extensions.
39 export fn peek_ext(buf: *buffer) (str | void) = split_ext(buf).1;
40
41 // helper function, returns (end of non-extension, extension string)
42 fn split_ext(buf: *buffer) (size, (str | void)) = {
43 match (peek(buf)) {
44 case void =>
45 return (buf.end, void);
46 case let s: str =>
47 const bs = strings::toutf8(s);
48 bs = bytes::ltrim(bs, '.');
49 match (bytes::rindex(bs, '.')) {
50 case void =>
51 return (buf.end, void);
52 case let i: size =>
53 return (buf.end - len(bs) + i, strings::fromutf8_unsafe(bs[i+1..]));
54 };
55 };
56 };
57
58 // Remove and return all the extensions in a path. The result will not
59 // include the leading '.', but will include separating dots. Leading dots
60 // will be ignored when looking for extensions, such that ".ssh" isn't
61 // considered to have any extensions. The result is borrowed from the buffer.
62 export fn pop_exts(buf: *buffer) (str | void) = {
63 const ext = split_exts(buf);
64 buf.end = ext.0;
65 return ext.1;
66 };
67
68 // Examine all the extensions in a path. The result will not include the
69 // leading '.', but will include separating dots. Leading dots will
70 // be ignored when looking for extensions, such that ".ssh" isn't considered
71 // to have any extensions. The result is borrowed from the buffer.
72 export fn peek_exts(buf: *buffer) (str | void) = split_exts(buf).1;
73
74 // helper function, returns (end of non-extension, extension string)
75 fn split_exts(buf: *buffer) (size, (str | void)) = {
76 match (peek(buf)) {
77 case void =>
78 return (buf.end, void);
79 case let s: str =>
80 const bs = strings::toutf8(s);
81 bs = bytes::ltrim(bs, '.');
82 match (bytes::index(bs, '.')) {
83 case void =>
84 return (buf.end, void);
85 case let i: size =>
86 return (buf.end - len(bs) + i, strings::fromutf8_unsafe(bs[i+1..]));
87 };
88 };
89 };
90
91 @test fn ext() void = {
92 // push_ext
93 let buf = init()!;
94 assert(push_ext(&buf, "bash") is cant_extend);
95 set(&buf, sepstr)!;
96 assert(push_ext(&buf, "bash") is cant_extend);
97 set(&buf, "....")!;
98 assert(push_ext(&buf, "bash") is cant_extend);
99 set(&buf, "bashrc")!;
100 assert(push_ext(&buf, "bash") as str == "bashrc.bash");
101 set(&buf, ".bashrc")!;
102 assert(push_ext(&buf, "bash") as str == ".bashrc.bash");
103
104 // pop_ext
105 set(&buf)!;
106 assert(pop_ext(&buf) is void);
107 set(&buf, "..")!;
108 assert(pop_ext(&buf) is void);
109 set(&buf, sepstr)!;
110 assert(pop_ext(&buf) is void);
111
112 set(&buf, "index.html.tmpl")!;
113 assert(pop_ext(&buf) as str == "tmpl");
114 assert(string(&buf) == "index.html");
115 assert(pop_ext(&buf) as str == "html");
116 assert(string(&buf) == "index");
117 assert(pop_ext(&buf) is void);
118 assert(string(&buf) == "index");
119
120 set(&buf, ".secret.tar.gz")!;
121 assert(pop_ext(&buf) as str == "gz");
122 assert(string(&buf) == ".secret.tar");
123 assert(pop_ext(&buf) as str == "tar");
124 assert(string(&buf) == ".secret");
125 assert(pop_ext(&buf) is void);
126 assert(string(&buf) == ".secret");
127
128 set(&buf, "..ext")!;
129 assert(pop_ext(&buf) is void);
130 assert(string(&buf) == "..ext");
131
132 // pop_exts
133 set(&buf, "index.html.tmpl")!;
134 assert(pop_exts(&buf) as str == "html.tmpl");
135 assert(string(&buf) == "index");
136 assert(pop_exts(&buf) is void);
137 assert(string(&buf) == "index");
138
139 set(&buf, ".secret.tar.gz")!;
140 assert(pop_exts(&buf) as str == "tar.gz");
141 assert(string(&buf) == ".secret");
142 assert(pop_ext(&buf) is void);
143 assert(string(&buf) == ".secret");
144 };