encoding.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  1. var _this;
  2. import encodingIndexes from "./encoding-indexes.js"
  3. (function(global) {
  4. 'use strict';
  5. _this = global;
  6. if (typeof module !== "undefined" && module.exports &&
  7. !global["encoding-indexes"]) {
  8. global["encoding-indexes"] =
  9. require("./encoding-indexes.js")["encoding-indexes"];
  10. } else {
  11. // console.log(encodingIndexes);
  12. global["encoding-indexes"] = encodingIndexes;
  13. }
  14. function inRange(a, min, max) {
  15. return min <= a && a <= max;
  16. }
  17. function includes(array, item) {
  18. return array.indexOf(item) !== -1;
  19. }
  20. var floor = Math.floor;
  21. function ToDictionary(o) {
  22. if (o === undefined) return {};
  23. if (o === Object(o)) return o;
  24. throw TypeError('Could not convert argument to dictionary');
  25. }
  26. function stringToCodePoints(string) {
  27. var s = String(string);
  28. var n = s.length;
  29. var i = 0;
  30. var u = [];
  31. while (i < n) {
  32. var c = s.charCodeAt(i);
  33. if (c < 0xD800 || c > 0xDFFF) {
  34. u.push(c);
  35. } else if (0xDC00 <= c && c <= 0xDFFF) {
  36. u.push(0xFFFD);
  37. } else if (0xD800 <= c && c <= 0xDBFF) {
  38. if (i === n - 1) {
  39. u.push(0xFFFD);
  40. } else {
  41. var d = s.charCodeAt(i + 1);
  42. if (0xDC00 <= d && d <= 0xDFFF) {
  43. var a = c & 0x3FF;
  44. var b = d & 0x3FF;
  45. u.push(0x10000 + (a << 10) + b);
  46. i += 1;
  47. } else {
  48. u.push(0xFFFD);
  49. }
  50. }
  51. }
  52. i += 1;
  53. }
  54. return u;
  55. }
  56. function codePointsToString(code_points) {
  57. var s = '';
  58. for (var i = 0; i < code_points.length; ++i) {
  59. var cp = code_points[i];
  60. if (cp <= 0xFFFF) {
  61. s += String.fromCharCode(cp);
  62. } else {
  63. cp -= 0x10000;
  64. s += String.fromCharCode((cp >> 10) + 0xD800,
  65. (cp & 0x3FF) + 0xDC00);
  66. }
  67. }
  68. return s;
  69. }
  70. function isASCIIByte(a) {
  71. return 0x00 <= a && a <= 0x7F;
  72. }
  73. var isASCIICodePoint = isASCIIByte;
  74. var end_of_stream = -1;
  75. function Stream(tokens) {
  76. this.tokens = [].slice.call(tokens);
  77. this.tokens.reverse();
  78. }
  79. Stream.prototype = {
  80. endOfStream: function() {
  81. return !this.tokens.length;
  82. },
  83. read: function() {
  84. if (!this.tokens.length)
  85. return end_of_stream;
  86. return this.tokens.pop();
  87. },
  88. prepend: function(token) {
  89. if (Array.isArray(token)) {
  90. var tokens = /**@type {!Array.<number>}*/ (token);
  91. while (tokens.length)
  92. this.tokens.push(tokens.pop());
  93. } else {
  94. this.tokens.push(token);
  95. }
  96. },
  97. push: function(token) {
  98. if (Array.isArray(token)) {
  99. var tokens = /**@type {!Array.<number>}*/ (token);
  100. while (tokens.length)
  101. this.tokens.unshift(tokens.shift());
  102. } else {
  103. this.tokens.unshift(token);
  104. }
  105. }
  106. };
  107. var finished = -1;
  108. function decoderError(fatal, opt_code_point) {
  109. if (fatal)
  110. throw TypeError('Decoder error');
  111. return opt_code_point || 0xFFFD;
  112. }
  113. function encoderError(code_point) {
  114. throw TypeError('The code point ' + code_point + ' could not be encoded.');
  115. }
  116. /** @interface */
  117. function Decoder() {}
  118. Decoder.prototype = {
  119. handler: function(stream, bite) {}
  120. };
  121. /** @interface */
  122. function Encoder() {}
  123. Encoder.prototype = {
  124. handler: function(stream, code_point) {}
  125. };
  126. function getEncoding(label) {
  127. label = String(label).trim().toLowerCase();
  128. if (Object.prototype.hasOwnProperty.call(label_to_encoding, label)) {
  129. return label_to_encoding[label];
  130. }
  131. return null;
  132. }
  133. var encodings = [{
  134. "encodings": [{
  135. "labels": [
  136. "unicode-1-1-utf-8",
  137. "utf-8",
  138. "utf8"
  139. ],
  140. "name": "UTF-8"
  141. }],
  142. "heading": "The Encoding"
  143. },
  144. {
  145. "encodings": [{
  146. "labels": [
  147. "866",
  148. "cp866",
  149. "csibm866",
  150. "ibm866"
  151. ],
  152. "name": "IBM866"
  153. },
  154. {
  155. "labels": [
  156. "csisolatin2",
  157. "iso-8859-2",
  158. "iso-ir-101",
  159. "iso8859-2",
  160. "iso88592",
  161. "iso_8859-2",
  162. "iso_8859-2:1987",
  163. "l2",
  164. "latin2"
  165. ],
  166. "name": "ISO-8859-2"
  167. },
  168. {
  169. "labels": [
  170. "csisolatin3",
  171. "iso-8859-3",
  172. "iso-ir-109",
  173. "iso8859-3",
  174. "iso88593",
  175. "iso_8859-3",
  176. "iso_8859-3:1988",
  177. "l3",
  178. "latin3"
  179. ],
  180. "name": "ISO-8859-3"
  181. },
  182. {
  183. "labels": [
  184. "csisolatin4",
  185. "iso-8859-4",
  186. "iso-ir-110",
  187. "iso8859-4",
  188. "iso88594",
  189. "iso_8859-4",
  190. "iso_8859-4:1988",
  191. "l4",
  192. "latin4"
  193. ],
  194. "name": "ISO-8859-4"
  195. },
  196. {
  197. "labels": [
  198. "csisolatincyrillic",
  199. "cyrillic",
  200. "iso-8859-5",
  201. "iso-ir-144",
  202. "iso8859-5",
  203. "iso88595",
  204. "iso_8859-5",
  205. "iso_8859-5:1988"
  206. ],
  207. "name": "ISO-8859-5"
  208. },
  209. {
  210. "labels": [
  211. "arabic",
  212. "asmo-708",
  213. "csiso88596e",
  214. "csiso88596i",
  215. "csisolatinarabic",
  216. "ecma-114",
  217. "iso-8859-6",
  218. "iso-8859-6-e",
  219. "iso-8859-6-i",
  220. "iso-ir-127",
  221. "iso8859-6",
  222. "iso88596",
  223. "iso_8859-6",
  224. "iso_8859-6:1987"
  225. ],
  226. "name": "ISO-8859-6"
  227. },
  228. {
  229. "labels": [
  230. "csisolatingreek",
  231. "ecma-118",
  232. "elot_928",
  233. "greek",
  234. "greek8",
  235. "iso-8859-7",
  236. "iso-ir-126",
  237. "iso8859-7",
  238. "iso88597",
  239. "iso_8859-7",
  240. "iso_8859-7:1987",
  241. "sun_eu_greek"
  242. ],
  243. "name": "ISO-8859-7"
  244. },
  245. {
  246. "labels": [
  247. "csiso88598e",
  248. "csisolatinhebrew",
  249. "hebrew",
  250. "iso-8859-8",
  251. "iso-8859-8-e",
  252. "iso-ir-138",
  253. "iso8859-8",
  254. "iso88598",
  255. "iso_8859-8",
  256. "iso_8859-8:1988",
  257. "visual"
  258. ],
  259. "name": "ISO-8859-8"
  260. },
  261. {
  262. "labels": [
  263. "csiso88598i",
  264. "iso-8859-8-i",
  265. "logical"
  266. ],
  267. "name": "ISO-8859-8-I"
  268. },
  269. {
  270. "labels": [
  271. "csisolatin6",
  272. "iso-8859-10",
  273. "iso-ir-157",
  274. "iso8859-10",
  275. "iso885910",
  276. "l6",
  277. "latin6"
  278. ],
  279. "name": "ISO-8859-10"
  280. },
  281. {
  282. "labels": [
  283. "iso-8859-13",
  284. "iso8859-13",
  285. "iso885913"
  286. ],
  287. "name": "ISO-8859-13"
  288. },
  289. {
  290. "labels": [
  291. "iso-8859-14",
  292. "iso8859-14",
  293. "iso885914"
  294. ],
  295. "name": "ISO-8859-14"
  296. },
  297. {
  298. "labels": [
  299. "csisolatin9",
  300. "iso-8859-15",
  301. "iso8859-15",
  302. "iso885915",
  303. "iso_8859-15",
  304. "l9"
  305. ],
  306. "name": "ISO-8859-15"
  307. },
  308. {
  309. "labels": [
  310. "iso-8859-16"
  311. ],
  312. "name": "ISO-8859-16"
  313. },
  314. {
  315. "labels": [
  316. "cskoi8r",
  317. "koi",
  318. "koi8",
  319. "koi8-r",
  320. "koi8_r"
  321. ],
  322. "name": "KOI8-R"
  323. },
  324. {
  325. "labels": [
  326. "koi8-ru",
  327. "koi8-u"
  328. ],
  329. "name": "KOI8-U"
  330. },
  331. {
  332. "labels": [
  333. "csmacintosh",
  334. "mac",
  335. "macintosh",
  336. "x-mac-roman"
  337. ],
  338. "name": "macintosh"
  339. },
  340. {
  341. "labels": [
  342. "dos-874",
  343. "iso-8859-11",
  344. "iso8859-11",
  345. "iso885911",
  346. "tis-620",
  347. "windows-874"
  348. ],
  349. "name": "windows-874"
  350. },
  351. {
  352. "labels": [
  353. "cp1250",
  354. "windows-1250",
  355. "x-cp1250"
  356. ],
  357. "name": "windows-1250"
  358. },
  359. {
  360. "labels": [
  361. "cp1251",
  362. "windows-1251",
  363. "x-cp1251"
  364. ],
  365. "name": "windows-1251"
  366. },
  367. {
  368. "labels": [
  369. "ansi_x3.4-1968",
  370. "ascii",
  371. "cp1252",
  372. "cp819",
  373. "csisolatin1",
  374. "ibm819",
  375. "iso-8859-1",
  376. "iso-ir-100",
  377. "iso8859-1",
  378. "iso88591",
  379. "iso_8859-1",
  380. "iso_8859-1:1987",
  381. "l1",
  382. "latin1",
  383. "us-ascii",
  384. "windows-1252",
  385. "x-cp1252"
  386. ],
  387. "name": "windows-1252"
  388. },
  389. {
  390. "labels": [
  391. "cp1253",
  392. "windows-1253",
  393. "x-cp1253"
  394. ],
  395. "name": "windows-1253"
  396. },
  397. {
  398. "labels": [
  399. "cp1254",
  400. "csisolatin5",
  401. "iso-8859-9",
  402. "iso-ir-148",
  403. "iso8859-9",
  404. "iso88599",
  405. "iso_8859-9",
  406. "iso_8859-9:1989",
  407. "l5",
  408. "latin5",
  409. "windows-1254",
  410. "x-cp1254"
  411. ],
  412. "name": "windows-1254"
  413. },
  414. {
  415. "labels": [
  416. "cp1255",
  417. "windows-1255",
  418. "x-cp1255"
  419. ],
  420. "name": "windows-1255"
  421. },
  422. {
  423. "labels": [
  424. "cp1256",
  425. "windows-1256",
  426. "x-cp1256"
  427. ],
  428. "name": "windows-1256"
  429. },
  430. {
  431. "labels": [
  432. "cp1257",
  433. "windows-1257",
  434. "x-cp1257"
  435. ],
  436. "name": "windows-1257"
  437. },
  438. {
  439. "labels": [
  440. "cp1258",
  441. "windows-1258",
  442. "x-cp1258"
  443. ],
  444. "name": "windows-1258"
  445. },
  446. {
  447. "labels": [
  448. "x-mac-cyrillic",
  449. "x-mac-ukrainian"
  450. ],
  451. "name": "x-mac-cyrillic"
  452. }
  453. ],
  454. "heading": "Legacy single-byte encodings"
  455. },
  456. {
  457. "encodings": [{
  458. "labels": [
  459. "chinese",
  460. "csgb2312",
  461. "csiso58gb231280",
  462. "gb2312",
  463. "gb_2312",
  464. "gb_2312-80",
  465. "gbk",
  466. "iso-ir-58",
  467. "x-gbk"
  468. ],
  469. "name": "GBK"
  470. },
  471. {
  472. "labels": [
  473. "gb18030"
  474. ],
  475. "name": "gb18030"
  476. }
  477. ],
  478. "heading": "Legacy multi-byte Chinese (simplified) encodings"
  479. },
  480. {
  481. "encodings": [{
  482. "labels": [
  483. "big5",
  484. "big5-hkscs",
  485. "cn-big5",
  486. "csbig5",
  487. "x-x-big5"
  488. ],
  489. "name": "Big5"
  490. }],
  491. "heading": "Legacy multi-byte Chinese (traditional) encodings"
  492. },
  493. {
  494. "encodings": [{
  495. "labels": [
  496. "cseucpkdfmtjapanese",
  497. "euc-jp",
  498. "x-euc-jp"
  499. ],
  500. "name": "EUC-JP"
  501. },
  502. {
  503. "labels": [
  504. "csiso2022jp",
  505. "iso-2022-jp"
  506. ],
  507. "name": "ISO-2022-JP"
  508. },
  509. {
  510. "labels": [
  511. "csshiftjis",
  512. "ms932",
  513. "ms_kanji",
  514. "shift-jis",
  515. "shift_jis",
  516. "sjis",
  517. "windows-31j",
  518. "x-sjis"
  519. ],
  520. "name": "Shift_JIS"
  521. }
  522. ],
  523. "heading": "Legacy multi-byte Japanese encodings"
  524. },
  525. {
  526. "encodings": [{
  527. "labels": [
  528. "cseuckr",
  529. "csksc56011987",
  530. "euc-kr",
  531. "iso-ir-149",
  532. "korean",
  533. "ks_c_5601-1987",
  534. "ks_c_5601-1989",
  535. "ksc5601",
  536. "ksc_5601",
  537. "windows-949"
  538. ],
  539. "name": "EUC-KR"
  540. }],
  541. "heading": "Legacy multi-byte Korean encodings"
  542. },
  543. {
  544. "encodings": [{
  545. "labels": [
  546. "csiso2022kr",
  547. "hz-gb-2312",
  548. "iso-2022-cn",
  549. "iso-2022-cn-ext",
  550. "iso-2022-kr"
  551. ],
  552. "name": "replacement"
  553. },
  554. {
  555. "labels": [
  556. "utf-16be"
  557. ],
  558. "name": "UTF-16BE"
  559. },
  560. {
  561. "labels": [
  562. "utf-16",
  563. "utf-16le"
  564. ],
  565. "name": "UTF-16LE"
  566. },
  567. {
  568. "labels": [
  569. "x-user-defined"
  570. ],
  571. "name": "x-user-defined"
  572. }
  573. ],
  574. "heading": "Legacy miscellaneous encodings"
  575. }
  576. ];
  577. /** @type {Object.<string,{name:string,labels:Array.<string>}>} */
  578. var label_to_encoding = {};
  579. encodings.forEach(function(category) {
  580. category.encodings.forEach(function(encoding) {
  581. encoding.labels.forEach(function(label) {
  582. label_to_encoding[label] = encoding;
  583. });
  584. });
  585. });
  586. /** @type {Object.<string, function({fatal:boolean}): Encoder>} */
  587. var encoders = {};
  588. /** @type {Object.<string, function({fatal:boolean}): Decoder>} */
  589. var decoders = {};
  590. function indexCodePointFor(pointer, index) {
  591. if (!index) return null;
  592. return index[pointer] || null;
  593. }
  594. function indexPointerFor(code_point, index) {
  595. var pointer = index.indexOf(code_point);
  596. return pointer === -1 ? null : pointer;
  597. }
  598. function index(name) {
  599. if (!('encoding-indexes' in global)) {
  600. throw Error("Indexes missing." +
  601. " Did you forget to include encoding-indexes.js first?");
  602. }
  603. return global['encoding-indexes'][name];
  604. }
  605. function indexGB18030RangesCodePointFor(pointer) {
  606. if ((pointer > 39419 && pointer < 189000) || (pointer > 1237575))
  607. return null;
  608. if (pointer === 7457) return 0xE7C7;
  609. var offset = 0;
  610. var code_point_offset = 0;
  611. var idx = index('gb18030-ranges');
  612. var i;
  613. for (i = 0; i < idx.length; ++i) {
  614. /** @type {!Array.<number>} */
  615. var entry = idx[i];
  616. if (entry[0] <= pointer) {
  617. offset = entry[0];
  618. code_point_offset = entry[1];
  619. } else {
  620. break;
  621. }
  622. }
  623. return code_point_offset + pointer - offset;
  624. }
  625. function indexGB18030RangesPointerFor(code_point) {
  626. if (code_point === 0xE7C7) return 7457;
  627. var offset = 0;
  628. var pointer_offset = 0;
  629. var idx = index('gb18030-ranges');
  630. var i;
  631. for (i = 0; i < idx.length; ++i) {
  632. /** @type {!Array.<number>} */
  633. var entry = idx[i];
  634. if (entry[1] <= code_point) {
  635. offset = entry[1];
  636. pointer_offset = entry[0];
  637. } else {
  638. break;
  639. }
  640. }
  641. return pointer_offset + code_point - offset;
  642. }
  643. function indexShiftJISPointerFor(code_point) {
  644. shift_jis_index = shift_jis_index ||
  645. index('jis0208').map(function(code_point, pointer) {
  646. return inRange(pointer, 8272, 8835) ? null : code_point;
  647. });
  648. var index_ = shift_jis_index;
  649. return index_.indexOf(code_point);
  650. }
  651. var shift_jis_index;
  652. function indexBig5PointerFor(code_point) {
  653. big5_index_no_hkscs = big5_index_no_hkscs ||
  654. index('big5').map(function(code_point, pointer) {
  655. return (pointer < (0xA1 - 0x81) * 157) ? null : code_point;
  656. });
  657. var index_ = big5_index_no_hkscs;
  658. if (code_point === 0x2550 || code_point === 0x255E ||
  659. code_point === 0x2561 || code_point === 0x256A ||
  660. code_point === 0x5341 || code_point === 0x5345) {
  661. return index_.lastIndexOf(code_point);
  662. }
  663. return indexPointerFor(code_point, index_);
  664. }
  665. var big5_index_no_hkscs;
  666. /** @const */
  667. var DEFAULT_ENCODING = 'utf-8';
  668. function TextDecoder(label, options) {
  669. // if (!(this instanceof TextDecoder))
  670. // throw TypeError('Called as a function. Did you forget \'new\'?');
  671. label = label !== undefined ? String(label) : DEFAULT_ENCODING;
  672. options = ToDictionary(options);
  673. /** @private */
  674. this._encoding = null;
  675. /** @private @type {?Decoder} */
  676. this._decoder = null;
  677. /** @private @type {boolean} */
  678. this._ignoreBOM = false;
  679. /** @private @type {boolean} */
  680. this._BOMseen = false;
  681. /** @private @type {string} */
  682. this._error_mode = 'replacement';
  683. /** @private @type {boolean} */
  684. this._do_not_flush = false;
  685. var encoding = getEncoding(label);
  686. if (encoding === null || encoding.name === 'replacement')
  687. throw RangeError('Unknown encoding: ' + label);
  688. if (!decoders[encoding.name]) {
  689. throw Error('Decoder not present.' +
  690. ' Did you forget to include encoding-indexes.js first?');
  691. }
  692. var dec = this;
  693. dec._encoding = encoding;
  694. if (Boolean(options['fatal']))
  695. dec._error_mode = 'fatal';
  696. if (Boolean(options['ignoreBOM']))
  697. dec._ignoreBOM = true;
  698. if (!Object.defineProperty) {
  699. this.encoding = dec._encoding.name.toLowerCase();
  700. this.fatal = dec._error_mode === 'fatal';
  701. this.ignoreBOM = dec._ignoreBOM;
  702. }
  703. return dec;
  704. }
  705. if (Object.defineProperty) {
  706. Object.defineProperty(TextDecoder.prototype, 'encoding', {
  707. /** @this {TextDecoder} */
  708. get: function() {
  709. return this._encoding.name.toLowerCase();
  710. }
  711. });
  712. Object.defineProperty(TextDecoder.prototype, 'fatal', {
  713. /** @this {TextDecoder} */
  714. get: function() {
  715. return this._error_mode === 'fatal';
  716. }
  717. });
  718. Object.defineProperty(TextDecoder.prototype, 'ignoreBOM', {
  719. /** @this {TextDecoder} */
  720. get: function() {
  721. return this._ignoreBOM;
  722. }
  723. });
  724. }
  725. TextDecoder.prototype.decode = function decode(input, options) {
  726. var bytes;
  727. if (typeof input === 'object' && input instanceof ArrayBuffer) {
  728. bytes = new Uint8Array(input);
  729. } else if (typeof input === 'object' && 'buffer' in input &&
  730. input.buffer instanceof ArrayBuffer) {
  731. bytes = new Uint8Array(input.buffer,
  732. input.byteOffset,
  733. input.byteLength);
  734. } else {
  735. bytes = new Uint8Array(0);
  736. }
  737. options = ToDictionary(options);
  738. if (!this._do_not_flush) {
  739. this._decoder = decoders[this._encoding.name]({
  740. fatal: this._error_mode === 'fatal'
  741. });
  742. this._BOMseen = false;
  743. }
  744. this._do_not_flush = Boolean(options['stream']);
  745. var input_stream = new Stream(bytes);
  746. var output = [];
  747. /** @type {?(number|!Array.<number>)} */
  748. var result;
  749. while (true) {
  750. var token = input_stream.read();
  751. if (token === end_of_stream)
  752. break;
  753. result = this._decoder.handler(input_stream, token);
  754. if (result === finished)
  755. break;
  756. if (result !== null) {
  757. if (Array.isArray(result))
  758. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  759. else
  760. output.push(result);
  761. }
  762. }
  763. if (!this._do_not_flush) {
  764. do {
  765. result = this._decoder.handler(input_stream, input_stream.read());
  766. if (result === finished)
  767. break;
  768. if (result === null)
  769. continue;
  770. if (Array.isArray(result))
  771. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  772. else
  773. output.push(result);
  774. } while (!input_stream.endOfStream());
  775. this._decoder = null;
  776. }
  777. function serializeStream(stream) {
  778. if (includes(['UTF-8', 'UTF-16LE', 'UTF-16BE'], this._encoding.name) &&
  779. !this._ignoreBOM && !this._BOMseen) {
  780. if (stream.length > 0 && stream[0] === 0xFEFF) {
  781. this._BOMseen = true;
  782. stream.shift();
  783. } else if (stream.length > 0) {
  784. this._BOMseen = true;
  785. } else {
  786. }
  787. }
  788. return codePointsToString(stream);
  789. }
  790. return serializeStream.call(this, output);
  791. };
  792. function TextEncoder(label, options) {
  793. // if (!(this instanceof TextEncoder))
  794. // throw TypeError('Called as a function. Did you forget \'new\'?');
  795. options = ToDictionary(options);
  796. /** @private */
  797. this._encoding = null;
  798. /** @private @type {?Encoder} */
  799. this._encoder = null;
  800. /** @private @type {boolean} */
  801. this._do_not_flush = false;
  802. /** @private @type {string} */
  803. this._fatal = Boolean(options['fatal']) ? 'fatal' : 'replacement';
  804. var enc = this;
  805. if (options['NONSTANDARD_allowLegacyEncoding']) {
  806. label = label !== undefined ? String(label) : DEFAULT_ENCODING;
  807. var encoding = getEncoding(label);
  808. if (encoding === null || encoding.name === 'replacement')
  809. throw RangeError('Unknown encoding: ' + label);
  810. if (!encoders[encoding.name]) {
  811. throw Error('Encoder not present.' +
  812. ' Did you forget to include encoding-indexes.js first?');
  813. }
  814. enc._encoding = encoding;
  815. } else {
  816. enc._encoding = getEncoding('utf-8');
  817. if (label !== undefined && 'console' in global) {
  818. console.warn('TextEncoder constructor called with encoding label, ' +
  819. 'which is ignored.');
  820. }
  821. }
  822. if (!Object.defineProperty)
  823. this.encoding = enc._encoding.name.toLowerCase();
  824. return enc;
  825. }
  826. if (Object.defineProperty) {
  827. Object.defineProperty(TextEncoder.prototype, 'encoding', {
  828. /** @this {TextEncoder} */
  829. get: function() {
  830. return this._encoding.name.toLowerCase();
  831. }
  832. });
  833. }
  834. function encode(opt_string, options) {
  835. opt_string = opt_string === undefined ? '' : String(opt_string);
  836. options = ToDictionary(options);
  837. if (!this._do_not_flush)
  838. this._encoder = encoders[this._encoding.name]({
  839. fatal: this._fatal === 'fatal'
  840. });
  841. this._do_not_flush = Boolean(options['stream']);
  842. var input = new Stream(stringToCodePoints(opt_string));
  843. var output = [];
  844. /** @type {?(number|!Array.<number>)} */
  845. var result;
  846. while (true) {
  847. var token = input.read();
  848. if (token === end_of_stream)
  849. break;
  850. result = this._encoder.handler(input, token);
  851. if (result === finished)
  852. break;
  853. if (Array.isArray(result))
  854. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  855. else
  856. output.push(result);
  857. }
  858. if (!this._do_not_flush) {
  859. while (true) {
  860. result = this._encoder.handler(input, input.read());
  861. if (result === finished)
  862. break;
  863. if (Array.isArray(result))
  864. output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  865. else
  866. output.push(result);
  867. }
  868. this._encoder = null;
  869. }
  870. return new Uint8Array(output);
  871. };
  872. // TextEncoder.prototype.encode = function encode(opt_string, options) {
  873. // opt_string = opt_string === undefined ? '' : String(opt_string);
  874. // options = ToDictionary(options);
  875. // if (!this._do_not_flush)
  876. // this._encoder = encoders[this._encoding.name]({
  877. // fatal: this._fatal === 'fatal'
  878. // });
  879. // this._do_not_flush = Boolean(options['stream']);
  880. // var input = new Stream(stringToCodePoints(opt_string));
  881. // var output = [];
  882. // /** @type {?(number|!Array.<number>)} */
  883. // var result;
  884. // while (true) {
  885. // var token = input.read();
  886. // if (token === end_of_stream)
  887. // break;
  888. // result = this._encoder.handler(input, token);
  889. // if (result === finished)
  890. // break;
  891. // if (Array.isArray(result))
  892. // output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  893. // else
  894. // output.push(result);
  895. // }
  896. // if (!this._do_not_flush) {
  897. // while (true) {
  898. // result = this._encoder.handler(input, input.read());
  899. // if (result === finished)
  900. // break;
  901. // if (Array.isArray(result))
  902. // output.push.apply(output, /**@type {!Array.<number>}*/ (result));
  903. // else
  904. // output.push(result);
  905. // }
  906. // this._encoder = null;
  907. // }
  908. // return new Uint8Array(output);
  909. // };
  910. function UTF8Decoder(options) {
  911. var fatal = options.fatal;
  912. var /** @type {number} */ utf8_code_point = 0,
  913. /** @type {number} */
  914. utf8_bytes_seen = 0,
  915. /** @type {number} */
  916. utf8_bytes_needed = 0,
  917. /** @type {number} */
  918. utf8_lower_boundary = 0x80,
  919. /** @type {number} */
  920. utf8_upper_boundary = 0xBF;
  921. this.handler = function(stream, bite) {
  922. if (bite === end_of_stream && utf8_bytes_needed !== 0) {
  923. utf8_bytes_needed = 0;
  924. return decoderError(fatal);
  925. }
  926. if (bite === end_of_stream)
  927. return finished;
  928. if (utf8_bytes_needed === 0) {
  929. if (inRange(bite, 0x00, 0x7F)) {
  930. return bite;
  931. } else if (inRange(bite, 0xC2, 0xDF)) {
  932. utf8_bytes_needed = 1;
  933. utf8_code_point = bite & 0x1F;
  934. } else if (inRange(bite, 0xE0, 0xEF)) {
  935. if (bite === 0xE0)
  936. utf8_lower_boundary = 0xA0;
  937. if (bite === 0xED)
  938. utf8_upper_boundary = 0x9F;
  939. utf8_bytes_needed = 2;
  940. utf8_code_point = bite & 0xF;
  941. } else if (inRange(bite, 0xF0, 0xF4)) {
  942. if (bite === 0xF0)
  943. utf8_lower_boundary = 0x90;
  944. if (bite === 0xF4)
  945. utf8_upper_boundary = 0x8F;
  946. utf8_bytes_needed = 3;
  947. utf8_code_point = bite & 0x7;
  948. } else {
  949. return decoderError(fatal);
  950. }
  951. return null;
  952. }
  953. if (!inRange(bite, utf8_lower_boundary, utf8_upper_boundary)) {
  954. utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
  955. utf8_lower_boundary = 0x80;
  956. utf8_upper_boundary = 0xBF;
  957. stream.prepend(bite);
  958. return decoderError(fatal);
  959. }
  960. utf8_lower_boundary = 0x80;
  961. utf8_upper_boundary = 0xBF;
  962. utf8_code_point = (utf8_code_point << 6) | (bite & 0x3F);
  963. utf8_bytes_seen += 1;
  964. if (utf8_bytes_seen !== utf8_bytes_needed)
  965. return null;
  966. var code_point = utf8_code_point;
  967. utf8_code_point = utf8_bytes_needed = utf8_bytes_seen = 0;
  968. return code_point;
  969. };
  970. }
  971. /**
  972. * @constructor
  973. * @implements {Encoder}
  974. * @param {{fatal: boolean}} options
  975. */
  976. function UTF8Encoder(options) {
  977. var fatal = options.fatal;
  978. /**
  979. * @param {Stream} stream Input stream.
  980. * @param {number} code_point Next code point read from the stream.
  981. * @return {(number|!Array.<number>)} Byte(s) to emit.
  982. */
  983. this.handler = function(stream, code_point) {
  984. if (code_point === end_of_stream)
  985. return finished;
  986. if (isASCIICodePoint(code_point))
  987. return code_point;
  988. var count, offset;
  989. if (inRange(code_point, 0x0080, 0x07FF)) {
  990. count = 1;
  991. offset = 0xC0;
  992. } else if (inRange(code_point, 0x0800, 0xFFFF)) {
  993. count = 2;
  994. offset = 0xE0;
  995. } else if (inRange(code_point, 0x10000, 0x10FFFF)) {
  996. count = 3;
  997. offset = 0xF0;
  998. }
  999. var bytes = [(code_point >> (6 * count)) + offset];
  1000. while (count > 0) {
  1001. var temp = code_point >> (6 * (count - 1));
  1002. bytes.push(0x80 | (temp & 0x3F));
  1003. count -= 1;
  1004. }
  1005. return bytes;
  1006. };
  1007. }
  1008. /** @param {{fatal: boolean}} options */
  1009. encoders['UTF-8'] = function(options) {
  1010. return new UTF8Encoder(options);
  1011. };
  1012. /** @param {{fatal: boolean}} options */
  1013. decoders['UTF-8'] = function(options) {
  1014. return new UTF8Decoder(options);
  1015. };
  1016. /**
  1017. * @constructor
  1018. * @implements {Decoder}
  1019. * @param {!Array.<number>} index The encoding index.
  1020. * @param {{fatal: boolean}} options
  1021. */
  1022. function SingleByteDecoder(index, options) {
  1023. var fatal = options.fatal;
  1024. this.handler = function(stream, bite) {
  1025. if (bite === end_of_stream)
  1026. return finished;
  1027. if (isASCIIByte(bite))
  1028. return bite;
  1029. var code_point = index[bite - 0x80];
  1030. if (code_point === null)
  1031. return decoderError(fatal);
  1032. return code_point;
  1033. };
  1034. }
  1035. /**
  1036. * @constructor
  1037. * @implements {Encoder}
  1038. * @param {!Array.<?number>} index The encoding index.
  1039. * @param {{fatal: boolean}} options
  1040. */
  1041. function SingleByteEncoder(index, options) {
  1042. var fatal = options.fatal;
  1043. this.handler = function(stream, code_point) {
  1044. if (code_point === end_of_stream)
  1045. return finished;
  1046. if (isASCIICodePoint(code_point))
  1047. return code_point;
  1048. var pointer = indexPointerFor(code_point, index);
  1049. if (pointer === null)
  1050. encoderError(code_point);
  1051. return pointer + 0x80;
  1052. };
  1053. }
  1054. (function() {
  1055. if (!('encoding-indexes' in global))
  1056. return;
  1057. encodings.forEach(function(category) {
  1058. if (category.heading !== 'Legacy single-byte encodings')
  1059. return;
  1060. category.encodings.forEach(function(encoding) {
  1061. var name = encoding.name;
  1062. var idx = index(name.toLowerCase());
  1063. /** @param {{fatal: boolean}} options */
  1064. decoders[name] = function(options) {
  1065. return new SingleByteDecoder(idx, options);
  1066. };
  1067. /** @param {{fatal: boolean}} options */
  1068. encoders[name] = function(options) {
  1069. return new SingleByteEncoder(idx, options);
  1070. };
  1071. });
  1072. });
  1073. }());
  1074. /** @param {{fatal: boolean}} options */
  1075. decoders['GBK'] = function(options) {
  1076. return new GB18030Decoder(options);
  1077. };
  1078. /** @param {{fatal: boolean}} options */
  1079. encoders['GBK'] = function(options) {
  1080. return new GB18030Encoder(options, true);
  1081. };
  1082. /**
  1083. * @constructor
  1084. * @implements {Decoder}
  1085. * @param {{fatal: boolean}} options
  1086. */
  1087. function GB18030Decoder(options) {
  1088. var fatal = options.fatal;
  1089. var /** @type {number} */ gb18030_first = 0x00,
  1090. /** @type {number} */
  1091. gb18030_second = 0x00,
  1092. /** @type {number} */
  1093. gb18030_third = 0x00;
  1094. this.handler = function(stream, bite) {
  1095. if (bite === end_of_stream && gb18030_first === 0x00 &&
  1096. gb18030_second === 0x00 && gb18030_third === 0x00) {
  1097. return finished;
  1098. }
  1099. if (bite === end_of_stream &&
  1100. (gb18030_first !== 0x00 || gb18030_second !== 0x00 ||
  1101. gb18030_third !== 0x00)) {
  1102. gb18030_first = 0x00;
  1103. gb18030_second = 0x00;
  1104. gb18030_third = 0x00;
  1105. decoderError(fatal);
  1106. }
  1107. var code_point;
  1108. if (gb18030_third !== 0x00) {
  1109. code_point = null;
  1110. if (inRange(bite, 0x30, 0x39)) {
  1111. code_point = indexGB18030RangesCodePointFor(
  1112. (((gb18030_first - 0x81) * 10 + gb18030_second - 0x30) * 126 +
  1113. gb18030_third - 0x81) * 10 + bite - 0x30);
  1114. }
  1115. var buffer = [gb18030_second, gb18030_third, bite];
  1116. gb18030_first = 0x00;
  1117. gb18030_second = 0x00;
  1118. gb18030_third = 0x00;
  1119. if (code_point === null) {
  1120. stream.prepend(buffer);
  1121. return decoderError(fatal);
  1122. }
  1123. return code_point;
  1124. }
  1125. if (gb18030_second !== 0x00) {
  1126. if (inRange(bite, 0x81, 0xFE)) {
  1127. gb18030_third = bite;
  1128. return null;
  1129. }
  1130. stream.prepend([gb18030_second, bite]);
  1131. gb18030_first = 0x00;
  1132. gb18030_second = 0x00;
  1133. return decoderError(fatal);
  1134. }
  1135. if (gb18030_first !== 0x00) {
  1136. if (inRange(bite, 0x30, 0x39)) {
  1137. gb18030_second = bite;
  1138. return null;
  1139. }
  1140. var lead = gb18030_first;
  1141. var pointer = null;
  1142. gb18030_first = 0x00;
  1143. var offset = bite < 0x7F ? 0x40 : 0x41;
  1144. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFE))
  1145. pointer = (lead - 0x81) * 190 + (bite - offset);
  1146. code_point = pointer === null ? null :
  1147. indexCodePointFor(pointer, index('gb18030'));
  1148. if (code_point === null && isASCIIByte(bite))
  1149. stream.prepend(bite);
  1150. if (code_point === null)
  1151. return decoderError(fatal);
  1152. return code_point;
  1153. }
  1154. if (isASCIIByte(bite))
  1155. return bite;
  1156. if (bite === 0x80)
  1157. return 0x20AC;
  1158. if (inRange(bite, 0x81, 0xFE)) {
  1159. gb18030_first = bite;
  1160. return null;
  1161. }
  1162. return decoderError(fatal);
  1163. };
  1164. }
  1165. /**
  1166. * @constructor
  1167. * @implements {Encoder}
  1168. * @param {{fatal: boolean}} options
  1169. * @param {boolean=} gbk_flag
  1170. */
  1171. function GB18030Encoder(options, gbk_flag) {
  1172. var fatal = options.fatal;
  1173. /**
  1174. * @param {Stream} stream Input stream.
  1175. * @param {number} code_point Next code point read from the stream.
  1176. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1177. */
  1178. this.handler = function(stream, code_point) {
  1179. if (code_point === end_of_stream)
  1180. return finished;
  1181. if (isASCIICodePoint(code_point))
  1182. return code_point;
  1183. if (code_point === 0xE5E5)
  1184. return encoderError(code_point);
  1185. if (gbk_flag && code_point === 0x20AC)
  1186. return 0x80;
  1187. var pointer = indexPointerFor(code_point, index('gb18030'));
  1188. if (pointer !== null) {
  1189. var lead = floor(pointer / 190) + 0x81;
  1190. var trail = pointer % 190;
  1191. var offset = trail < 0x3F ? 0x40 : 0x41;
  1192. return [lead, trail + offset];
  1193. }
  1194. if (gbk_flag)
  1195. return encoderError(code_point);
  1196. pointer = indexGB18030RangesPointerFor(code_point);
  1197. var byte1 = floor(pointer / 10 / 126 / 10);
  1198. pointer = pointer - byte1 * 10 * 126 * 10;
  1199. var byte2 = floor(pointer / 10 / 126);
  1200. pointer = pointer - byte2 * 10 * 126;
  1201. var byte3 = floor(pointer / 10);
  1202. var byte4 = pointer - byte3 * 10;
  1203. return [byte1 + 0x81,
  1204. byte2 + 0x30,
  1205. byte3 + 0x81,
  1206. byte4 + 0x30
  1207. ];
  1208. };
  1209. }
  1210. /** @param {{fatal: boolean}} options */
  1211. encoders['gb18030'] = function(options) {
  1212. return new GB18030Encoder(options);
  1213. };
  1214. /** @param {{fatal: boolean}} options */
  1215. decoders['gb18030'] = function(options) {
  1216. return new GB18030Decoder(options);
  1217. };
  1218. /**
  1219. * @constructor
  1220. * @implements {Decoder}
  1221. * @param {{fatal: boolean}} options
  1222. */
  1223. function Big5Decoder(options) {
  1224. var fatal = options.fatal;
  1225. var /** @type {number} */ Big5_lead = 0x00;
  1226. this.handler = function(stream, bite) {
  1227. if (bite === end_of_stream && Big5_lead !== 0x00) {
  1228. Big5_lead = 0x00;
  1229. return decoderError(fatal);
  1230. }
  1231. if (bite === end_of_stream && Big5_lead === 0x00)
  1232. return finished;
  1233. if (Big5_lead !== 0x00) {
  1234. var lead = Big5_lead;
  1235. var pointer = null;
  1236. Big5_lead = 0x00;
  1237. var offset = bite < 0x7F ? 0x40 : 0x62;
  1238. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0xA1, 0xFE))
  1239. pointer = (lead - 0x81) * 157 + (bite - offset);
  1240. switch (pointer) {
  1241. case 1133:
  1242. return [0x00CA, 0x0304];
  1243. case 1135:
  1244. return [0x00CA, 0x030C];
  1245. case 1164:
  1246. return [0x00EA, 0x0304];
  1247. case 1166:
  1248. return [0x00EA, 0x030C];
  1249. }
  1250. var code_point = (pointer === null) ? null :
  1251. indexCodePointFor(pointer, index('big5'));
  1252. if (code_point === null && isASCIIByte(bite))
  1253. stream.prepend(bite);
  1254. if (code_point === null)
  1255. return decoderError(fatal);
  1256. return code_point;
  1257. }
  1258. if (isASCIIByte(bite))
  1259. return bite;
  1260. if (inRange(bite, 0x81, 0xFE)) {
  1261. Big5_lead = bite;
  1262. return null;
  1263. }
  1264. return decoderError(fatal);
  1265. };
  1266. }
  1267. /**
  1268. * @constructor
  1269. * @implements {Encoder}
  1270. * @param {{fatal: boolean}} options
  1271. */
  1272. function Big5Encoder(options) {
  1273. var fatal = options.fatal;
  1274. this.handler = function(stream, code_point) {
  1275. if (code_point === end_of_stream)
  1276. return finished;
  1277. if (isASCIICodePoint(code_point))
  1278. return code_point;
  1279. var pointer = indexBig5PointerFor(code_point);
  1280. if (pointer === null)
  1281. return encoderError(code_point);
  1282. var lead = floor(pointer / 157) + 0x81;
  1283. if (lead < 0xA1)
  1284. return encoderError(code_point);
  1285. var trail = pointer % 157;
  1286. var offset = trail < 0x3F ? 0x40 : 0x62;
  1287. return [lead, trail + offset];
  1288. };
  1289. }
  1290. /** @param {{fatal: boolean}} options */
  1291. encoders['Big5'] = function(options) {
  1292. return new Big5Encoder(options);
  1293. };
  1294. /** @param {{fatal: boolean}} options */
  1295. decoders['Big5'] = function(options) {
  1296. return new Big5Decoder(options);
  1297. };
  1298. /**
  1299. * @constructor
  1300. * @implements {Decoder}
  1301. * @param {{fatal: boolean}} options
  1302. */
  1303. function EUCJPDecoder(options) {
  1304. var fatal = options.fatal;
  1305. var /** @type {boolean} */ eucjp_jis0212_flag = false,
  1306. /** @type {number} */
  1307. eucjp_lead = 0x00;
  1308. this.handler = function(stream, bite) {
  1309. if (bite === end_of_stream && eucjp_lead !== 0x00) {
  1310. eucjp_lead = 0x00;
  1311. return decoderError(fatal);
  1312. }
  1313. if (bite === end_of_stream && eucjp_lead === 0x00)
  1314. return finished;
  1315. if (eucjp_lead === 0x8E && inRange(bite, 0xA1, 0xDF)) {
  1316. eucjp_lead = 0x00;
  1317. return 0xFF61 - 0xA1 + bite;
  1318. }
  1319. if (eucjp_lead === 0x8F && inRange(bite, 0xA1, 0xFE)) {
  1320. eucjp_jis0212_flag = true;
  1321. eucjp_lead = bite;
  1322. return null;
  1323. }
  1324. if (eucjp_lead !== 0x00) {
  1325. var lead = eucjp_lead;
  1326. eucjp_lead = 0x00;
  1327. var code_point = null;
  1328. if (inRange(lead, 0xA1, 0xFE) && inRange(bite, 0xA1, 0xFE)) {
  1329. code_point = indexCodePointFor(
  1330. (lead - 0xA1) * 94 + (bite - 0xA1),
  1331. index(!eucjp_jis0212_flag ? 'jis0208' : 'jis0212'));
  1332. }
  1333. eucjp_jis0212_flag = false;
  1334. if (!inRange(bite, 0xA1, 0xFE))
  1335. stream.prepend(bite);
  1336. if (code_point === null)
  1337. return decoderError(fatal);
  1338. return code_point;
  1339. }
  1340. if (isASCIIByte(bite))
  1341. return bite;
  1342. if (bite === 0x8E || bite === 0x8F || inRange(bite, 0xA1, 0xFE)) {
  1343. eucjp_lead = bite;
  1344. return null;
  1345. }
  1346. return decoderError(fatal);
  1347. };
  1348. }
  1349. /**
  1350. * @constructor
  1351. * @implements {Encoder}
  1352. * @param {{fatal: boolean}} options
  1353. */
  1354. function EUCJPEncoder(options) {
  1355. var fatal = options.fatal;
  1356. this.handler = function(stream, code_point) {
  1357. if (code_point === end_of_stream)
  1358. return finished;
  1359. if (isASCIICodePoint(code_point))
  1360. return code_point;
  1361. if (code_point === 0x00A5)
  1362. return 0x5C;
  1363. if (code_point === 0x203E)
  1364. return 0x7E;
  1365. if (inRange(code_point, 0xFF61, 0xFF9F))
  1366. return [0x8E, code_point - 0xFF61 + 0xA1];
  1367. if (code_point === 0x2212)
  1368. code_point = 0xFF0D;
  1369. var pointer = indexPointerFor(code_point, index('jis0208'));
  1370. if (pointer === null)
  1371. return encoderError(code_point);
  1372. var lead = floor(pointer / 94) + 0xA1;
  1373. var trail = pointer % 94 + 0xA1;
  1374. return [lead, trail];
  1375. };
  1376. }
  1377. /** @param {{fatal: boolean}} options */
  1378. encoders['EUC-JP'] = function(options) {
  1379. return new EUCJPEncoder(options);
  1380. };
  1381. /** @param {{fatal: boolean}} options */
  1382. decoders['EUC-JP'] = function(options) {
  1383. return new EUCJPDecoder(options);
  1384. };
  1385. /**
  1386. * @constructor
  1387. * @implements {Decoder}
  1388. * @param {{fatal: boolean}} options
  1389. */
  1390. function ISO2022JPDecoder(options) {
  1391. var fatal = options.fatal;
  1392. /** @enum */
  1393. var states = {
  1394. ASCII: 0,
  1395. Roman: 1,
  1396. Katakana: 2,
  1397. LeadByte: 3,
  1398. TrailByte: 4,
  1399. EscapeStart: 5,
  1400. Escape: 6
  1401. };
  1402. var /** @type {number} */ iso2022jp_decoder_state = states.ASCII,
  1403. /** @type {number} */
  1404. iso2022jp_decoder_output_state = states.ASCII,
  1405. /** @type {number} */
  1406. iso2022jp_lead = 0x00,
  1407. /** @type {boolean} */
  1408. iso2022jp_output_flag = false;
  1409. this.handler = function(stream, bite) {
  1410. switch (iso2022jp_decoder_state) {
  1411. default:
  1412. case states.ASCII:
  1413. if (bite === 0x1B) {
  1414. iso2022jp_decoder_state = states.EscapeStart;
  1415. return null;
  1416. }
  1417. if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E &&
  1418. bite !== 0x0F && bite !== 0x1B) {
  1419. iso2022jp_output_flag = false;
  1420. return bite;
  1421. }
  1422. if (bite === end_of_stream) {
  1423. return finished;
  1424. }
  1425. iso2022jp_output_flag = false;
  1426. return decoderError(fatal);
  1427. case states.Roman:
  1428. if (bite === 0x1B) {
  1429. iso2022jp_decoder_state = states.EscapeStart;
  1430. return null;
  1431. }
  1432. if (bite === 0x5C) {
  1433. iso2022jp_output_flag = false;
  1434. return 0x00A5;
  1435. }
  1436. if (bite === 0x7E) {
  1437. iso2022jp_output_flag = false;
  1438. return 0x203E;
  1439. }
  1440. if (inRange(bite, 0x00, 0x7F) && bite !== 0x0E && bite !== 0x0F &&
  1441. bite !== 0x1B && bite !== 0x5C && bite !== 0x7E) {
  1442. iso2022jp_output_flag = false;
  1443. return bite;
  1444. }
  1445. if (bite === end_of_stream) {
  1446. return finished;
  1447. }
  1448. iso2022jp_output_flag = false;
  1449. return decoderError(fatal);
  1450. case states.Katakana:
  1451. if (bite === 0x1B) {
  1452. iso2022jp_decoder_state = states.EscapeStart;
  1453. return null;
  1454. }
  1455. if (inRange(bite, 0x21, 0x5F)) {
  1456. iso2022jp_output_flag = false;
  1457. return 0xFF61 - 0x21 + bite;
  1458. }
  1459. if (bite === end_of_stream) {
  1460. return finished;
  1461. }
  1462. iso2022jp_output_flag = false;
  1463. return decoderError(fatal);
  1464. case states.LeadByte:
  1465. if (bite === 0x1B) {
  1466. iso2022jp_decoder_state = states.EscapeStart;
  1467. return null;
  1468. }
  1469. if (inRange(bite, 0x21, 0x7E)) {
  1470. iso2022jp_output_flag = false;
  1471. iso2022jp_lead = bite;
  1472. iso2022jp_decoder_state = states.TrailByte;
  1473. return null;
  1474. }
  1475. if (bite === end_of_stream) {
  1476. return finished;
  1477. }
  1478. iso2022jp_output_flag = false;
  1479. return decoderError(fatal);
  1480. case states.TrailByte:
  1481. if (bite === 0x1B) {
  1482. iso2022jp_decoder_state = states.EscapeStart;
  1483. return decoderError(fatal);
  1484. }
  1485. if (inRange(bite, 0x21, 0x7E)) {
  1486. iso2022jp_decoder_state = states.LeadByte;
  1487. var pointer = (iso2022jp_lead - 0x21) * 94 + bite - 0x21;
  1488. var code_point = indexCodePointFor(pointer, index('jis0208'));
  1489. if (code_point === null)
  1490. return decoderError(fatal);
  1491. return code_point;
  1492. }
  1493. if (bite === end_of_stream) {
  1494. iso2022jp_decoder_state = states.LeadByte;
  1495. stream.prepend(bite);
  1496. return decoderError(fatal);
  1497. }
  1498. iso2022jp_decoder_state = states.LeadByte;
  1499. return decoderError(fatal);
  1500. case states.EscapeStart:
  1501. if (bite === 0x24 || bite === 0x28) {
  1502. iso2022jp_lead = bite;
  1503. iso2022jp_decoder_state = states.Escape;
  1504. return null;
  1505. }
  1506. stream.prepend(bite);
  1507. iso2022jp_output_flag = false;
  1508. iso2022jp_decoder_state = iso2022jp_decoder_output_state;
  1509. return decoderError(fatal);
  1510. case states.Escape:
  1511. var lead = iso2022jp_lead;
  1512. iso2022jp_lead = 0x00;
  1513. var state = null;
  1514. if (lead === 0x28 && bite === 0x42)
  1515. state = states.ASCII;
  1516. if (lead === 0x28 && bite === 0x4A)
  1517. state = states.Roman;
  1518. if (lead === 0x28 && bite === 0x49)
  1519. state = states.Katakana;
  1520. if (lead === 0x24 && (bite === 0x40 || bite === 0x42))
  1521. state = states.LeadByte;
  1522. if (state !== null) {
  1523. iso2022jp_decoder_state = iso2022jp_decoder_state = state;
  1524. var output_flag = iso2022jp_output_flag;
  1525. iso2022jp_output_flag = true;
  1526. return !output_flag ? null : decoderError(fatal);
  1527. }
  1528. stream.prepend([lead, bite]);
  1529. iso2022jp_output_flag = false;
  1530. iso2022jp_decoder_state = iso2022jp_decoder_output_state;
  1531. return decoderError(fatal);
  1532. }
  1533. };
  1534. }
  1535. /**
  1536. * @constructor
  1537. * @implements {Encoder}
  1538. * @param {{fatal: boolean}} options
  1539. */
  1540. function ISO2022JPEncoder(options) {
  1541. var fatal = options.fatal;
  1542. /** @enum */
  1543. var states = {
  1544. ASCII: 0,
  1545. Roman: 1,
  1546. jis0208: 2
  1547. };
  1548. var /** @type {number} */ iso2022jp_state = states.ASCII;
  1549. /**
  1550. * @param {Stream} stream Input stream.
  1551. * @param {number} code_point Next code point read from the stream.
  1552. * @return {(number|!Array.<number>)} Byte(s) to emit.
  1553. */
  1554. this.handler = function(stream, code_point) {
  1555. if (code_point === end_of_stream &&
  1556. iso2022jp_state !== states.ASCII) {
  1557. stream.prepend(code_point);
  1558. iso2022jp_state = states.ASCII;
  1559. return [0x1B, 0x28, 0x42];
  1560. }
  1561. if (code_point === end_of_stream && iso2022jp_state === states.ASCII)
  1562. return finished;
  1563. if ((iso2022jp_state === states.ASCII ||
  1564. iso2022jp_state === states.Roman) &&
  1565. (code_point === 0x000E || code_point === 0x000F ||
  1566. code_point === 0x001B)) {
  1567. return encoderError(0xFFFD);
  1568. }
  1569. if (iso2022jp_state === states.ASCII &&
  1570. isASCIICodePoint(code_point))
  1571. return code_point;
  1572. if (iso2022jp_state === states.Roman &&
  1573. ((isASCIICodePoint(code_point) &&
  1574. code_point !== 0x005C && code_point !== 0x007E) ||
  1575. (code_point == 0x00A5 || code_point == 0x203E))) {
  1576. if (isASCIICodePoint(code_point))
  1577. return code_point;
  1578. if (code_point === 0x00A5)
  1579. return 0x5C;
  1580. if (code_point === 0x203E)
  1581. return 0x7E;
  1582. }
  1583. if (isASCIICodePoint(code_point) &&
  1584. iso2022jp_state !== states.ASCII) {
  1585. stream.prepend(code_point);
  1586. iso2022jp_state = states.ASCII;
  1587. return [0x1B, 0x28, 0x42];
  1588. }
  1589. if ((code_point === 0x00A5 || code_point === 0x203E) &&
  1590. iso2022jp_state !== states.Roman) {
  1591. stream.prepend(code_point);
  1592. iso2022jp_state = states.Roman;
  1593. return [0x1B, 0x28, 0x4A];
  1594. }
  1595. if (code_point === 0x2212)
  1596. code_point = 0xFF0D;
  1597. var pointer = indexPointerFor(code_point, index('jis0208'));
  1598. if (pointer === null)
  1599. return encoderError(code_point);
  1600. if (iso2022jp_state !== states.jis0208) {
  1601. stream.prepend(code_point);
  1602. iso2022jp_state = states.jis0208;
  1603. return [0x1B, 0x24, 0x42];
  1604. }
  1605. var lead = floor(pointer / 94) + 0x21;
  1606. var trail = pointer % 94 + 0x21;
  1607. return [lead, trail];
  1608. };
  1609. }
  1610. /** @param {{fatal: boolean}} options */
  1611. encoders['ISO-2022-JP'] = function(options) {
  1612. return new ISO2022JPEncoder(options);
  1613. };
  1614. /** @param {{fatal: boolean}} options */
  1615. decoders['ISO-2022-JP'] = function(options) {
  1616. return new ISO2022JPDecoder(options);
  1617. };
  1618. /**
  1619. * @constructor
  1620. * @implements {Decoder}
  1621. * @param {{fatal: boolean}} options
  1622. */
  1623. function ShiftJISDecoder(options) {
  1624. var fatal = options.fatal;
  1625. var /** @type {number} */ Shift_JIS_lead = 0x00;
  1626. this.handler = function(stream, bite) {
  1627. if (bite === end_of_stream && Shift_JIS_lead !== 0x00) {
  1628. Shift_JIS_lead = 0x00;
  1629. return decoderError(fatal);
  1630. }
  1631. if (bite === end_of_stream && Shift_JIS_lead === 0x00)
  1632. return finished;
  1633. if (Shift_JIS_lead !== 0x00) {
  1634. var lead = Shift_JIS_lead;
  1635. var pointer = null;
  1636. Shift_JIS_lead = 0x00;
  1637. var offset = (bite < 0x7F) ? 0x40 : 0x41;
  1638. var lead_offset = (lead < 0xA0) ? 0x81 : 0xC1;
  1639. if (inRange(bite, 0x40, 0x7E) || inRange(bite, 0x80, 0xFC))
  1640. pointer = (lead - lead_offset) * 188 + bite - offset;
  1641. if (inRange(pointer, 8836, 10715))
  1642. return 0xE000 - 8836 + pointer;
  1643. var code_point = (pointer === null) ? null :
  1644. indexCodePointFor(pointer, index('jis0208'));
  1645. if (code_point === null && isASCIIByte(bite))
  1646. stream.prepend(bite);
  1647. if (code_point === null)
  1648. return decoderError(fatal);
  1649. return code_point;
  1650. }
  1651. if (isASCIIByte(bite) || bite === 0x80)
  1652. return bite;
  1653. if (inRange(bite, 0xA1, 0xDF))
  1654. return 0xFF61 - 0xA1 + bite;
  1655. if (inRange(bite, 0x81, 0x9F) || inRange(bite, 0xE0, 0xFC)) {
  1656. Shift_JIS_lead = bite;
  1657. return null;
  1658. }
  1659. return decoderError(fatal);
  1660. };
  1661. }
  1662. /**
  1663. * @constructor
  1664. * @implements {Encoder}
  1665. * @param {{fatal: boolean}} options
  1666. */
  1667. function ShiftJISEncoder(options) {
  1668. var fatal = options.fatal;
  1669. this.handler = function(stream, code_point) {
  1670. if (code_point === end_of_stream)
  1671. return finished;
  1672. if (isASCIICodePoint(code_point) || code_point === 0x0080)
  1673. return code_point;
  1674. if (code_point === 0x00A5)
  1675. return 0x5C;
  1676. if (code_point === 0x203E)
  1677. return 0x7E;
  1678. if (inRange(code_point, 0xFF61, 0xFF9F))
  1679. return code_point - 0xFF61 + 0xA1;
  1680. if (code_point === 0x2212)
  1681. code_point = 0xFF0D;
  1682. var pointer = indexShiftJISPointerFor(code_point);
  1683. if (pointer === null)
  1684. return encoderError(code_point);
  1685. var lead = floor(pointer / 188);
  1686. var lead_offset = (lead < 0x1F) ? 0x81 : 0xC1;
  1687. var trail = pointer % 188;
  1688. var offset = (trail < 0x3F) ? 0x40 : 0x41;
  1689. return [lead + lead_offset, trail + offset];
  1690. };
  1691. }
  1692. /** @param {{fatal: boolean}} options */
  1693. encoders['Shift_JIS'] = function(options) {
  1694. return new ShiftJISEncoder(options);
  1695. };
  1696. /** @param {{fatal: boolean}} options */
  1697. decoders['Shift_JIS'] = function(options) {
  1698. return new ShiftJISDecoder(options);
  1699. };
  1700. /**
  1701. * @constructor
  1702. * @implements {Decoder}
  1703. * @param {{fatal: boolean}} options
  1704. */
  1705. function EUCKRDecoder(options) {
  1706. var fatal = options.fatal;
  1707. var /** @type {number} */ euckr_lead = 0x00;
  1708. this.handler = function(stream, bite) {
  1709. if (bite === end_of_stream && euckr_lead !== 0) {
  1710. euckr_lead = 0x00;
  1711. return decoderError(fatal);
  1712. }
  1713. if (bite === end_of_stream && euckr_lead === 0)
  1714. return finished;
  1715. if (euckr_lead !== 0x00) {
  1716. var lead = euckr_lead;
  1717. var pointer = null;
  1718. euckr_lead = 0x00;
  1719. if (inRange(bite, 0x41, 0xFE))
  1720. pointer = (lead - 0x81) * 190 + (bite - 0x41);
  1721. var code_point = (pointer === null) ?
  1722. null : indexCodePointFor(pointer, index('euc-kr'));
  1723. if (pointer === null && isASCIIByte(bite))
  1724. stream.prepend(bite);
  1725. if (code_point === null)
  1726. return decoderError(fatal);
  1727. return code_point;
  1728. }
  1729. if (isASCIIByte(bite))
  1730. return bite;
  1731. if (inRange(bite, 0x81, 0xFE)) {
  1732. euckr_lead = bite;
  1733. return null;
  1734. }
  1735. return decoderError(fatal);
  1736. };
  1737. }
  1738. /**
  1739. * @constructor
  1740. * @implements {Encoder}
  1741. * @param {{fatal: boolean}} options
  1742. */
  1743. function EUCKREncoder(options) {
  1744. var fatal = options.fatal;
  1745. this.handler = function(stream, code_point) {
  1746. if (code_point === end_of_stream)
  1747. return finished;
  1748. if (isASCIICodePoint(code_point))
  1749. return code_point;
  1750. var pointer = indexPointerFor(code_point, index('euc-kr'));
  1751. if (pointer === null)
  1752. return encoderError(code_point);
  1753. var lead = floor(pointer / 190) + 0x81;
  1754. var trail = (pointer % 190) + 0x41;
  1755. return [lead, trail];
  1756. };
  1757. }
  1758. /** @param {{fatal: boolean}} options */
  1759. encoders['EUC-KR'] = function(options) {
  1760. return new EUCKREncoder(options);
  1761. };
  1762. /** @param {{fatal: boolean}} options */
  1763. decoders['EUC-KR'] = function(options) {
  1764. return new EUCKRDecoder(options);
  1765. };
  1766. /**
  1767. * @param {number} code_unit
  1768. * @param {boolean} utf16be
  1769. * @return {!Array.<number>} bytes
  1770. */
  1771. function convertCodeUnitToBytes(code_unit, utf16be) {
  1772. var byte1 = code_unit >> 8;
  1773. var byte2 = code_unit & 0x00FF;
  1774. if (utf16be)
  1775. return [byte1, byte2];
  1776. return [byte2, byte1];
  1777. }
  1778. function UTF16Decoder(utf16_be, options) {
  1779. var fatal = options.fatal;
  1780. var /** @type {?number} */ utf16_lead_byte = null,
  1781. /** @type {?number} */
  1782. utf16_lead_surrogate = null;
  1783. this.handler = function(stream, bite) {
  1784. if (bite === end_of_stream && (utf16_lead_byte !== null ||
  1785. utf16_lead_surrogate !== null)) {
  1786. return decoderError(fatal);
  1787. }
  1788. if (bite === end_of_stream && utf16_lead_byte === null &&
  1789. utf16_lead_surrogate === null) {
  1790. return finished;
  1791. }
  1792. if (utf16_lead_byte === null) {
  1793. utf16_lead_byte = bite;
  1794. return null;
  1795. }
  1796. var code_unit;
  1797. if (utf16_be) {
  1798. code_unit = (utf16_lead_byte << 8) + bite;
  1799. } else {
  1800. code_unit = (bite << 8) + utf16_lead_byte;
  1801. }
  1802. utf16_lead_byte = null;
  1803. if (utf16_lead_surrogate !== null) {
  1804. var lead_surrogate = utf16_lead_surrogate;
  1805. utf16_lead_surrogate = null;
  1806. if (inRange(code_unit, 0xDC00, 0xDFFF)) {
  1807. return 0x10000 + (lead_surrogate - 0xD800) * 0x400 +
  1808. (code_unit - 0xDC00);
  1809. }
  1810. stream.prepend(convertCodeUnitToBytes(code_unit, utf16_be));
  1811. return decoderError(fatal);
  1812. }
  1813. if (inRange(code_unit, 0xD800, 0xDBFF)) {
  1814. utf16_lead_surrogate = code_unit;
  1815. return null;
  1816. }
  1817. if (inRange(code_unit, 0xDC00, 0xDFFF))
  1818. return decoderError(fatal);
  1819. return code_unit;
  1820. };
  1821. }
  1822. function UTF16Encoder(utf16_be, options) {
  1823. var fatal = options.fatal;
  1824. this.handler = function(stream, code_point) {
  1825. if (code_point === end_of_stream)
  1826. return finished;
  1827. if (inRange(code_point, 0x0000, 0xFFFF))
  1828. return convertCodeUnitToBytes(code_point, utf16_be);
  1829. var lead = convertCodeUnitToBytes(
  1830. ((code_point - 0x10000) >> 10) + 0xD800, utf16_be);
  1831. var trail = convertCodeUnitToBytes(
  1832. ((code_point - 0x10000) & 0x3FF) + 0xDC00, utf16_be);
  1833. return lead.concat(trail);
  1834. };
  1835. }
  1836. /** @param {{fatal: boolean}} options */
  1837. encoders['UTF-16BE'] = function(options) {
  1838. return new UTF16Encoder(true, options);
  1839. };
  1840. /** @param {{fatal: boolean}} options */
  1841. decoders['UTF-16BE'] = function(options) {
  1842. return new UTF16Decoder(true, options);
  1843. };
  1844. /** @param {{fatal: boolean}} options */
  1845. encoders['UTF-16LE'] = function(options) {
  1846. return new UTF16Encoder(false, options);
  1847. };
  1848. /** @param {{fatal: boolean}} options */
  1849. decoders['UTF-16LE'] = function(options) {
  1850. return new UTF16Decoder(false, options);
  1851. };
  1852. /**
  1853. * @constructor
  1854. * @implements {Decoder}
  1855. * @param {{fatal: boolean}} options
  1856. */
  1857. function XUserDefinedDecoder(options) {
  1858. var fatal = options.fatal;
  1859. this.handler = function(stream, bite) {
  1860. if (bite === end_of_stream)
  1861. return finished;
  1862. if (isASCIIByte(bite))
  1863. return bite;
  1864. return 0xF780 + bite - 0x80;
  1865. };
  1866. }
  1867. /**
  1868. * @constructor
  1869. * @implements {Encoder}
  1870. * @param {{fatal: boolean}} options
  1871. */
  1872. function XUserDefinedEncoder(options) {
  1873. var fatal = options.fatal;
  1874. this.handler = function(stream, code_point) {
  1875. if (code_point === end_of_stream)
  1876. return finished;
  1877. if (isASCIICodePoint(code_point))
  1878. return code_point;
  1879. if (inRange(code_point, 0xF780, 0xF7FF))
  1880. return code_point - 0xF780 + 0x80;
  1881. return encoderError(code_point);
  1882. };
  1883. }
  1884. /** @param {{fatal: boolean}} options */
  1885. encoders['x-user-defined'] = function(options) {
  1886. return new XUserDefinedEncoder(options);
  1887. };
  1888. /** @param {{fatal: boolean}} options */
  1889. decoders['x-user-defined'] = function(options) {
  1890. return new XUserDefinedDecoder(options);
  1891. };
  1892. if (!global['TextEncoder'])
  1893. global['TextEncoder'] = TextEncoder;
  1894. if (!global['TextDecoder'])
  1895. global['TextDecoder'] = TextDecoder;
  1896. if (!global['encode'])
  1897. global['encode'] = encode;
  1898. if (typeof module !== "undefined" && module.exports) {
  1899. module.exports = {
  1900. TextEncoder: global['TextEncoder'],
  1901. TextDecoder: global['TextDecoder'],
  1902. EncodingIndexes: global["encoding-indexes"]
  1903. };
  1904. }
  1905. }(this || {}));
  1906. export default {
  1907. _this
  1908. }