Path: redb-route/redb.Route.DataFormats.XmlJson/Internal/ArrayPaths.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.DataFormats.XmlJson/Internal/ArrayPaths.cs
----
namespace redb.Route.DataFormats.XmlJson.Internal;
///
/// The paths, parsed once: /a/b/c matches the element path from the root,
/// //b/c matches its end at any depth. A step without a prefix compares with the local name, a step with one
/// compares with the name as the document writes it.
///
internal sealed class ArrayPaths
{
private readonly List<(bool Anywhere, string[] Steps)> _paths = [];
public ArrayPaths(IEnumerable paths)
{
foreach (var path in paths)
_paths.Add(Parse(path));
}
/// Whether no path is given.
public bool IsEmpty => _paths.Count == 0;
private static (bool, string[]) Parse(string path)
{
var anywhere = path.StartsWith("//", StringComparison.Ordinal);
if (!anywhere && !path.StartsWith('/'))
throw new ArgumentException(
$"xmljson arrays path '{path}' starts with '/' (from the root) or '//' (at any depth).", nameof(path));
var steps = path[(anywhere ? 2 : 1)..].Split('/');
if (steps.Any(s => s.Length == 0 || s.Any(char.IsWhiteSpace)))
throw new ArgumentException(
$"xmljson arrays path '{path}' has an empty or blank step; write it as /Order/Items/Item or //Item.",
nameof(path));
return (anywhere, steps);
}
///
/// Whether the element at the end of (local and written names, root first) is an array.
///
public bool Matches(IReadOnlyList<(string Local, string Written)> path)
{
foreach (var (anywhere, steps) in _paths)
{
if (steps.Length > path.Count || (!anywhere && steps.Length != path.Count))
continue;
var offset = path.Count - steps.Length;
var all = true;
for (var i = 0; i < steps.Length && all; i++)
{
var (local, written) = path[offset + i];
all = steps[i].Contains(':')
? string.Equals(steps[i], written, StringComparison.Ordinal)
: string.Equals(steps[i], local, StringComparison.Ordinal);
}
if (all)
return true;
}
return false;
}
}