added new method
This commit is contained in:
63
Identity.cs
63
Identity.cs
@@ -1,4 +1,4 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Benchmark
|
||||
@@ -20,29 +20,74 @@ namespace Benchmark
|
||||
{
|
||||
identity = default;
|
||||
var position = value.Length;
|
||||
if (!TryGetNumber(value, null, ref position, out var revision)) return false;
|
||||
if (!TryGetNumberOld(ref value, null, ref position, out var revision)) return false;
|
||||
if (value[position] != '-') return false;
|
||||
if (!TryGetNumber(value, 7, ref position, out var index)) return false;
|
||||
if (!TryGetPrefix(value, ref position, out var prefix)) return false;
|
||||
if (!TryGetNumberOld(ref value, 7, ref position, out var index)) return false;
|
||||
if (!TryGetPrefixOld(ref value, ref position, out var prefix)) return false;
|
||||
identity = new Identity(prefix, index, revision);
|
||||
return true; }
|
||||
|
||||
public static bool TryParse(Span<char> value, out Identity identity)
|
||||
{
|
||||
identity = default;
|
||||
var position = value.Length;
|
||||
if (!TryGetNumber(ref value, null, ref position, out var revision)) return false;
|
||||
if (value[position] != '-') return false;
|
||||
if (!TryGetNumber(ref value, 7, ref position, out var index)) return false;
|
||||
if (!TryGetPrefix(ref value, ref position, out var prefix)) return false;
|
||||
identity = new Identity(prefix, index, revision);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryGetPrefix(in string identity, ref int lastPosition, out string prefix)
|
||||
|
||||
private static bool TryGetPrefixOld(ref string identity, ref int lastPosition, out string prefix)
|
||||
{
|
||||
prefix = identity.Remove(lastPosition + 1);
|
||||
prefix = identity[(lastPosition + 1)..];
|
||||
if (prefix.Length < 3) return false;
|
||||
return !prefix.Any(character => character < 'A' || character > 'Z');
|
||||
for (int i = 0; i < prefix.Length; i++)
|
||||
{
|
||||
if (prefix[i] < 'A' || prefix[i] > 'Z') return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryGetNumber(in string identity, int? exactLength, ref int currentPosition, out int revision)
|
||||
private static bool TryGetPrefix(ref Span<char> identity, ref int lastPosition, out string prefix)
|
||||
{
|
||||
var span = identity[(lastPosition + 1)..];
|
||||
prefix = span.ToString();
|
||||
if (span.Length < 3) return false;
|
||||
for (int i = 0; i < span.Length; i++)
|
||||
{
|
||||
if (span[i] < 'A' || span[i] > 'Z') return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool TryGetNumberOld(ref string identity, int? exactLength, ref int currentPosition, out int revision)
|
||||
{
|
||||
revision = 0;
|
||||
var start = currentPosition;
|
||||
for (currentPosition = start - 1; currentPosition >= 0; currentPosition--)
|
||||
{
|
||||
var current = identity[currentPosition];
|
||||
if(current == '0') continue;
|
||||
if (current == '0') continue;
|
||||
if (current < '1' || current > '9') break;
|
||||
revision += (current - '0') * IntegerPow(10, start - currentPosition - 1);
|
||||
}
|
||||
|
||||
if (exactLength != null && start - (currentPosition + 1) != exactLength) return false;
|
||||
return revision != default;
|
||||
}
|
||||
|
||||
private static bool TryGetNumber(ref Span<char> identity, int? exactLength, ref int currentPosition, out int revision)
|
||||
{
|
||||
revision = 0;
|
||||
var start = currentPosition;
|
||||
for (currentPosition = start - 1; currentPosition >= 0; currentPosition--)
|
||||
{
|
||||
var current = identity[currentPosition];
|
||||
if (current == '0') continue;
|
||||
if (current < '1' || current > '9') break;
|
||||
revision += (current - '0') * IntegerPow(10, start - currentPosition - 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user