Skip to content

Commit b809646

Browse files
authored
fix: Count line break characters as ASCII (#8376)
Previously, shaka.util.StringUtils.fromBytesAutoDetect assumed any character between ' ' and '~' was ASCII. This worked for many cases, but it meant that the method would be unable to determine the encoding of a buffer if there was a newline character near the start. This could be a problem for parsing, for example, JSON. Closes #8336
1 parent 9ade92f commit b809646

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/util/string_utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ shaka.util.StringUtils = class {
179179
}
180180

181181
const isAscii = (i) => {
182-
// arr[i] >= ' ' && arr[i] <= '~';
183-
return uint8.byteLength <= i || (uint8[i] >= 0x20 && uint8[i] <= 0x7e);
182+
// arr[i] >= horizontal tab && arr[i] <= '~';
183+
return uint8.byteLength <= i || (uint8[i] >= 0x09 && uint8[i] <= 0x7e);
184184
};
185185

186186
shaka.log.debug(

test/util/string_utils_unit.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ function defineStringUtilTests() {
122122
expect(StringUtils.fromBytesAutoDetect(new Uint8Array(arr))).toBe('Foo');
123123
});
124124

125+
// Regression test for #8336
126+
it('counts newlines as ASCII', () => {
127+
const arr = [0x0a, 0x46, 0x6f];
128+
expect(StringUtils.fromBytesAutoDetect(new Uint8Array(arr))).toBe('\nFo');
129+
});
130+
125131
it('fails if unable to guess', () => {
126132
const expected = shaka.test.Util.jasmineError(new shaka.util.Error(
127133
shaka.util.Error.Severity.CRITICAL,

0 commit comments

Comments
 (0)