intrinsics rewritten without ++, --, etc

This commit is contained in:
2026-02-10 07:19:45 -06:00
parent 3f7e34cd7a
commit c4ff0bc109
13 changed files with 545 additions and 468 deletions

35
toml.cm
View File

@@ -3,21 +3,16 @@
function toml_unescape(s) {
if (!is_text(s)) return null
// Order matters:
// "\\\"" (backslash + quote) should become "\"", not just '"'
// So: unescape \" first, then unescape \\.
s = replace(s, '\\"', '"')
s = replace(s, '\\\\', '\\')
return s
var r = replace(s, '\\"', '"')
r = replace(r, '\\\\', '\\')
return r
}
function toml_escape(s) {
if (!is_text(s)) return null
// Order matters:
// escape backslashes first, otherwise escaping quotes introduces new backslashes that would get double-escaped.
s = replace(s, '\\', '\\\\')
s = replace(s, '"', '\\"')
return s
var r = replace(s, '\\', '\\\\')
r = replace(r, '"', '\\"')
return r
}
function parse_toml(toml_text) {
@@ -123,7 +118,7 @@ function parse_key_path(str) {
current = ''
continue
}
current += c
current = current + c
}
var tail = trim(current)
@@ -137,27 +132,27 @@ function parse_array(str) {
if (!is_text(str)) return null
// Remove brackets and trim
str = text(str, 1, -1)
str = trim(str)
if (!str) return []
var s = text(str, 1, -1)
s = trim(s)
if (!s) return []
var items = []
var current = ''
var in_quotes = false
for (var i = 0; i < length(str); i++) {
var ch = str[i]
for (var i = 0; i < length(s); i++) {
var ch = s[i]
if (ch == '"' && (i == 0 || str[i - 1] != '\\')) {
if (ch == '"' && (i == 0 || s[i - 1] != '\\')) {
in_quotes = !in_quotes
current += ch
current = current + ch
} else if (ch == ',' && !in_quotes) {
var piece = trim(current)
if (piece == null) piece = trim(current)
push(items, parse_value(piece))
current = ''
} else {
current += ch
current = current + ch
}
}