Files
hakorune/apps/libs/string_ext.nyash
Selfhosting Dev c8063c9e41 pyvm: split op handlers into ops_core/ops_box/ops_ctrl; add ops_flow + intrinsic; delegate vm.py without behavior change
net-plugin: modularize constants (consts.rs) and sockets (sockets.rs); remove legacy commented socket code; fix unused imports
mir: move instruction unit tests to tests/mir_instruction_unit.rs (file lean-up); no semantic changes
runner/pyvm: ensure using pre-strip; misc docs updates

Build: cargo build ok; legacy cfg warnings remain as before
2025-09-21 08:53:00 +09:00

47 lines
1.3 KiB
Plaintext

// StringExtBox: small helpers that make scripts concise
static box StringExtBox {
trim(s) {
// naive: trim spaces and newlines at both ends
local i = 0
local j = s.length()
loop (i < j) {
local ch = s.substring(i, i+1)
if ch == " " || ch == "\n" || ch == "\r" || ch == "\t" { i = i + 1 } else { break }
}
loop (j > i) {
local ch2 = s.substring(j-1, j)
if ch2 == " " || ch2 == "\n" || ch2 == "\r" || ch2 == "\t" { j = j - 1 } else { break }
}
return s.substring(i, j)
}
startsWith(s, pref) {
return s.substring(0, pref.length()) == pref
}
endsWith(s, suf) {
local n = s.length()
local m = suf.length()
if m > n { return false }
return s.substring(n-m, n) == suf
}
replace(s, old, neu) {
// naive single replacement (first occurrence)
local i = s.indexOf(old)
if i < 0 { return s }
return s.substring(0, i) + neu + s.substring(i + old.length(), s.length())
}
split(s, delim) {
// minimal split (no regex). returns ArrayBox of parts
local out = []
local pos = 0
local dlen = delim.length()
loop (true) {
local idx = s.indexOf(delim)
if idx < 0 { out.push(s) break }
out.push(s.substring(0, idx))
s = s.substring(idx + dlen, s.length())
}
return out
}
}