forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.nula
More file actions
265 lines (237 loc) · 8.45 KB
/
Copy pathstring.nula
File metadata and controls
265 lines (237 loc) · 8.45 KB
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
// Nulang standard library — String operations.
// Language version: 2.0.0-alpha
// Import: `import stdlib::string`
//
// Built on primitives: String.length, String.charAt (→ Int code),
// String.substring(s, start, length), String.concat(s1, s2).
//
// NOTE: `a + b` where both operands are non-literal strings returns 0
// (VM bug). Use `perform String.concat(a, b)` for variable-to-variable
// concatenation.
/// Length of the string in bytes.
pub fn length(s) { perform String.length(s) }
/// True when the string is empty.
pub fn is_empty(s) { perform String.length(s) == 0 }
/// Character at index i, returned as a 1-character string.
/// Returns "" if i is out of bounds.
pub fn char_at(s, i) {
let len = perform String.length(s);
if i >= 0 && i < len then { return perform String.substring(s, i, 1) } else { return "" }
}
/// Character code at index i, or -1 if out of bounds.
pub fn char_code_at(s, i) {
let len = perform String.length(s);
if i >= 0 && i < len then { return perform String.charAt(s, i) } else { return -1 }
}
// ── Substring search ──────────────────────────────────────────────────────
/// True when s starts with prefix.
pub fn starts_with(s, prefix) {
let plen = perform String.length(prefix);
if plen > perform String.length(s) then { return false } else {};
perform String.substring(s, 0, plen) == prefix
}
/// True when s ends with suffix.
pub fn ends_with(s, suffix) {
let sulen = perform String.length(suffix);
let slen = perform String.length(s);
if sulen > slen then { return false } else {};
perform String.substring(s, slen - sulen, sulen) == suffix
}
/// True when s contains the substring needle.
pub fn contains(s, needle) {
let slen = perform String.length(s);
let nlen = perform String.length(needle);
if nlen == 0 then { return true } else {};
if nlen > slen then { return false } else {};
var i = 0;
while i + nlen <= slen {
if perform String.substring(s, i, nlen) == needle then { return true } else {};
i = i + 1
};
false
}
/// First index of substring ch in s, or -1 if not found.
pub fn index_of(s, ch) {
let slen = perform String.length(s);
let chlen = perform String.length(ch);
if chlen == 0 then { return 0 } else {};
var i = 0;
while i + chlen <= slen {
var found = true;
var j = 0;
while j < chlen {
if perform String.charAt(s, i + j) != perform String.charAt(ch, j) then { found = false } else {};
j = j + 1
};
if found then { return i } else {};
i = i + 1
};
-1
}
// ── Building and repeating ────────────────────────────────────────────────
/// Repeat string s n times.
pub fn repeat(s, n) {
var result = "";
var i = 0;
while i < n {
result = perform String.concat(result, s);
i = i + 1
};
result
}
/// Reverse the string.
pub fn reverse(s: String) -> String {
var result = "";
var i = perform String.length(s) - 1;
while i >= 0 {
let code = perform String.charAt(s, i);
result = perform String.concat(result, perform String.from_char(code));
i = i - 1
};
result
}
// ── Whitespace trimming ───────────────────────────────────────────────────
/// Remove leading and trailing whitespace (spaces, tabs, newlines, CR).
pub fn trim(s) {
let len = perform String.length(s);
// Recursive helper to find first non-whitespace index.
let rec find_start = fn(i) {
if i >= len then i
else {
let c = perform String.charAt(s, i);
if c == 32 || c == 9 || c == 10 || c == 13 then find_start(i + 1)
else i
}
};
// Recursive helper to find one-past-last non-whitespace index.
let rec find_end = fn(i) {
if i <= 0 then 0
else {
let c = perform String.charAt(s, i - 1);
if c == 32 || c == 9 || c == 10 || c == 13 then find_end(i - 1)
else i
}
};
let start_idx = find_start(0);
let end_idx = find_end(len);
// Build trimmed string.
var result = "";
var i = start_idx;
while i < end_idx {
result = perform String.concat(result, perform String.substring(s, i, 1));
i = i + 1
};
result
}
// ── Split and join ────────────────────────────────────────────────────────
/// Split string s by a delimiter character (a 1-char string).
/// Returns an array of substrings.
pub fn split(s, delim) {
var result = [];
var current = "";
var i = 0;
let len = perform String.length(s);
let dcode = perform String.charAt(delim, 0);
while i < len {
let c = perform String.charAt(s, i);
if c == dcode then {
result = perform Array.push(result, current);
current = ""
} else {
current = perform String.concat(current, perform String.substring(s, i, 1))
};
i = i + 1
};
perform Array.push(result, current)
}
/// Join an array of strings with a separator string.
pub fn join(parts, sep) {
var result = "";
var i = 0;
let len = perform Array.length(parts);
while i < len {
if i > 0 then { result = perform String.concat(result, sep) } else {};
result = perform String.concat(result, parts[i]);
i = i + 1
};
result
}
// ── Replace ───────────────────────────────────────────────────────────────
/// Replace all occurrences of substring `from` with `to` in s.
pub fn replace(s, from, to) {
var result = "";
var i = 0;
let slen = perform String.length(s);
let flen = perform String.length(from);
while i < slen {
var found_match = flen > 0 && i + flen <= slen;
var j = 0;
while j < flen {
if perform String.charAt(s, i + j) != perform String.charAt(from, j) then { found_match = false } else {};
j = j + 1
};
if found_match then {
result = perform String.concat(result, to);
i = i + flen
} else {
result = perform String.concat(result, perform String.substring(s, i, 1));
i = i + 1
}
};
result
}
// ── Case conversion ───────────────────────────────────────────────────────
/// Convert string to lowercase (ASCII only, non-ASCII passthrough).
pub fn to_lower(s: String) -> String {
var result = "";
var i = 0;
let len = perform String.length(s);
while i < len {
let c = perform String.charAt(s, i);
if c >= 65 && c <= 90 then {
result = perform String.concat(result, perform String.from_char(c + 32))
} else {
result = perform String.concat(result, perform String.from_char(c))
};
i = i + 1
};
result
}
/// Convert string to uppercase (ASCII only, non-ASCII passthrough).
pub fn to_upper(s: String) -> String {
var result = "";
var i = 0;
let len = perform String.length(s);
while i < len {
let c = perform String.charAt(s, i);
if c >= 97 && c <= 122 then {
result = perform String.concat(result, perform String.from_char(c - 32))
} else {
result = perform String.concat(result, perform String.from_char(c))
};
i = i + 1
};
result
}
/// True when no alphabetic character in the string is lowercase.
pub fn is_upper(s: String) -> Bool {
var i = 0;
let len = perform String.length(s);
while i < len {
let c = perform String.charAt(s, i);
if c >= 97 && c <= 122 then { return false } else {};
i = i + 1
};
true
}
/// True when no alphabetic character in the string is uppercase.
pub fn is_lower(s: String) -> Bool {
var i = 0;
let len = perform String.length(s);
while i < len {
let c = perform String.charAt(s, i);
if c >= 65 && c <= 90 then { return false } else {};
i = i + 1
};
true
}