-
-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement workarounds for regex parsing known issues (#1603)
- Loading branch information
Showing
4 changed files
with
129 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
namespace Jint; | ||
|
||
internal static class Shims | ||
{ | ||
public static byte[] BytesFromHexString(this ReadOnlySpan<char> value) | ||
{ | ||
#if NET6_0_OR_GREATER | ||
return Convert.FromHexString(value); | ||
#else | ||
if ((value.Length & 1) != 0) | ||
{ | ||
throw new FormatException(); | ||
} | ||
|
||
var byteCount = value.Length >> 1; | ||
var result = new byte[byteCount]; | ||
var index = 0; | ||
for (var i = 0; i < byteCount; i++) | ||
{ | ||
int hi, lo; | ||
if ((hi = GetDigitValue(value[index++])) < 0 | ||
|| (lo = GetDigitValue(value[index++])) < 0) | ||
{ | ||
throw new FormatException(); | ||
} | ||
|
||
result[i] = (byte) (hi << 4 | lo); | ||
} | ||
|
||
return result; | ||
|
||
static int GetDigitValue(char ch) => ch switch | ||
{ | ||
>= '0' and <= '9' => ch - 0x30, | ||
>= 'a' and <= 'f' => ch - 0x57, | ||
>= 'A' and <= 'F' => ch - 0x37, | ||
_ => -1 | ||
}; | ||
#endif | ||
} | ||
} |