97 lines
2.2 KiB
Plaintext
97 lines
2.2 KiB
Plaintext
!! String manipulation functions module
|
|
|
|
import math
|
|
|
|
def string_length(s) {
|
|
return len(s);
|
|
}
|
|
|
|
def string_reverse(s) {
|
|
let result = ``;
|
|
let i = len(s);
|
|
while (i > 0) {
|
|
i = i - 1;
|
|
result = result + substring(s, i, i + 1);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
def string_startsWith(s, prefix) {
|
|
if (len(prefix) > len(s)) {
|
|
return 0;
|
|
}
|
|
return substring(s, 0, len(prefix)) == prefix ? 1 : 0;
|
|
}
|
|
|
|
def string_endsWith(s, suffix) {
|
|
if (len(suffix) > len(s)) {
|
|
return 0;
|
|
}
|
|
let start = len(s) - len(suffix);
|
|
return substring(s, start, len(s)) == suffix ? 1 : 0;
|
|
}
|
|
|
|
def string_contains(s, substr) {
|
|
if (len(substr) == 0) {
|
|
return 1;
|
|
}
|
|
if (len(substr) > len(s)) {
|
|
return 0;
|
|
}
|
|
let i = 0;
|
|
while (i <= len(s) - len(substr)) {
|
|
if (substring(s, i, i + len(substr)) == substr) {
|
|
return 1;
|
|
}
|
|
i = i + 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
def string_replace(s, old, new) {
|
|
if (len(old) == 0) {
|
|
return s;
|
|
}
|
|
let result = ``;
|
|
let i = 0;
|
|
while (i < len(s)) {
|
|
if (substring(s, i, i + len(old)) == old) {
|
|
result = result + new;
|
|
i = i + len(old);
|
|
} else {
|
|
result = result + substring(s, i, i + 1);
|
|
i = i + 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
def string_toUpperCase(s) {
|
|
return upper(s);
|
|
}
|
|
|
|
def string_toLowerCase(s) {
|
|
return lower(s);
|
|
}
|
|
|
|
def string_trim(s) {
|
|
let start = 0;
|
|
while (start < len(s) && (substring(s, start, start + 1) == ` ` || substring(s, start, start + 1) == `\t` || substring(s, start, start + 1) == `\n` || substring(s, start, start + 1) == `\r`)) {
|
|
start = start + 1;
|
|
}
|
|
let end = len(s) - 1;
|
|
while (end >= start && (substring(s, end, end + 1) == ` ` || substring(s, end, end + 1) == `\t` || substring(s, end, end + 1) == `\n` || substring(s, end, end + 1) == `\r`)) {
|
|
end = end - 1;
|
|
}
|
|
if (start > end) {
|
|
return ``;
|
|
}
|
|
return substring(s, start, end + 1);
|
|
}
|
|
|
|
def string_split(s, delimiter) {
|
|
// This is a placeholder for future implementation
|
|
// In a real implementation, we would return an array
|
|
return `split not implemented yet`;
|
|
}
|