Path: redb-route/redb.Route/Expressions/TemplateScanner.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/TemplateScanner.cs
----
namespace redb.Route.Expressions;
///
/// Finds the ${...} holes of a template: the one reader every place that looks for them uses (the template
/// compiler, , , the single-placeholder test, the
/// pack gate). A hole closes at the } that balances its ${, so a hole may hold another one
/// (${bean:svc.Check(${body})}) and a quoted literal may hold a } (${header.a == '}'}).
///
/// A plain { does not nest and an empty ${} is no hole, as before. A quote that never ends inside a
/// hole is read as a character, not as a literal (${header.O'Brien}). A ${ that nothing closes is an
/// error: the expression was started, so it is a mistake (owner decision 2026-11-05); see .
/// A literal ${ is written \${: an escape opens nothing and renders as ${.
///
///
internal static class TemplateScanner
{
/// One hole: is the index of $, spans to the closing }.
internal readonly record struct Hole(int Start, int Length)
{
/// The text between ${ and the closing }.
public string InnerOf(string template) => template.Substring(Start + 2, Length - 3);
}
///
/// The holes of a template, the index of every ${ that nothing closes, and the index of every escape
/// \${ (a literal ${ in the text).
///
internal sealed record ScanResult(IReadOnlyList Holes, IReadOnlyList Unclosed, IReadOnlyList Escapes);
///
/// The holes of , in order. Throws naming
/// the position of the first ${ that nothing closes.
///
internal static IReadOnlyList Holes(string template)
{
var scan = Scan(template);
if (scan.Unclosed.Count > 0)
throw new ExpressionCompilationException(Unclosed(template, scan.Unclosed[0]));
return scan.Holes;
}
///
/// Whether starts at least one ${...}, closed or not: an unclosed one is a
/// template too, and fails where it is compiled.
///
internal static bool HasPlaceholder(string template)
{
if (!template.Contains("${", StringComparison.Ordinal))
return false;
var scan = Scan(template);
return scan.Holes.Count > 0 || scan.Unclosed.Count > 0 || scan.Escapes.Count > 0;
}
/// The message for a ${ at that nothing closes.
internal static string Unclosed(string template, int index)
=> $"unclosed '${{' at position {index + 1} of \"{template}\": the placeholder has no closing '}}'.";
/// Scans ; see the class remarks for the reading.
internal static ScanResult Scan(string template)
{
ArgumentNullException.ThrowIfNull(template);
var holes = new List();
var unclosed = new List();
var escapes = new List();
var i = 0;
while (i < template.Length - 1)
{
// \${ is a literal ${ (findings register row 16): it opens nothing, and the compiler writes ${ in its place.
if (template[i] == '\\' && i + 2 < template.Length && template[i + 1] == '$' && template[i + 2] == '{')
{
escapes.Add(i);
i += 3;
continue;
}
if (template[i] != '$' || template[i + 1] != '{')
{
i++;
continue;
}
var close = Close(template, i, quotes: true) ?? Close(template, i, quotes: false);
if (close is null)
{
unclosed.Add(i);
break;
}
if (close.Value - i - 2 > 0)
{
holes.Add(new Hole(i, close.Value - i + 1));
i = close.Value + 1;
}
else
{
i++;
}
}
return new ScanResult(holes, unclosed, escapes);
}
/// The index of the } that balances the ${ at , or null.
private static int? Close(string template, int start, bool quotes)
{
var depth = 0;
var j = start;
while (j < template.Length)
{
var c = template[j];
if (c == '$' && j + 1 < template.Length && template[j + 1] == '{')
{
depth++;
j += 2;
continue;
}
if (c == '}')
{
depth--;
if (depth == 0)
return j;
j++;
continue;
}
if (quotes && (c == '\'' || c == '"'))
{
var endQuote = EndOfQuoted(template, j);
if (endQuote < 0)
return null;
j = endQuote + 1;
continue;
}
j++;
}
return null;
}
/// The index of the quote that ends the literal opened at , or -1.
private static int EndOfQuoted(string template, int open)
{
var quote = template[open];
for (var j = open + 1; j < template.Length; j++)
{
if (template[j] == '\\')
{
j++;
continue;
}
if (template[j] == quote)
return j;
}
return -1;
}
}