Optimize StartsWithPrefix
-Remove double "equality" test in StartsWithPrefix -Further StartsWithPrefix efficiencies -Remove subkey allocation in StartsWithPrefix
This commit is contained in:
parent
dacebacb90
commit
6c2c777bdc
|
|
@ -681,54 +681,51 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
|
||||||
throw new ArgumentNullException(nameof(key));
|
throw new ArgumentNullException(nameof(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StringComparer.OrdinalIgnoreCase.Equals(key, prefix))
|
if (prefix.Length == 0)
|
||||||
{
|
{
|
||||||
|
// Everything is prefixed by the empty string
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key.Length <= prefix.Length)
|
if (key.Length < prefix.Length)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var subKeyIndex = 0;
|
||||||
if (!key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
if (!key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
if (key.StartsWith("[", StringComparison.OrdinalIgnoreCase))
|
if (key[0] == '[')
|
||||||
{
|
{
|
||||||
var subKey = key.Substring(key.IndexOf('.') + 1);
|
subKeyIndex = key.IndexOf('.') + 1;
|
||||||
|
|
||||||
if (!subKey.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
|
if (string.Compare(key, subKeyIndex, prefix, 0, prefix.Length, StringComparison.OrdinalIgnoreCase) != 0)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
else if (prefix.Length == (key.Length - subKeyIndex))
|
||||||
if (string.Equals(prefix, subKey, StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
{
|
||||||
|
// prefix == subKey
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = subKey;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (key.Length == prefix.Length)
|
||||||
// Everything is prefixed by the empty string
|
|
||||||
if (prefix.Length == 0)
|
|
||||||
{
|
{
|
||||||
|
// key == prefix
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
var charAfterPrefix = key[subKeyIndex + prefix.Length];
|
||||||
|
switch (charAfterPrefix)
|
||||||
{
|
{
|
||||||
var charAfterPrefix = key[prefix.Length];
|
case '[':
|
||||||
switch (charAfterPrefix)
|
case '.':
|
||||||
{
|
return true;
|
||||||
case '[':
|
|
||||||
case '.':
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -871,4 +868,4 @@ namespace Microsoft.AspNetCore.Mvc.ModelBinding
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue