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
36 lines (33 loc) · 1.1 KB
/
Copy pathstring.nula
File metadata and controls
36 lines (33 loc) · 1.1 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
// Nulang standard library — String operations.
// Import: `import std.string`
//
// Strings are built-in: "..." syntax, s + t concatenation,
// String.length(s), String.charAt(s, idx).
/// Remove leading and trailing whitespace from a string.
pub fn trim(s: String) -> String {
// Strip leading whitespace
let mut start = 0;
let len = String.length(s);
while start < len {
let c = String.charAt(s, start);
if c == 32 || c == 9 || c == 10 || c == 13 then { start = start + 1; } else { break; };
};
// Strip trailing whitespace
let mut end = len;
while end > start {
let c = String.charAt(s, end - 1);
if c == 32 || c == 9 || c == 10 || c == 13 then { end = end - 1; } else { break; };
};
// Manual substring
let mut result = "";
let mut i = start;
while i < end {
result = result + String.charAt(s, i);
i = i + 1;
};
result
}
/// Get the length of a string in bytes.
pub fn length(s: String) -> Int { String.length(s) }
/// Check if the string is empty.
pub fn is_empty(s: String) -> Bool { String.length(s) == 0 }