refactor: rename DTree to DValue

This commit is contained in:
udo
2026-06-12 11:05:52 +00:00
parent 941f5aea08
commit 7066da3cde
157 changed files with 1203 additions and 1074 deletions
+97 -97
View File
@@ -110,57 +110,57 @@ String list_find(StringList items, std::function<bool (String)> f, String fallba
return(fallback);
}
StringList dtree_keys(DTree tree)
StringList dv_keys(DValue tree)
{
StringList result;
tree.each([&](const DTree& item, String key) {
tree.each([&](const DValue& item, String key) {
if(key != "")
result.push_back(key);
});
return(result);
}
DTree dtree_values(DTree tree)
DValue dv_values(DValue tree)
{
DTree result;
DValue result;
result.set_array();
tree.each([&](const DTree& item, String key) {
tree.each([&](const DValue& item, String key) {
result.push(item);
});
return(result);
}
DTree dtree_pick(DTree tree, StringList keys)
DValue dv_pick(DValue tree, StringList keys)
{
DTree result;
DValue result;
for(auto key : keys)
{
DTree* item = tree.key(key);
DValue* item = tree.key(key);
if(item)
result[key] = *item;
}
return(result);
}
DTree dtree_omit(DTree tree, StringList keys)
DValue dv_omit(DValue tree, StringList keys)
{
DTree result;
DValue result;
std::set<String> omitted(keys.begin(), keys.end());
tree.each([&](const DTree& item, String key) {
tree.each([&](const DValue& item, String key) {
if(key != "" && omitted.find(key) == omitted.end())
result[key] = item;
});
return(result);
}
DTree dtree_map(DTree tree, std::function<DTree (const DTree&, String)> f)
DValue dv_map(DValue tree, std::function<DValue (const DValue&, String)> f)
{
DTree result;
DValue result;
bool input_is_list = tree.is_list();
if(input_is_list)
result.set_array();
tree.each([&](const DTree& item, String key) {
DTree mapped = f(item, key);
tree.each([&](const DValue& item, String key) {
DValue mapped = f(item, key);
if(key != "" && !input_is_list)
result[key] = mapped;
else
@@ -169,13 +169,13 @@ DTree dtree_map(DTree tree, std::function<DTree (const DTree&, String)> f)
return(result);
}
DTree dtree_filter(DTree tree, std::function<bool (const DTree&, String)> f)
DValue dv_filter(DValue tree, std::function<bool (const DValue&, String)> f)
{
DTree result;
DValue result;
bool input_is_list = tree.is_list();
if(input_is_list)
result.set_array();
tree.each([&](const DTree& item, String key) {
tree.each([&](const DValue& item, String key) {
if(!f(item, key))
return;
if(key != "" && !input_is_list)
@@ -186,12 +186,12 @@ DTree dtree_filter(DTree tree, std::function<bool (const DTree&, String)> f)
return(result);
}
DTree dtree_group_by(DTree tree, std::function<String (const DTree&, String)> f)
DValue dv_group_by(DValue tree, std::function<String (const DValue&, String)> f)
{
DTree result;
tree.each([&](const DTree& item, String key) {
DValue result;
tree.each([&](const DValue& item, String key) {
String group = f(item, key);
DTree* group_items = result.get_or_create(group);
DValue* group_items = result.get_or_create(group);
if(!group_items->is_array())
group_items->set_array();
group_items->push(item);
@@ -423,7 +423,7 @@ size_t regex_next_utf8_offset(String subject, size_t offset)
return(offset + step);
}
void regex_add_named_captures(DTree& result, pcre2_code* code, String subject, PCRE2_SIZE* ovector, int rc)
void regex_add_named_captures(DValue& result, pcre2_code* code, String subject, PCRE2_SIZE* ovector, int rc)
{
uint32_t name_count = 0;
uint32_t entry_size = 0;
@@ -456,9 +456,9 @@ void regex_add_named_captures(DTree& result, pcre2_code* code, String subject, P
}
}
DTree regex_build_match_tree(String pattern, String flags, String subject, pcre2_code* code, pcre2_match_data* match_data, int rc)
DValue regex_build_match_tree(String pattern, String flags, String subject, pcre2_code* code, pcre2_match_data* match_data, int rc)
{
DTree result;
DValue result;
result["matched"].set_bool(rc >= 0);
result["pattern"] = pattern;
result["flags"] = regex_flags_label(flags);
@@ -473,7 +473,7 @@ DTree regex_build_match_tree(String pattern, String flags, String subject, pcre2
for(int i = 0; i < rc; i += 1)
{
DTree capture;
DValue capture;
PCRE2_SIZE start = ovector[i * 2];
PCRE2_SIZE end = ovector[i * 2 + 1];
capture["index"] = (f64)i;
@@ -520,7 +520,7 @@ bool regex_match(String pattern, String subject, String flags)
return(rc >= 0);
}
DTree regex_search(String pattern, String subject, String flags)
DValue regex_search(String pattern, String subject, String flags)
{
RegexCode regex(pattern, flags, "regex_search");
RegexMatchData match_data(regex.code);
@@ -528,11 +528,11 @@ DTree regex_search(String pattern, String subject, String flags)
return(regex_build_match_tree(pattern, flags, subject, regex.code, match_data.data, rc));
}
DTree regex_search_all(String pattern, String subject, String flags)
DValue regex_search_all(String pattern, String subject, String flags)
{
RegexCode regex(pattern, flags, "regex_search_all");
RegexMatchData match_data(regex.code);
DTree result;
DValue result;
result["matched"].set_bool(false);
result["pattern"] = pattern;
result["flags"] = regex_flags_label(flags);
@@ -544,7 +544,7 @@ DTree regex_search_all(String pattern, String subject, String flags)
if(rc == PCRE2_ERROR_NOMATCH)
break;
DTree match = regex_build_match_tree(pattern, flags, subject, regex.code, match_data.data, rc);
DValue match = regex_build_match_tree(pattern, flags, subject, regex.code, match_data.data, rc);
result["matches"].push(match);
result["matched"].set_bool(true);
@@ -827,14 +827,14 @@ StringMap array_merge(StringMap a, StringMap b)
return(result);
}
DTree array_merge(DTree a, DTree b)
DValue array_merge(DValue a, DValue b)
{
const DTree& left = a.deref();
const DTree& right = b.deref();
const DValue& left = a.deref();
const DValue& right = b.deref();
if(left.type != 'M')
return(b);
DTree result;
DValue result;
result.set(a);
if(right.type != 'M')
return(result);
@@ -844,7 +844,7 @@ DTree array_merge(DTree a, DTree b)
{
if(append_numeric_keys && array_merge_key_is_index(entry.first))
{
DTree child = entry.second;
DValue child = entry.second;
result.push(child);
}
else
@@ -1008,7 +1008,7 @@ String nibble(String& haystack, String delim)
}
}
String json_encode(DTree t, char quote_char)
String json_encode(DValue t, char quote_char)
{
String result = "";
if(t.is_array())
@@ -1036,7 +1036,7 @@ String json_encode(DTree t, char quote_char)
{
result += "{";
u32 count = 0;
t.each([&] (DTree item, String key) {
t.each([&] (DValue item, String key) {
if(count > 0)
result += ", ";
count += 1;
@@ -1138,35 +1138,35 @@ String xml_safe_name(String raw, String fallback = "item")
return(result);
}
String xml_tree_scalar_text(DTree t)
String xml_tree_scalar_text(DValue t)
{
const DTree& target = t.deref();
const DValue& target = t.deref();
if(target.type == 'B')
return(target._bool ? "true" : "false");
return(t.to_string());
}
const DTree* xml_tree_key(const DTree& t, String key)
const DValue* xml_tree_key(const DValue& t, String key)
{
return(t.deref().key(key));
}
bool xml_tree_has_element_shape(const DTree& t)
bool xml_tree_has_element_shape(const DValue& t)
{
const DTree& target = t.deref();
const DValue& target = t.deref();
return(target.type == 'M' && target.key("name"));
}
String xml_encode_element(DTree tree, String name_hint)
String xml_encode_element(DValue tree, String name_hint)
{
const DTree& target = tree.deref();
const DValue& target = tree.deref();
bool shaped = xml_tree_has_element_shape(target);
String name = xml_safe_name(name_hint, "root");
if(shaped)
{
const DTree* name_node = xml_tree_key(target, "name");
DTree name_copy = *name_node;
const DValue* name_node = xml_tree_key(target, "name");
DValue name_copy = *name_node;
name = xml_safe_name(name_copy.to_string(), name);
}
@@ -1174,12 +1174,12 @@ String xml_encode_element(DTree tree, String name_hint)
if(shaped)
{
const DTree* attrs = xml_tree_key(target, "attrs");
const DValue* attrs = xml_tree_key(target, "attrs");
if(attrs && attrs->deref().type == 'M')
{
for(const auto& entry : attrs->deref()._map)
{
DTree attr_value = entry.second;
DValue attr_value = entry.second;
result += " " + xml_safe_name(entry.first, "attr") + "=\"" + xml_escape_attr(xml_tree_scalar_text(attr_value)) + "\"";
}
}
@@ -1188,17 +1188,17 @@ String xml_encode_element(DTree tree, String name_hint)
String content;
if(shaped)
{
const DTree* text = xml_tree_key(target, "text");
const DValue* text = xml_tree_key(target, "text");
if(text)
{
DTree text_copy = *text;
DValue text_copy = *text;
content += xml_escape_text(xml_tree_scalar_text(text_copy));
}
const DTree* children = xml_tree_key(target, "children");
const DValue* children = xml_tree_key(target, "children");
if(children && children->deref().type == 'M')
{
const DTree& child_map = children->deref();
const DValue& child_map = children->deref();
if(child_map.is_list())
{
for(u32 i = 0; i < child_map._map.size(); i += 1)
@@ -1419,13 +1419,13 @@ struct XmlParser
return(xml_decode_text(src.substr(start, i - start)));
}
DTree parse_element()
DValue parse_element()
{
if(i >= src.length() || src[i] != '<')
xml_throw("expected element at byte " + std::to_string((u64)i));
i += 1;
DTree node;
DValue node;
String name = read_name();
node["name"] = name;
@@ -1494,7 +1494,7 @@ struct XmlParser
if(src[i] == '<')
{
DTree child = parse_element();
DValue child = parse_element();
node["children"].push(child);
continue;
}
@@ -1508,12 +1508,12 @@ struct XmlParser
return(node);
}
DTree parse_document()
DValue parse_document()
{
skip_misc();
if(i >= src.length())
xml_throw("empty document");
DTree result = parse_element();
DValue result = parse_element();
skip_misc();
if(i < src.length())
xml_throw("unexpected content after root element at byte " + std::to_string((u64)i));
@@ -1523,12 +1523,12 @@ struct XmlParser
}
String xml_encode(DTree t, String root_name)
String xml_encode(DValue t, String root_name)
{
return(xml_encode_element(t, root_name));
}
DTree xml_decode(String s)
DValue xml_decode(String s)
{
XmlParser parser(s);
return(parser.parse_document());
@@ -1665,9 +1665,9 @@ String yaml_quote_key(String key)
return(yaml_double_quote(key));
}
String yaml_scalar_to_string(DTree t)
String yaml_scalar_to_string(DValue t)
{
const DTree& target = t.deref();
const DValue& target = t.deref();
switch(target.type)
{
case('B'):
@@ -1693,10 +1693,10 @@ String yaml_scalar_to_string(DTree t)
}
}
String yaml_encode_scalar(DTree t, u32 indent)
String yaml_encode_scalar(DValue t, u32 indent)
{
String raw = yaml_scalar_to_string(t);
const DTree& target = t.deref();
const DValue& target = t.deref();
if(target.type == 'B' || target.type == 'F')
return(raw);
if(raw.find("\n") != String::npos)
@@ -1719,9 +1719,9 @@ String yaml_encode_scalar(DTree t, u32 indent)
return(yaml_double_quote(raw));
}
String yaml_encode_node(DTree t, u32 indent)
String yaml_encode_node(DValue t, u32 indent)
{
const DTree& target = t.deref();
const DValue& target = t.deref();
String result;
if(target.type == 'M')
{
@@ -1732,7 +1732,7 @@ String yaml_encode_node(DTree t, u32 indent)
auto it = target._map.find(std::to_string(i));
if(it == target._map.end())
continue;
const DTree& child = it->second.deref();
const DValue& child = it->second.deref();
if(child.type == 'M')
{
result += yaml_indent(indent) + "-\n";
@@ -1748,7 +1748,7 @@ String yaml_encode_node(DTree t, u32 indent)
for(const auto& entry : target._map)
{
const DTree& child = entry.second.deref();
const DValue& child = entry.second.deref();
result += yaml_indent(indent) + yaml_quote_key(entry.first) + ":";
if(child.type == 'M')
{
@@ -1929,9 +1929,9 @@ struct YamlParser
return(false);
}
DTree parse_scalar(String value)
DValue parse_scalar(String value)
{
DTree result;
DValue result;
value = trim(value);
if(value == "")
{
@@ -2019,11 +2019,11 @@ struct YamlParser
return(literal);
}
DTree parse_value_after_line(String value, u32 parent_indent)
DValue parse_value_after_line(String value, u32 parent_indent)
{
if(value == "|" || value == ">")
{
DTree result;
DValue result;
result = parse_block_scalar(parent_indent, value);
return(result);
}
@@ -2032,14 +2032,14 @@ struct YamlParser
skip_empty();
if(i < lines.size() && lines[i].indent > parent_indent)
return(parse_block(lines[i].indent));
DTree result;
DValue result;
result = "";
return(result);
}
return(parse_scalar(value));
}
void merge_map(DTree& target, DTree source)
void merge_map(DValue& target, DValue source)
{
if(source.deref().type != 'M' || source.deref().is_list())
return;
@@ -2047,9 +2047,9 @@ struct YamlParser
target[entry.first] = entry.second;
}
DTree parse_list(u32 indent)
DValue parse_list(u32 indent)
{
DTree result;
DValue result;
result.set_array();
while(i < lines.size())
{
@@ -2063,7 +2063,7 @@ struct YamlParser
u32 line_indent = lines[i].indent;
i += 1;
DTree item;
DValue item;
if(after == "")
{
skip_empty();
@@ -2093,9 +2093,9 @@ struct YamlParser
return(result);
}
DTree parse_map(u32 indent)
DValue parse_map(u32 indent)
{
DTree result;
DValue result;
result.set_type('M');
while(i < lines.size())
{
@@ -2111,7 +2111,7 @@ struct YamlParser
{
if(result.deref()._map.size() == 0)
{
DTree scalar = parse_scalar(lines[i].content);
DValue scalar = parse_scalar(lines[i].content);
i += 1;
return(scalar);
}
@@ -2124,18 +2124,18 @@ struct YamlParser
return(result);
}
DTree parse_block(u32 indent)
DValue parse_block(u32 indent)
{
skip_empty();
if(i >= lines.size())
{
DTree empty;
DValue empty;
empty.set_type('M');
return(empty);
}
if(lines[i].indent < indent)
{
DTree empty;
DValue empty;
empty.set_type('M');
return(empty);
}
@@ -2144,12 +2144,12 @@ struct YamlParser
return(parse_map(lines[i].indent));
}
DTree parse_document()
DValue parse_document()
{
skip_empty();
if(i < lines.size() && lines[i].content == "---")
i += 1;
DTree result = parse_block(i < lines.size() ? lines[i].indent : 0);
DValue result = parse_block(i < lines.size() ? lines[i].indent : 0);
skip_empty();
if(i < lines.size() && lines[i].content == "...")
i += 1;
@@ -2162,12 +2162,12 @@ struct YamlParser
}
String yaml_encode(DTree t)
String yaml_encode(DValue t)
{
return(yaml_encode_node(t, 0));
}
DTree yaml_decode(String s)
DValue yaml_decode(String s)
{
YamlParser parser(s);
return(parser.parse_document());
@@ -2228,8 +2228,8 @@ String json_decode_String(String s, u32& i, char termination_char)
return(result);
}
DTree json_decode_map(String s, u32& i);
DTree json_decode_array(String s, u32& i);
DValue json_decode_map(String s, u32& i);
DValue json_decode_array(String s, u32& i);
void json_consume_space(String s, u32& i)
{
@@ -2277,9 +2277,9 @@ String json_decode_number(String s, u32& i)
return(result);
}
DTree json_decode_value(String s, u32& i)
DValue json_decode_value(String s, u32& i)
{
DTree result;
DValue result;
String value = "";
json_consume_space(s, i);
char c = s[i];
@@ -2322,9 +2322,9 @@ DTree json_decode_value(String s, u32& i)
return(result);
}
DTree json_decode_map(String s, u32& i)
DValue json_decode_map(String s, u32& i)
{
DTree result;
DValue result;
result.type = 'M';
String key = "";
json_consume_space(s, i);
@@ -2349,7 +2349,7 @@ DTree json_decode_map(String s, u32& i)
if(s[i] != ':')
return(result); // malformed
i += 1;
DTree v = json_decode_value(s, i);
DValue v = json_decode_value(s, i);
//result._map[key] = json_decode_value(s, i);
//print("KV " + key + " = " + to_String(v) + "\n");
//printf("map add %s (%c) \n", key.c_str(), s[i]);
@@ -2365,9 +2365,9 @@ DTree json_decode_map(String s, u32& i)
return(result);
}
DTree json_decode_array(String s, u32& i)
DValue json_decode_array(String s, u32& i)
{
DTree result;
DValue result;
result.set_array();
json_consume_space(s, i);
while(i < s.length())
@@ -2384,7 +2384,7 @@ DTree json_decode_array(String s, u32& i)
}
else
{
DTree v = json_decode_value(s, i);
DValue v = json_decode_value(s, i);
result.push(v);
}
json_consume_space(s, i);
@@ -2392,7 +2392,7 @@ DTree json_decode_array(String s, u32& i)
return(result);
}
DTree json_decode(String s)
DValue json_decode(String s)
{
u32 i = 0;
return(json_decode_value(s, i));