Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jul 13, 2024
1 parent 5bd6e32 commit 5a61143
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Jint/Native/TypedArray/TypedArrayConstructor.Uint8Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ private JsTypedArray FromHex(JsValue thisObject, JsValue[] arguments)
return ta;
}

private static readonly SearchValues<char> HexAlphabet = SearchValues.Create("0123456789abcdefABCDEF");

internal static FromEncodingResult FromHex(Engine engine, string s, uint maxLength = int.MaxValue)
{
var length = s.Length;
Expand All @@ -319,20 +321,19 @@ internal static FromEncodingResult FromHex(Engine engine, string s, uint maxLeng
return new FromEncodingResult(bytes, ExceptionHelper.CreateSyntaxError(engine.Realm, "Invalid hex string"), read);
}

int byteIndex = 0;
const string Allowed = "0123456789abcdefABCDEF";
var byteIndex = 0;
while (read < length && byteIndex < maxLength)
{
var hexits = s.AsSpan(read, 2);
if (!Allowed.Contains(hexits[0]) || !Allowed.Contains(hexits[1]))
if (!HexAlphabet.Contains(hexits[0]) || !HexAlphabet.Contains(hexits[1]))
{
return new FromEncodingResult(bytes, ExceptionHelper.CreateSyntaxError(engine.Realm, "Invalid hex value"), read);
}

#if SUPPORTS_SPAN_PARSE
var b = byte.Parse(hexits, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
#else
var b = byte.Parse(hexits.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
var b = byte.Parse(hexits.ToString(), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
#endif
bytes[byteIndex++] = b;
read += 2;
Expand Down

0 comments on commit 5a61143

Please sign in to comment.