ludic/packages/ludic.ui/lib_util.ludic

53 lines
1.8 KiB
Text

# lib_util.ludic - a file made, found, read, and a path beside another (or from the project's
# root, when it starts with /)
function lib_find(names: []string, name: string) -> int {
for i in 0 .. len(names) {
if names[i] == name { return i }
}
return -1
}
function lib_new(ui_st: mut UiState, path: string) -> UiFile {
let f = lib_blank(path)
push(ui_st.lib_files, f)
return f
}
# a file that is not one of the loaded ones (a component's own)
function lib_blank(path: string) -> UiFile {
let f = new UiFile
f.path = path
f.names = new []string
f.comps = new []UiTpl
f.exported = new []bool
f.imp_as = new []string
f.imp = new []UiFile
f.own = new []UiRule
return f
}
function lib_text(ui_st: UiState, path: string) -> string {
let o = ov_find(ui_st, path)
if o >= 0 { return `{ui_st.ov_text[o]}` } # a copy: a caller may give back what it read (rl_let_go)
var src = ""
if ui_st.ui_be != null and ui_st.ui_be.read != null { src = ui_st.ui_be.read(path) } else { src = Fs.read_text(path) }
if src == null { return "" }
return src
}
# `src` read from beside the file that imports it
function lib_beside(path: string, src: string) -> string {
var cut = -1
for i in 0 .. len(path) {
if path[i] == 47 { cut = i }
}
if len(src) > 0 and src[0] == 47 { return src[1..len(src)] }
if cut < 0 { return src }
return path[0..cut + 1] + src
}
# `s` has `p` starting at `at`: compared in place, where s[at..at + len(p)] == p made a new string
# for every place it looked (Ludic frees nothing, and the cascade asks this per element per frame)
function sat(s: string, at: int, p: string) -> bool {
let n = len(p)
if at < 0 or at + n > len(s) { return false }
for i in 0 .. n {
if s[at + i] != p[i] { return false }
}
return true
}