2025-10-31 20:45:46 +09:00
|
|
|
// string_ops.hako — Common string utility functions
|
|
|
|
|
// Responsibility: Basic string operations used across the codebase
|
|
|
|
|
// Non-goals: JSON parsing, complex text processing
|
|
|
|
|
|
|
|
|
|
static box StringOps {
|
|
|
|
|
// Find substring starting from position
|
|
|
|
|
// Returns: index of first occurrence at/after pos, or -1 if not found
|
|
|
|
|
index_of_from(text, needle, pos) {
|
|
|
|
|
if text == null { return -1 }
|
|
|
|
|
if pos < 0 { pos = 0 }
|
2025-11-01 13:28:56 +09:00
|
|
|
local n = text.length()
|
2025-10-31 20:45:46 +09:00
|
|
|
if pos >= n { return -1 }
|
2025-11-01 13:28:56 +09:00
|
|
|
local m = needle.length()
|
2025-10-31 20:45:46 +09:00
|
|
|
if m <= 0 { return pos }
|
|
|
|
|
local i = pos
|
|
|
|
|
local limit = n - m
|
|
|
|
|
loop (i <= limit) {
|
|
|
|
|
if text.substring(i, i + m) == needle { return i }
|
|
|
|
|
i = i + 1
|
|
|
|
|
}
|
|
|
|
|
return -1
|
|
|
|
|
}
|
|
|
|
|
}
|