fix(expr): list items may hold commas, or be lists
Lists are stored as "[a,b]" and were split on every comma, so an item with a comma, or a list added to a list, came apart. NewList now writes a whole-list item as is (lists nest: [a,[b,c]]) and escapes \ , [ ] in any other item; GetList splits on top-level commas only and undoes the escapes. Plain lists keep their old text. The host test is no longer a known failure.
This commit is contained in:
parent
0daf0a44a3
commit
2ab70d87d2
3 changed files with 107 additions and 5 deletions
26
Changelog/2026-10-08-fix-expr-list-items-with-commas.md
Normal file
26
Changelog/2026-10-08-fix-expr-list-items-with-commas.md
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# Expressions - list items may hold commas, or be lists - 2026-10-08
|
||||
|
||||
## Fixed
|
||||
|
||||
- **A list item with a comma stays one item.** Lists are stored as text, `"[a,b]"`, and were
|
||||
split on every comma, so `listcreate[`a,b`, c]` held three items and a list added to a list
|
||||
(`listadd[listcreate[a], listcreate[b,c]]`) came apart into its pieces.
|
||||
- New list text rules (`ExpressionEngine.NewList` / `GetList`, used by every list function):
|
||||
- an item that is itself a whole list is written as is, so lists nest: `[a,[b,c]]`, and
|
||||
`listgetitem` returns `[b,c]`, a list again;
|
||||
- any other item has `\`, `,`, `[` and `]` escaped with `\` (`[a\,b,c]`), and reads back
|
||||
unescaped;
|
||||
- items without those characters are written exactly as before, so existing lists and
|
||||
saved variables read the same.
|
||||
|
||||
## Files
|
||||
|
||||
- `Plugins/RynthCore.Plugin.RynthAi/Meta/ExpressionEngine.cs`: `NewList` escapes or nests
|
||||
items (`AppendListItem`, `IsWholeList`); `GetList` splits on top-level commas only.
|
||||
- `Tools/RynthCore.RynthAiHostTests/Tests/ExpressionTests.cs`: the list test is a normal test
|
||||
now (was a known failure), and also checks read-back, `listcontains` / `listindexof`,
|
||||
brackets and backslashes, a plain list's unchanged text, and two levels of nesting.
|
||||
|
||||
## Checks
|
||||
|
||||
- `expr` host tests: 39 passed, 0 known failures. Full host suite: no new failures.
|
||||
|
|
@ -1074,15 +1074,76 @@ internal sealed class ExpressionEngine
|
|||
return NewList(items);
|
||||
}
|
||||
|
||||
// List text: "[item,item,...]". An item that is itself a whole list is written as is, so
|
||||
// lists nest ("[a,[b,c]]"); any other item has '\', ',', '[' and ']' escaped with '\'.
|
||||
// Items without those characters are written exactly as before ("[a,b]").
|
||||
private static string NewList(IEnumerable<string>? items = null)
|
||||
=> items != null ? "[" + string.Join(",", items) + "]" : "[]";
|
||||
{
|
||||
if (items == null) return "[]";
|
||||
var sb = new System.Text.StringBuilder("[");
|
||||
bool first = true;
|
||||
foreach (string? item in items)
|
||||
{
|
||||
if (!first) sb.Append(',');
|
||||
first = false;
|
||||
AppendListItem(sb, item ?? "");
|
||||
}
|
||||
return sb.Append(']').ToString();
|
||||
}
|
||||
|
||||
private static void AppendListItem(System.Text.StringBuilder sb, string item)
|
||||
{
|
||||
if (IsWholeList(item)) { sb.Append(item); return; }
|
||||
foreach (char c in item)
|
||||
{
|
||||
if (c is '\\' or ',' or '[' or ']') sb.Append('\\');
|
||||
sb.Append(c);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>True when <paramref name="s"/> is one list: its first '[' closes on the last character (escapes skipped).</summary>
|
||||
private static bool IsWholeList(string s)
|
||||
{
|
||||
if (s.Length < 2 || s[0] != '[' || s[s.Length - 1] != ']') return false;
|
||||
int depth = 0;
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
char c = s[i];
|
||||
if (c == '\\') { i++; continue; }
|
||||
if (c == '[') depth++;
|
||||
else if (c == ']' && --depth == 0) return i == s.Length - 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<string>? GetList(string handle)
|
||||
{
|
||||
handle = handle.Trim();
|
||||
if (handle.Length < 2 || handle[0] != '[' || handle[handle.Length - 1] != ']') return null;
|
||||
string inner = handle.Substring(1, handle.Length - 2);
|
||||
return inner.Length == 0 ? new List<string>() : new List<string>(inner.Split(','));
|
||||
var items = new List<string>();
|
||||
if (inner.Length == 0) return items;
|
||||
|
||||
// Split on commas outside nested lists. Escapes are undone for this list's own items;
|
||||
// a nested list keeps them for when it is read as a list itself.
|
||||
var cur = new System.Text.StringBuilder();
|
||||
int depth = 0;
|
||||
for (int i = 0; i < inner.Length; i++)
|
||||
{
|
||||
char c = inner[i];
|
||||
if (c == '\\' && i + 1 < inner.Length)
|
||||
{
|
||||
if (depth > 0) cur.Append(c);
|
||||
cur.Append(inner[++i]);
|
||||
continue;
|
||||
}
|
||||
if (c == '[') depth++;
|
||||
else if (c == ']' && depth > 0) depth--;
|
||||
else if (c == ',' && depth == 0) { items.Add(cur.ToString()); cur.Clear(); continue; }
|
||||
cur.Append(c);
|
||||
}
|
||||
items.Add(cur.ToString());
|
||||
return items;
|
||||
}
|
||||
|
||||
private static string StripBackticks(string s)
|
||||
|
|
|
|||
|
|
@ -53,9 +53,7 @@ internal static class ExpressionTests
|
|||
r.Add("expr: chat and echo", ChatFunctions);
|
||||
r.Add("expr: $name reads an unset variable like getvar[name]", DollarMatchesGetvar); // was a known failure; fixed by 79a39b7
|
||||
r.Add("expr: arithmetic that gives negative zero is false", NegativeZeroIsFalse); // was a known failure; fixed by 92a6318
|
||||
r.KnownFailure("expr: list items may contain commas or be lists",
|
||||
"lists are stored as \"[a,b]\" and split on every comma, so an item with a comma (or a nested list) becomes several items",
|
||||
ListItemsWithCommas);
|
||||
r.Add("expr: list items may contain commas or be lists", ListItemsWithCommas); // was a known failure
|
||||
r.Add("expr: # takes a regex pattern without backticks, as documented", RegexOperatorUnquoted); // was a known failure; fixed by c34b39c
|
||||
r.Add("expr: vitae[] reports no penalty when it cannot read vitae", VitaeWithoutHost); // was a known failure; fixed by 33f23f7
|
||||
}
|
||||
|
|
@ -914,6 +912,23 @@ internal static class ExpressionTests
|
|||
var e = Engine();
|
||||
Check.Eq(e.Evaluate("listcount[listcreate[`a,b`, c]]"), "2", "an item holding a comma");
|
||||
Check.Eq(e.Evaluate("listcount[listadd[listcreate[a], listcreate[b,c]]]"), "2", "a list added to a list");
|
||||
|
||||
// The items come back whole, and a nested list reads as a list again.
|
||||
Check.Eq(e.Evaluate("listgetitem[listcreate[`a,b`, c], 0]"), "a,b", "the comma item reads back unescaped");
|
||||
Check.Eq(e.Evaluate("listgetitem[listadd[listcreate[a], listcreate[b,c]], 1]"), "[b,c]", "the nested list reads back as a list");
|
||||
Check.Eq(e.Evaluate("listcount[listgetitem[listadd[listcreate[a], listcreate[b,c]], 1]]"), "2", "and has its own two items");
|
||||
Check.Eq(e.Evaluate("listcontains[listcreate[`a,b`, c], `a,b`]"), "1", "listcontains finds the comma item");
|
||||
Check.Eq(e.Evaluate("listindexof[listcreate[x, `a,b`], `a,b`]"), "1", "listindexof finds it at 1");
|
||||
|
||||
// Brackets and backslashes in plain text survive too; plain lists keep their old text.
|
||||
Check.Eq(e.Evaluate("listgetitem[listcreate[`x[1`, `c:\\d`], 0]"), "x[1", "an unbalanced bracket");
|
||||
Check.Eq(e.Evaluate("listgetitem[listcreate[`x[1`, `c:\\d`], 1]"), "c:\\d", "a backslash");
|
||||
Check.Eq(e.Evaluate("listcreate[a, b, c]"), "[a,b,c]", "a plain list is written as before");
|
||||
|
||||
// A nested list holding a comma item: the escape survives the nesting.
|
||||
e.Evaluate("setvar[inner, listcreate[`p,q`, r]]");
|
||||
e.Evaluate("setvar[outer, listadd[listcreate[z], getvar[inner]]]");
|
||||
Check.Eq(e.Evaluate("listgetitem[listgetitem[getvar[outer], 1], 0]"), "p,q", "two levels down");
|
||||
}
|
||||
|
||||
private static void RegexOperatorUnquoted()
|
||||
|
|
|
|||
Loading…
Reference in a new issue