Skip to content

Commit

Permalink
Use non-allocating split (#7837)
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams authored Nov 29, 2024
1 parent 9c80dbf commit 89c5835
Showing 1 changed file with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,22 +168,24 @@ public static bool TryGetSubProperty(this JsonElement element, string innerPath,
{
ArgumentNullException.ThrowIfNullOrEmpty(innerPath);

if (innerPath.Contains('.'))
ReadOnlySpan<char> pathSpan = innerPath.AsSpan();
int lastDot = pathSpan.LastIndexOf('.');
if (lastDot >= 0)
{
string[] parts = innerPath.Split('.');
JsonElement currentElement = element;
for (int i = 0; i < parts.Length - 1; i++)
foreach (Range subPath in pathSpan[..lastDot].Split('.'))
{
if (!currentElement.TryGetProperty(parts[i], out currentElement))
if (!currentElement.TryGetProperty(pathSpan[subPath], out currentElement))
{
value = default;
return false;
}
}
return currentElement.TryGetProperty(parts[^1], out value);
lastDot++;
return currentElement.TryGetProperty(pathSpan[lastDot..], out value);
}

return element.TryGetProperty(innerPath.AsSpan(), out value);
return element.TryGetProperty(pathSpan, out value);
}
}
}

0 comments on commit 89c5835

Please sign in to comment.