Skip to content

Commit 086af3a

Browse files
committed
Blobs are hex and can have dots between bytes
1 parent 47130e8 commit 086af3a

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/vimscript/expression/parser.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,22 @@ import {
3636
VariableExpression,
3737
} from './types';
3838

39-
// TODO: Support dots between bytes
4039
const blobParser: Parser<BlobValue> = regexp(/0[z]/i).then(
41-
regexp(/[0-1a-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-1a-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+
}),
5155
);
5256

5357
const binaryNumberParser: Parser<NumberValue> = regexp(/0[b]/i).then(

test/vimscript/expression.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ suite('Vimscript expressions', () => {
111111
});
112112

113113
suite('Blobs', () => {
114+
exprTest('0z', {
115+
expr: {
116+
type: 'blob',
117+
data: new Uint8Array([]),
118+
},
119+
});
114120
exprTest('0zabcd', {
115121
expr: {
116122
type: 'blob',
@@ -123,6 +129,12 @@ suite('Vimscript expressions', () => {
123129
data: new Uint8Array([171, 205]),
124130
},
125131
});
132+
exprTest('0zAB.CD', {
133+
expr: {
134+
type: 'blob',
135+
data: new Uint8Array([171, 205]),
136+
},
137+
});
126138
exprTest('0zabc', {
127139
error: ErrorCode.BlobLiteralShouldHaveAnEvenNumberOfHexCharacters,
128140
});

0 commit comments

Comments
 (0)