csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
using System;
|
|
using System.Globalization;
|
|
|
|
namespace Avalonia.Utilities
|
|
{
|
|
#if !BUILDTASK
|
|
public
|
|
#endif
|
|
static class IdentifierParser
|
|
{
|
|
public static ReadOnlySpan<char> ParseIdentifier(this
|
|
#if NET7SDK
|
|
scoped
|
|
#endif
|
|
ref CharacterReader r)
|
|
{
|
|
if (IsValidIdentifierStart(r.Peek))
|
|
{
|
|
return r.TakeWhile(c => IsValidIdentifierChar(c));
|
|
}
|
|
else
|
|
{
|
|
return ReadOnlySpan<char>.Empty;
|
|
}
|
|
}
|
|
|
|
private static bool IsValidIdentifierStart(char c)
|
|
{
|
|
return char.IsLetter(c) || c == '_';
|
|
}
|
|
|
|
private static bool IsValidIdentifierChar(char c)
|
|
{
|
|
if (IsValidIdentifierStart(c))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
var cat = CharUnicodeInfo.GetUnicodeCategory(c);
|
|
return cat == UnicodeCategory.NonSpacingMark ||
|
|
cat == UnicodeCategory.SpacingCombiningMark ||
|
|
cat == UnicodeCategory.ConnectorPunctuation ||
|
|
cat == UnicodeCategory.Format ||
|
|
cat == UnicodeCategory.DecimalDigitNumber;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|