Skip to content

Commit

Permalink
refactor utf8ArrayToStr method in ID3.js JW8-359
Browse files Browse the repository at this point in the history
  • Loading branch information
DanFerrer committed Nov 2, 2017
1 parent 905e65f commit 1640fbe
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/demux/id3.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,41 +292,47 @@
* LastModified: Dec 25 1999
* This library is free. You can redistribute it and/or modify it.
*/
static _utf8ArrayToStr(array) {
static _utf8ArrayToStr(array, startingIndex) {

const len = array.length;
let c;
let char2;
let char3;
let out = '';
let i = 0;
let length = array.length;

while (i < length) {
let c = array[i++];
switch (c >> 4) {
case 0:
return out;
case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
let i = startingIndex || 0;
while (i < len) {
c = array[i++];
// If the character is 3 (END_OF_TEXT) or 0 (NULL) then skip it
if (c === 0x00 || c === 0x03) {
continue;
}
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
}
}

return out;
}
}

const utf8ArrayToStr = ID3._utf8ArrayToStr;

export default ID3;

export { utf8ArrayToStr };
1 change: 1 addition & 0 deletions src/utils/webvtt-parser.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import VTTParser from './vttparser';
import { utf8ArrayToStr } from '../demux/id3'

// String.prototype.startsWith is not supported in IE11
const startsWith = function(inputString, searchString, position) {
Expand Down

0 comments on commit 1640fbe

Please sign in to comment.