File tree Expand file tree Collapse file tree 2 files changed +27
-11
lines changed Expand file tree Collapse file tree 2 files changed +27
-11
lines changed Original file line number Diff line number Diff line change @@ -36,18 +36,22 @@ import {
36
36
VariableExpression ,
37
37
} from './types' ;
38
38
39
- // TODO: Support dots between bytes
40
39
const blobParser : Parser < BlobValue > = regexp ( / 0 [ z ] / i) . then (
41
- regexp ( / [ 0 - 1 a - z ] * / i) . map < BlobValue > ( ( hexData ) => {
42
- if ( hexData . length % 2 !== 0 ) {
43
- throw VimError . fromCode ( ErrorCode . BlobLiteralShouldHaveAnEvenNumberOfHexCharacters ) ;
44
- }
45
- const data = new Uint8Array ( new ArrayBuffer ( hexData . length / 2 ) ) ;
46
- for ( let i = 0 ; i < hexData . length ; i += 2 ) {
47
- data [ i / 2 ] = Number . parseInt ( hexData . substring ( i , i + 2 ) , 16 ) ;
48
- }
49
- return blob ( data ) ;
50
- } ) ,
40
+ regexp ( / [ 0 - 1 a - f ] { 1 , 2 } / i)
41
+ . sepBy ( string ( '.' ) . fallback ( undefined ) )
42
+ . map < BlobValue > ( ( bytes ) => {
43
+ const lastByte = bytes . at ( - 1 ) ;
44
+ if ( lastByte && lastByte . length !== 2 ) {
45
+ throw VimError . fromCode ( ErrorCode . BlobLiteralShouldHaveAnEvenNumberOfHexCharacters ) ;
46
+ }
47
+ const data = new Uint8Array ( new ArrayBuffer ( bytes . length ) ) ;
48
+ let i = 0 ;
49
+ for ( const byte of bytes ) {
50
+ data [ i ] = Number . parseInt ( byte , 16 ) ;
51
+ ++ i ;
52
+ }
53
+ return blob ( data ) ;
54
+ } ) ,
51
55
) ;
52
56
53
57
const binaryNumberParser : Parser < NumberValue > = regexp ( / 0 [ b ] / i) . then (
Original file line number Diff line number Diff line change @@ -111,6 +111,12 @@ suite('Vimscript expressions', () => {
111
111
} ) ;
112
112
113
113
suite ( 'Blobs' , ( ) => {
114
+ exprTest ( '0z' , {
115
+ expr : {
116
+ type : 'blob' ,
117
+ data : new Uint8Array ( [ ] ) ,
118
+ } ,
119
+ } ) ;
114
120
exprTest ( '0zabcd' , {
115
121
expr : {
116
122
type : 'blob' ,
@@ -123,6 +129,12 @@ suite('Vimscript expressions', () => {
123
129
data : new Uint8Array ( [ 171 , 205 ] ) ,
124
130
} ,
125
131
} ) ;
132
+ exprTest ( '0zAB.CD' , {
133
+ expr : {
134
+ type : 'blob' ,
135
+ data : new Uint8Array ( [ 171 , 205 ] ) ,
136
+ } ,
137
+ } ) ;
126
138
exprTest ( '0zabc' , {
127
139
error : ErrorCode . BlobLiteralShouldHaveAnEvenNumberOfHexCharacters ,
128
140
} ) ;
You can’t perform that action at this time.
0 commit comments