// System access through the C stub, plus small string helpers. // Helpers are written against `for c in s` only, to stay independent of // String APIs that have changed between MoonBit releases. ///| suberror OrbitError { OrbitError(String) } ///| #borrow(cmd) extern "C" fn c_run(cmd : Bytes) -> Int32 = "orbit_run" ///| #borrow(cmd) extern "C" fn c_capture(cmd : Bytes) -> Bytes = "orbit_capture" ///| extern "C" fn c_last_status() -> Int32 = "orbit_last_status" ///| #borrow(path) extern "C" fn c_read_file(path : Bytes) -> Bytes = "orbit_read_file" ///| #borrow(path, data) extern "C" fn c_write_file(path : Bytes, data : Bytes) -> Int32 = "orbit_write_file" ///| #borrow(name) extern "C" fn c_getenv(name : Bytes) -> Bytes = "orbit_getenv" ///| extern "C" fn c_exit(code : Int32) = "orbit_exit" // ---------------------------------------------------------------- UTF-8 ///| fn utf8_encode(s : String) -> Bytes { let out : Array[Byte] = [] for c in s { let cp = c.to_int() if cp < 0x80 { out.push(cp.to_byte()) } else if cp < 0x800 { out.push((0xC0 | (cp >> 6)).to_byte()) out.push((0x80 | (cp & 0x3F)).to_byte()) } else if cp < 0x10000 { out.push((0xE0 | (cp >> 12)).to_byte()) out.push((0x80 | ((cp >> 6) & 0x3F)).to_byte()) out.push((0x80 | (cp & 0x3F)).to_byte()) } else { out.push((0xF0 | (cp >> 18)).to_byte()) out.push((0x80 | ((cp >> 12) & 0x3F)).to_byte()) out.push((0x80 | ((cp >> 6) & 0x3F)).to_byte()) out.push((0x80 | (cp & 0x3F)).to_byte()) } } Bytes::makei(out.length(), fn(i) { out[i] }) } ///| fn utf8_decode(b : Bytes) -> String { let sb = StringBuilder() let n = b.length() let mut i = 0 while i < n { let c0 = b[i].to_int() if c0 < 0x80 { sb.write_char(Int32::unsafe_to_char(c0)) i = i + 1 } else if c0 >= 0xC0 && c0 < 0xE0 && i + 1 < n { let cp = ((c0 & 0x1F) << 6) | (b[i + 1].to_int() & 0x3F) sb.write_char(Int32::unsafe_to_char(cp)) i = i + 2 } else if c0 >= 0xE0 && c0 < 0xF0 && i + 2 < n { let cp = ((c0 & 0x0F) << 12) | ((b[i + 1].to_int() & 0x3F) << 6) | (b[i + 2].to_int() & 0x3F) sb.write_char(Int32::unsafe_to_char(cp)) i = i + 3 } else if c0 >= 0xF0 && i + 3 < n { let cp = ((c0 & 0x07) << 18) | ((b[i + 1].to_int() & 0x3F) << 12) | ((b[i + 2].to_int() & 0x3F) << 6) | (b[i + 3].to_int() & 0x3F) sb.write_char(Int32::unsafe_to_char(cp)) i = i + 4 } else { sb.write_char('\u{FFFD}') i = i + 1 } } sb.to_string() } // ---------------------------------------------------------------- strings ///| fn chars(s : String) -> Array[Char] { let a : Array[Char] = [] for c in s { a.push(c) } a } ///| fn slice(a : Array[Char], start : Int32, end : Int32) -> String { let sb = StringBuilder() for i = start; i < end; i = i + 1 { sb.write_char(a[i]) } sb.to_string() } ///| fn drop_chars(s : String, n : Int32) -> String { let a = chars(s) if n >= a.length() { "" } else { slice(a, n, a.length()) } } ///| fn has_prefix(s : String, p : String) -> Bool { let a = chars(s) let b = chars(p) if b.length() > a.length() { return false } for i = 0; i < b.length(); i = i + 1 { if a[i] != b[i] { return false } } true } ///| fn has_char(s : String, ch : Char) -> Bool { for c in s { if c == ch { return true } } false } ///| fn contains_str(s : String, sub : String) -> Bool { let a = chars(s) let b = chars(sub) if b.length() == 0 { return true } for i = 0; i + b.length() <= a.length(); i = i + 1 { let mut ok = true for j = 0; j < b.length(); j = j + 1 { if a[i + j] != b[j] { ok = false break } } if ok { return true } } false } ///| fn split_on(s : String, sep : Char) -> Array[String] { let out : Array[String] = [] let sb = StringBuilder() let mut cur = sb for c in s { if c == sep { out.push(cur.to_string()) cur = StringBuilder() } else { cur.write_char(c) } } out.push(cur.to_string()) out } ///| /// Split at the first `sep`; the second part is "" when `sep` is absent. fn split_first(s : String, sep : Char) -> (String, String) { let a = chars(s) for i = 0; i < a.length(); i = i + 1 { if a[i] == sep { return (slice(a, 0, i), slice(a, i + 1, a.length())) } } (s, "") } ///| fn is_space(c : Char) -> Bool { c == ' ' || c == '\n' || c == '\t' || c == '\r' } ///| fn trim(s : String) -> String { let a = chars(s) let mut start = 0 let mut end = a.length() while start < end && is_space(a[start]) { start = start + 1 } while end > start && is_space(a[end - 1]) { end = end - 1 } slice(a, start, end) } ///| /// Non-empty, trimmed lines. fn lines(s : String) -> Array[String] { let out : Array[String] = [] for l in split_on(s, '\n') { let t = trim(l) if t != "" { out.push(t) } } out } ///| fn join(xs : Array[String], sep : String) -> String { let sb = StringBuilder() for i = 0; i < xs.length(); i = i + 1 { if i > 0 { sb.write_string(sep) } sb.write_string(xs[i]) } sb.to_string() } ///| fn basename(p : String) -> String { let parts = split_on(p, '/') parts[parts.length() - 1] } ///| /// Single-quote a string for /bin/sh. fn sq(s : String) -> String { let sb = StringBuilder() sb.write_char('\'') for c in s { if c == '\'' { sb.write_string("'\\''") } else { sb.write_char(c) } } sb.write_char('\'') sb.to_string() } // ---------------------------------------------------------------- process / fs ///| fn sh(cmd : String) -> Int32 { c_run(utf8_encode(cmd)) } ///| fn sh_ok(cmd : String) -> Unit raise OrbitError { let code = sh(cmd) if code != 0 { raise OrbitError("command failed (exit \{code}): \{cmd}") } } ///| /// Run a command and return its stdout; stderr goes to the terminal. fn capture(cmd : String) -> String raise OrbitError { let out = utf8_decode(c_capture(utf8_encode(cmd))) let code = c_last_status() if code != 0 { raise OrbitError("command failed (exit \{code}): \{cmd}") } out } ///| fn exists(path : String) -> Bool { sh("test -e " + sq(path)) == 0 } ///| fn read_text(path : String) -> String? { let b = c_read_file(utf8_encode(path)) if c_last_status() != 0 { None } else { Some(utf8_decode(b)) } } ///| /// Write via a temp file and rename, so readers never see a half-written file. fn write_text_atomic(path : String, text : String) -> Unit raise OrbitError { let tmp = path + ".tmp" if c_write_file(utf8_encode(tmp), utf8_encode(text)) != 0 { raise OrbitError("cannot write \{tmp}") } sh_ok("mv -f " + sq(tmp) + " " + sq(path)) } ///| fn getenv(name : String) -> String { utf8_decode(c_getenv(utf8_encode(name))) } ///| /// Command-line arguments without the program name (Linux: /proc/self/cmdline). fn cli_args() -> Array[String] { let raw = c_read_file(utf8_encode("/proc/self/cmdline")) let out : Array[String] = [] let cur : Array[Byte] = [] for i = 0; i < raw.length(); i = i + 1 { if raw[i].to_int() == 0 { out.push(utf8_decode(Bytes::makei(cur.length(), fn(k) { cur[k] }))) cur.clear() } else { cur.push(raw[i]) } } if cur.length() > 0 { out.push(utf8_decode(Bytes::makei(cur.length(), fn(k) { cur[k] }))) } let args : Array[String] = [] for i = 1; i < out.length(); i = i + 1 { args.push(out[i]) } args }