@@ -4,48 +4,76 @@ import {
4
4
isCodecExecuteError ,
5
5
} from "./error" ;
6
6
7
- const HEX_DECIMAL_REGEX = / ^ 0 x ( [ 0 - 9 a - f A - F ] ) + $ / ;
8
- const HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP = new Map < number , RegExp > ( ) ;
7
+ const CHAR_0 = "0" . charCodeAt ( 0 ) ; // 48
8
+ const CHAR_9 = "9" . charCodeAt ( 0 ) ; // 57
9
+ const CHAR_A = "A" . charCodeAt ( 0 ) ; // 65
10
+ const CHAR_F = "F" . charCodeAt ( 0 ) ; // 70
11
+ const CHAR_a = "a" . charCodeAt ( 0 ) ; // 97
12
+ const CHAR_f = "f" . charCodeAt ( 0 ) ; // 102
9
13
10
- export function assertHexDecimal ( str : string , byteLength ?: number ) : void {
11
- if ( byteLength ) {
12
- let regex = HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP . get ( byteLength ) ;
13
- if ( ! regex ) {
14
- const newRegex = new RegExp ( `^0x([0-9a-fA-F]){1,${ byteLength * 2 } }$` ) ;
15
- HEX_DECIMAL_WITH_BYTELENGTH_REGEX_MAP . set ( byteLength , newRegex ) ;
16
- regex = newRegex ;
17
- }
18
- if ( ! regex . test ( str ) ) {
19
- throw new Error ( "Invalid hex decimal!" ) ;
20
- }
21
- } else {
22
- if ( ! HEX_DECIMAL_REGEX . test ( str ) ) {
23
- throw new Error ( "Invalid hex decimal!" ) ;
14
+ function assertStartsWith0x ( str : string ) : void {
15
+ if ( ! str || ! str . startsWith ( "0x" ) ) {
16
+ throw new Error ( "Invalid hex string" ) ;
17
+ }
18
+ }
19
+
20
+ function assertHexChars ( str : string ) : void {
21
+ const strLen = str . length ;
22
+
23
+ for ( let i = 2 ; i < strLen ; i ++ ) {
24
+ const char = str [ i ] . charCodeAt ( 0 ) ;
25
+ if (
26
+ ( char >= CHAR_0 && char <= CHAR_9 ) ||
27
+ ( char >= CHAR_a && char <= CHAR_f ) ||
28
+ ( char >= CHAR_A && char <= CHAR_F )
29
+ ) {
30
+ continue ;
24
31
}
32
+
33
+ throw new Error ( `Invalid hex character ${ str [ i ] } in the string ${ str } ` ) ;
25
34
}
26
35
}
27
36
28
- const HEX_STRING_REGEX = / ^ 0 x ( [ 0 - 9 a - f A - F ] [ 0 - 9 a - f A - F ] ) * $ / ;
29
- const HEX_STRING_WITH_BYTELENGTH_REGEX_MAP = new Map < number , RegExp > ( ) ;
37
+ export function assertHexDecimal ( str : string , byteLength ?: number ) : void {
38
+ assertStartsWith0x ( str ) ;
39
+ if ( str . length === 2 ) {
40
+ throw new Error (
41
+ "Invalid hex decimal length, should be at least 1 character, the '0x' is incorrect, should be '0x0'"
42
+ ) ;
43
+ }
44
+
45
+ const strLen = str . length ;
30
46
47
+ if ( typeof byteLength === "number" && strLen > byteLength * 2 + 2 ) {
48
+ throw new Error (
49
+ `Invalid hex decimal length, should be less than ${ byteLength } bytes, got ${
50
+ strLen / 2 - 1
51
+ } bytes`
52
+ ) ;
53
+ }
54
+
55
+ assertHexChars ( str ) ;
56
+ }
57
+
58
+ /**
59
+ * Assert if a string is a valid hex string that is matched with /^0x([0-9a-fA-F][0-9a-fA-F])*$/
60
+ * @param str
61
+ * @param byteLength
62
+ */
31
63
export function assertHexString ( str : string , byteLength ?: number ) : void {
32
- if ( byteLength ) {
33
- let regex = HEX_STRING_WITH_BYTELENGTH_REGEX_MAP . get ( byteLength ) ;
34
- if ( ! regex ) {
35
- const newRegex = new RegExp (
36
- `^0x([0-9a-fA-F][0-9a-fA-F]){${ byteLength } }$`
37
- ) ;
38
- HEX_STRING_WITH_BYTELENGTH_REGEX_MAP . set ( byteLength , newRegex ) ;
39
- regex = newRegex ;
40
- }
41
- if ( ! regex . test ( str ) ) {
42
- throw new Error ( "Invalid hex string!" ) ;
43
- }
44
- } else {
45
- if ( ! HEX_STRING_REGEX . test ( str ) ) {
46
- throw new Error ( "Invalid hex string!" ) ;
47
- }
64
+ assertStartsWith0x ( str ) ;
65
+
66
+ const strLen = str . length ;
67
+
68
+ if ( strLen % 2 ) {
69
+ throw new Error ( "Invalid hex string length, must be even!" ) ;
48
70
}
71
+
72
+ if ( typeof byteLength === "number" && strLen !== byteLength * 2 + 2 ) {
73
+ throw new Error ( "Invalid hex string length, not match with byteLength!" ) ;
74
+ }
75
+
76
+ assertHexChars ( str ) ;
49
77
}
50
78
51
79
export function assertUtf8String ( str : string ) : void {
2 commit comments
github-actions[bot] commentedon Dec 6, 2023
🚀 New canary release:
0.0.0-canary-b8e9396-20231206155226
vercel[bot] commentedon Dec 6, 2023
Successfully deployed to the following URLs:
lumos-website – ./
lumos-website-git-develop-magickbase.vercel.app
lumos-website-magickbase.vercel.app
lumos-website.vercel.app