Skip to content

Commit 9fba7fc

Browse files
committed
1.1.5
1 parent 7b26532 commit 9fba7fc

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@highmobility/auto-api-javascript",
3-
"version": "1.1.4",
3+
"version": "1.1.5",
44
"description": "Auto API for JavaScript - the parsing library for the Auto API vehicle data model",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/utils/encoding.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,3 @@ export function intToHex(int: number) {
124124
export function intToTwoBytes(int: number) {
125125
return hexToUint8Array(intToHex(int).padStart(4, '0'));
126126
}
127-
128-
export function utfStringToByteArray(string: string, minLength?: number) {
129-
const byteArray = unescape(encodeURIComponent(string))
130-
.split('')
131-
.map((char) => char.charCodeAt(0));
132-
133-
if (minLength !== undefined) {
134-
while (byteArray.length < minLength) {
135-
byteArray.unshift(0);
136-
}
137-
}
138-
139-
return byteArray;
140-
}

src/utils/strings.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,73 @@ export function capitalize(name: string) {
55
export function snakeCaseToPascalCase(name: string) {
66
return name.split('_').map(capitalize).join('');
77
}
8+
9+
/**
10+
* Copyright 2008 The Closure Library Authors. All Rights Reserved.
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*/
13+
export const byteArrayToUtfString = (byteArray: number[]) => {
14+
const out = [];
15+
16+
let pos = 0;
17+
let c = 0;
18+
while (pos < byteArray.length) {
19+
const c1 = byteArray[pos++];
20+
if (c1 < 128) {
21+
out[c++] = String.fromCharCode(c1);
22+
} else if (c1 > 191 && c1 < 224) {
23+
const c2 = byteArray[pos++];
24+
out[c++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
25+
} else if (c1 > 239 && c1 < 365) {
26+
// Surrogate Pair
27+
const c2 = byteArray[pos++];
28+
const c3 = byteArray[pos++];
29+
const c4 = byteArray[pos++];
30+
const u = (((c1 & 7) << 18) | ((c2 & 63) << 12) | ((c3 & 63) << 6) | (c4 & 63)) - 0x10000;
31+
out[c++] = String.fromCharCode(0xd800 + (u >> 10));
32+
out[c++] = String.fromCharCode(0xdc00 + (u & 1023));
33+
} else {
34+
const c2 = byteArray[pos++];
35+
const c3 = byteArray[pos++];
36+
out[c++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
37+
}
38+
}
39+
40+
return out.join('');
41+
};
42+
43+
/**
44+
* Copyright 2008 The Closure Library Authors. All Rights Reserved.
45+
* http://www.apache.org/licenses/LICENSE-2.0
46+
*/
47+
export function utfStringToByteArray(string: string) {
48+
const out = [];
49+
50+
let p = 0;
51+
for (let i = 0; i < string.length; i++) {
52+
let c = string.charCodeAt(i);
53+
if (c < 128) {
54+
out[p++] = c;
55+
} else if (c < 2048) {
56+
out[p++] = (c >> 6) | 192;
57+
out[p++] = (c & 63) | 128;
58+
} else if (
59+
(c & 0xfc00) == 0xd800 &&
60+
i + 1 < string.length &&
61+
(string.charCodeAt(i + 1) & 0xfc00) == 0xdc00
62+
) {
63+
// Surrogate Pair
64+
c = 0x10000 + ((c & 0x03ff) << 10) + (string.charCodeAt(++i) & 0x03ff);
65+
out[p++] = (c >> 18) | 240;
66+
out[p++] = ((c >> 12) & 63) | 128;
67+
out[p++] = ((c >> 6) & 63) | 128;
68+
out[p++] = (c & 63) | 128;
69+
} else {
70+
out[p++] = (c >> 12) | 224;
71+
out[p++] = ((c >> 6) & 63) | 128;
72+
out[p++] = (c & 63) | 128;
73+
}
74+
}
75+
76+
return out;
77+
}

src/values/StringValue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FormatError } from '../core/Error';
44
import { NamedEntity } from '../core/NamedEntity';
55
import { Value } from '../core/Value';
66

7-
import { isString, utfStringToByteArray } from '../utils';
7+
import { byteArrayToUtfString, isString, utfStringToByteArray } from '../utils';
88

99
export class StringValue extends Value<string> implements NamedEntity {
1010
public constructor(public readonly definition: Readonly<Pick<TypeDefinition, 'name' | 'size'>>) {
@@ -20,7 +20,7 @@ export class StringValue extends Value<string> implements NamedEntity {
2020
}
2121

2222
public decode(bytes: number[]) {
23-
this._value = Buffer.from(bytes).toString('utf8').replace(/\0/g, '');
23+
this._value = byteArrayToUtfString(bytes);
2424
return this;
2525
}
2626

0 commit comments

Comments
 (0)