spell-checker.js 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841
  1. /**
  2. * codemirror-spell-checker v1.1.2
  3. * Copyright Next Step Webs, Inc.
  4. * @link https://github.com/NextStepWebs/codemirror-spell-checker
  5. * @license MIT
  6. */
  7. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.CodeMirrorSpellChecker = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  8. 'use strict'
  9. exports.toByteArray = toByteArray
  10. exports.fromByteArray = fromByteArray
  11. var lookup = []
  12. var revLookup = []
  13. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  14. function init () {
  15. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  16. for (var i = 0, len = code.length; i < len; ++i) {
  17. lookup[i] = code[i]
  18. revLookup[code.charCodeAt(i)] = i
  19. }
  20. revLookup['-'.charCodeAt(0)] = 62
  21. revLookup['_'.charCodeAt(0)] = 63
  22. }
  23. init()
  24. function toByteArray (b64) {
  25. var i, j, l, tmp, placeHolders, arr
  26. var len = b64.length
  27. if (len % 4 > 0) {
  28. throw new Error('Invalid string. Length must be a multiple of 4')
  29. }
  30. // the number of equal signs (place holders)
  31. // if there are two placeholders, than the two characters before it
  32. // represent one byte
  33. // if there is only one, then the three characters before it represent 2 bytes
  34. // this is just a cheap hack to not do indexOf twice
  35. placeHolders = b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
  36. // base64 is 4/3 + up to two characters of the original data
  37. arr = new Arr(len * 3 / 4 - placeHolders)
  38. // if there are placeholders, only get up to the last complete 4 chars
  39. l = placeHolders > 0 ? len - 4 : len
  40. var L = 0
  41. for (i = 0, j = 0; i < l; i += 4, j += 3) {
  42. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
  43. arr[L++] = (tmp >> 16) & 0xFF
  44. arr[L++] = (tmp >> 8) & 0xFF
  45. arr[L++] = tmp & 0xFF
  46. }
  47. if (placeHolders === 2) {
  48. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
  49. arr[L++] = tmp & 0xFF
  50. } else if (placeHolders === 1) {
  51. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
  52. arr[L++] = (tmp >> 8) & 0xFF
  53. arr[L++] = tmp & 0xFF
  54. }
  55. return arr
  56. }
  57. function tripletToBase64 (num) {
  58. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  59. }
  60. function encodeChunk (uint8, start, end) {
  61. var tmp
  62. var output = []
  63. for (var i = start; i < end; i += 3) {
  64. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  65. output.push(tripletToBase64(tmp))
  66. }
  67. return output.join('')
  68. }
  69. function fromByteArray (uint8) {
  70. var tmp
  71. var len = uint8.length
  72. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  73. var output = ''
  74. var parts = []
  75. var maxChunkLength = 16383 // must be multiple of 3
  76. // go through the array every three bytes, we'll deal with trailing stuff later
  77. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  78. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
  79. }
  80. // pad the end with zeros, but make sure to not forget the extra bytes
  81. if (extraBytes === 1) {
  82. tmp = uint8[len - 1]
  83. output += lookup[tmp >> 2]
  84. output += lookup[(tmp << 4) & 0x3F]
  85. output += '=='
  86. } else if (extraBytes === 2) {
  87. tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
  88. output += lookup[tmp >> 10]
  89. output += lookup[(tmp >> 4) & 0x3F]
  90. output += lookup[(tmp << 2) & 0x3F]
  91. output += '='
  92. }
  93. parts.push(output)
  94. return parts.join('')
  95. }
  96. },{}],2:[function(require,module,exports){
  97. },{}],3:[function(require,module,exports){
  98. (function (global){
  99. /*!
  100. * The buffer module from node.js, for the browser.
  101. *
  102. * @author Feross Aboukhadijeh <feross@feross.org> <http://feross.org>
  103. * @license MIT
  104. */
  105. /* eslint-disable no-proto */
  106. 'use strict'
  107. var base64 = require('base64-js')
  108. var ieee754 = require('ieee754')
  109. var isArray = require('isarray')
  110. exports.Buffer = Buffer
  111. exports.SlowBuffer = SlowBuffer
  112. exports.INSPECT_MAX_BYTES = 50
  113. /**
  114. * If `Buffer.TYPED_ARRAY_SUPPORT`:
  115. * === true Use Uint8Array implementation (fastest)
  116. * === false Use Object implementation (most compatible, even IE6)
  117. *
  118. * Browsers that support typed arrays are IE 10+, Firefox 4+, Chrome 7+, Safari 5.1+,
  119. * Opera 11.6+, iOS 4.2+.
  120. *
  121. * Due to various browser bugs, sometimes the Object implementation will be used even
  122. * when the browser supports typed arrays.
  123. *
  124. * Note:
  125. *
  126. * - Firefox 4-29 lacks support for adding new properties to `Uint8Array` instances,
  127. * See: https://bugzilla.mozilla.org/show_bug.cgi?id=695438.
  128. *
  129. * - Chrome 9-10 is missing the `TypedArray.prototype.subarray` function.
  130. *
  131. * - IE10 has a broken `TypedArray.prototype.subarray` function which returns arrays of
  132. * incorrect length in some situations.
  133. * We detect these buggy browsers and set `Buffer.TYPED_ARRAY_SUPPORT` to `false` so they
  134. * get the Object implementation, which is slower but behaves correctly.
  135. */
  136. Buffer.TYPED_ARRAY_SUPPORT = global.TYPED_ARRAY_SUPPORT !== undefined
  137. ? global.TYPED_ARRAY_SUPPORT
  138. : typedArraySupport()
  139. /*
  140. * Export kMaxLength after typed array support is determined.
  141. */
  142. exports.kMaxLength = kMaxLength()
  143. function typedArraySupport () {
  144. try {
  145. var arr = new Uint8Array(1)
  146. arr.foo = function () { return 42 }
  147. return arr.foo() === 42 && // typed array instances can be augmented
  148. typeof arr.subarray === 'function' && // chrome 9-10 lack `subarray`
  149. arr.subarray(1, 1).byteLength === 0 // ie10 has broken `subarray`
  150. } catch (e) {
  151. return false
  152. }
  153. }
  154. function kMaxLength () {
  155. return Buffer.TYPED_ARRAY_SUPPORT
  156. ? 0x7fffffff
  157. : 0x3fffffff
  158. }
  159. function createBuffer (that, length) {
  160. if (kMaxLength() < length) {
  161. throw new RangeError('Invalid typed array length')
  162. }
  163. if (Buffer.TYPED_ARRAY_SUPPORT) {
  164. // Return an augmented `Uint8Array` instance, for best performance
  165. that = new Uint8Array(length)
  166. that.__proto__ = Buffer.prototype
  167. } else {
  168. // Fallback: Return an object instance of the Buffer class
  169. if (that === null) {
  170. that = new Buffer(length)
  171. }
  172. that.length = length
  173. }
  174. return that
  175. }
  176. /**
  177. * The Buffer constructor returns instances of `Uint8Array` that have their
  178. * prototype changed to `Buffer.prototype`. Furthermore, `Buffer` is a subclass of
  179. * `Uint8Array`, so the returned instances will have all the node `Buffer` methods
  180. * and the `Uint8Array` methods. Square bracket notation works as expected -- it
  181. * returns a single octet.
  182. *
  183. * The `Uint8Array` prototype remains unmodified.
  184. */
  185. function Buffer (arg, encodingOrOffset, length) {
  186. if (!Buffer.TYPED_ARRAY_SUPPORT && !(this instanceof Buffer)) {
  187. return new Buffer(arg, encodingOrOffset, length)
  188. }
  189. // Common case.
  190. if (typeof arg === 'number') {
  191. if (typeof encodingOrOffset === 'string') {
  192. throw new Error(
  193. 'If encoding is specified then the first argument must be a string'
  194. )
  195. }
  196. return allocUnsafe(this, arg)
  197. }
  198. return from(this, arg, encodingOrOffset, length)
  199. }
  200. Buffer.poolSize = 8192 // not used by this implementation
  201. // TODO: Legacy, not needed anymore. Remove in next major version.
  202. Buffer._augment = function (arr) {
  203. arr.__proto__ = Buffer.prototype
  204. return arr
  205. }
  206. function from (that, value, encodingOrOffset, length) {
  207. if (typeof value === 'number') {
  208. throw new TypeError('"value" argument must not be a number')
  209. }
  210. if (typeof ArrayBuffer !== 'undefined' && value instanceof ArrayBuffer) {
  211. return fromArrayBuffer(that, value, encodingOrOffset, length)
  212. }
  213. if (typeof value === 'string') {
  214. return fromString(that, value, encodingOrOffset)
  215. }
  216. return fromObject(that, value)
  217. }
  218. /**
  219. * Functionally equivalent to Buffer(arg, encoding) but throws a TypeError
  220. * if value is a number.
  221. * Buffer.from(str[, encoding])
  222. * Buffer.from(array)
  223. * Buffer.from(buffer)
  224. * Buffer.from(arrayBuffer[, byteOffset[, length]])
  225. **/
  226. Buffer.from = function (value, encodingOrOffset, length) {
  227. return from(null, value, encodingOrOffset, length)
  228. }
  229. if (Buffer.TYPED_ARRAY_SUPPORT) {
  230. Buffer.prototype.__proto__ = Uint8Array.prototype
  231. Buffer.__proto__ = Uint8Array
  232. if (typeof Symbol !== 'undefined' && Symbol.species &&
  233. Buffer[Symbol.species] === Buffer) {
  234. // Fix subarray() in ES2016. See: https://github.com/feross/buffer/pull/97
  235. Object.defineProperty(Buffer, Symbol.species, {
  236. value: null,
  237. configurable: true
  238. })
  239. }
  240. }
  241. function assertSize (size) {
  242. if (typeof size !== 'number') {
  243. throw new TypeError('"size" argument must be a number')
  244. }
  245. }
  246. function alloc (that, size, fill, encoding) {
  247. assertSize(size)
  248. if (size <= 0) {
  249. return createBuffer(that, size)
  250. }
  251. if (fill !== undefined) {
  252. // Only pay attention to encoding if it's a string. This
  253. // prevents accidentally sending in a number that would
  254. // be interpretted as a start offset.
  255. return typeof encoding === 'string'
  256. ? createBuffer(that, size).fill(fill, encoding)
  257. : createBuffer(that, size).fill(fill)
  258. }
  259. return createBuffer(that, size)
  260. }
  261. /**
  262. * Creates a new filled Buffer instance.
  263. * alloc(size[, fill[, encoding]])
  264. **/
  265. Buffer.alloc = function (size, fill, encoding) {
  266. return alloc(null, size, fill, encoding)
  267. }
  268. function allocUnsafe (that, size) {
  269. assertSize(size)
  270. that = createBuffer(that, size < 0 ? 0 : checked(size) | 0)
  271. if (!Buffer.TYPED_ARRAY_SUPPORT) {
  272. for (var i = 0; i < size; i++) {
  273. that[i] = 0
  274. }
  275. }
  276. return that
  277. }
  278. /**
  279. * Equivalent to Buffer(num), by default creates a non-zero-filled Buffer instance.
  280. * */
  281. Buffer.allocUnsafe = function (size) {
  282. return allocUnsafe(null, size)
  283. }
  284. /**
  285. * Equivalent to SlowBuffer(num), by default creates a non-zero-filled Buffer instance.
  286. */
  287. Buffer.allocUnsafeSlow = function (size) {
  288. return allocUnsafe(null, size)
  289. }
  290. function fromString (that, string, encoding) {
  291. if (typeof encoding !== 'string' || encoding === '') {
  292. encoding = 'utf8'
  293. }
  294. if (!Buffer.isEncoding(encoding)) {
  295. throw new TypeError('"encoding" must be a valid string encoding')
  296. }
  297. var length = byteLength(string, encoding) | 0
  298. that = createBuffer(that, length)
  299. that.write(string, encoding)
  300. return that
  301. }
  302. function fromArrayLike (that, array) {
  303. var length = checked(array.length) | 0
  304. that = createBuffer(that, length)
  305. for (var i = 0; i < length; i += 1) {
  306. that[i] = array[i] & 255
  307. }
  308. return that
  309. }
  310. function fromArrayBuffer (that, array, byteOffset, length) {
  311. array.byteLength // this throws if `array` is not a valid ArrayBuffer
  312. if (byteOffset < 0 || array.byteLength < byteOffset) {
  313. throw new RangeError('\'offset\' is out of bounds')
  314. }
  315. if (array.byteLength < byteOffset + (length || 0)) {
  316. throw new RangeError('\'length\' is out of bounds')
  317. }
  318. if (length === undefined) {
  319. array = new Uint8Array(array, byteOffset)
  320. } else {
  321. array = new Uint8Array(array, byteOffset, length)
  322. }
  323. if (Buffer.TYPED_ARRAY_SUPPORT) {
  324. // Return an augmented `Uint8Array` instance, for best performance
  325. that = array
  326. that.__proto__ = Buffer.prototype
  327. } else {
  328. // Fallback: Return an object instance of the Buffer class
  329. that = fromArrayLike(that, array)
  330. }
  331. return that
  332. }
  333. function fromObject (that, obj) {
  334. if (Buffer.isBuffer(obj)) {
  335. var len = checked(obj.length) | 0
  336. that = createBuffer(that, len)
  337. if (that.length === 0) {
  338. return that
  339. }
  340. obj.copy(that, 0, 0, len)
  341. return that
  342. }
  343. if (obj) {
  344. if ((typeof ArrayBuffer !== 'undefined' &&
  345. obj.buffer instanceof ArrayBuffer) || 'length' in obj) {
  346. if (typeof obj.length !== 'number' || isnan(obj.length)) {
  347. return createBuffer(that, 0)
  348. }
  349. return fromArrayLike(that, obj)
  350. }
  351. if (obj.type === 'Buffer' && isArray(obj.data)) {
  352. return fromArrayLike(that, obj.data)
  353. }
  354. }
  355. throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
  356. }
  357. function checked (length) {
  358. // Note: cannot use `length < kMaxLength` here because that fails when
  359. // length is NaN (which is otherwise coerced to zero.)
  360. if (length >= kMaxLength()) {
  361. throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
  362. 'size: 0x' + kMaxLength().toString(16) + ' bytes')
  363. }
  364. return length | 0
  365. }
  366. function SlowBuffer (length) {
  367. if (+length != length) { // eslint-disable-line eqeqeq
  368. length = 0
  369. }
  370. return Buffer.alloc(+length)
  371. }
  372. Buffer.isBuffer = function isBuffer (b) {
  373. return !!(b != null && b._isBuffer)
  374. }
  375. Buffer.compare = function compare (a, b) {
  376. if (!Buffer.isBuffer(a) || !Buffer.isBuffer(b)) {
  377. throw new TypeError('Arguments must be Buffers')
  378. }
  379. if (a === b) return 0
  380. var x = a.length
  381. var y = b.length
  382. for (var i = 0, len = Math.min(x, y); i < len; ++i) {
  383. if (a[i] !== b[i]) {
  384. x = a[i]
  385. y = b[i]
  386. break
  387. }
  388. }
  389. if (x < y) return -1
  390. if (y < x) return 1
  391. return 0
  392. }
  393. Buffer.isEncoding = function isEncoding (encoding) {
  394. switch (String(encoding).toLowerCase()) {
  395. case 'hex':
  396. case 'utf8':
  397. case 'utf-8':
  398. case 'ascii':
  399. case 'binary':
  400. case 'base64':
  401. case 'raw':
  402. case 'ucs2':
  403. case 'ucs-2':
  404. case 'utf16le':
  405. case 'utf-16le':
  406. return true
  407. default:
  408. return false
  409. }
  410. }
  411. Buffer.concat = function concat (list, length) {
  412. if (!isArray(list)) {
  413. throw new TypeError('"list" argument must be an Array of Buffers')
  414. }
  415. if (list.length === 0) {
  416. return Buffer.alloc(0)
  417. }
  418. var i
  419. if (length === undefined) {
  420. length = 0
  421. for (i = 0; i < list.length; i++) {
  422. length += list[i].length
  423. }
  424. }
  425. var buffer = Buffer.allocUnsafe(length)
  426. var pos = 0
  427. for (i = 0; i < list.length; i++) {
  428. var buf = list[i]
  429. if (!Buffer.isBuffer(buf)) {
  430. throw new TypeError('"list" argument must be an Array of Buffers')
  431. }
  432. buf.copy(buffer, pos)
  433. pos += buf.length
  434. }
  435. return buffer
  436. }
  437. function byteLength (string, encoding) {
  438. if (Buffer.isBuffer(string)) {
  439. return string.length
  440. }
  441. if (typeof ArrayBuffer !== 'undefined' && typeof ArrayBuffer.isView === 'function' &&
  442. (ArrayBuffer.isView(string) || string instanceof ArrayBuffer)) {
  443. return string.byteLength
  444. }
  445. if (typeof string !== 'string') {
  446. string = '' + string
  447. }
  448. var len = string.length
  449. if (len === 0) return 0
  450. // Use a for loop to avoid recursion
  451. var loweredCase = false
  452. for (;;) {
  453. switch (encoding) {
  454. case 'ascii':
  455. case 'binary':
  456. // Deprecated
  457. case 'raw':
  458. case 'raws':
  459. return len
  460. case 'utf8':
  461. case 'utf-8':
  462. case undefined:
  463. return utf8ToBytes(string).length
  464. case 'ucs2':
  465. case 'ucs-2':
  466. case 'utf16le':
  467. case 'utf-16le':
  468. return len * 2
  469. case 'hex':
  470. return len >>> 1
  471. case 'base64':
  472. return base64ToBytes(string).length
  473. default:
  474. if (loweredCase) return utf8ToBytes(string).length // assume utf8
  475. encoding = ('' + encoding).toLowerCase()
  476. loweredCase = true
  477. }
  478. }
  479. }
  480. Buffer.byteLength = byteLength
  481. function slowToString (encoding, start, end) {
  482. var loweredCase = false
  483. // No need to verify that "this.length <= MAX_UINT32" since it's a read-only
  484. // property of a typed array.
  485. // This behaves neither like String nor Uint8Array in that we set start/end
  486. // to their upper/lower bounds if the value passed is out of range.
  487. // undefined is handled specially as per ECMA-262 6th Edition,
  488. // Section 13.3.3.7 Runtime Semantics: KeyedBindingInitialization.
  489. if (start === undefined || start < 0) {
  490. start = 0
  491. }
  492. // Return early if start > this.length. Done here to prevent potential uint32
  493. // coercion fail below.
  494. if (start > this.length) {
  495. return ''
  496. }
  497. if (end === undefined || end > this.length) {
  498. end = this.length
  499. }
  500. if (end <= 0) {
  501. return ''
  502. }
  503. // Force coersion to uint32. This will also coerce falsey/NaN values to 0.
  504. end >>>= 0
  505. start >>>= 0
  506. if (end <= start) {
  507. return ''
  508. }
  509. if (!encoding) encoding = 'utf8'
  510. while (true) {
  511. switch (encoding) {
  512. case 'hex':
  513. return hexSlice(this, start, end)
  514. case 'utf8':
  515. case 'utf-8':
  516. return utf8Slice(this, start, end)
  517. case 'ascii':
  518. return asciiSlice(this, start, end)
  519. case 'binary':
  520. return binarySlice(this, start, end)
  521. case 'base64':
  522. return base64Slice(this, start, end)
  523. case 'ucs2':
  524. case 'ucs-2':
  525. case 'utf16le':
  526. case 'utf-16le':
  527. return utf16leSlice(this, start, end)
  528. default:
  529. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  530. encoding = (encoding + '').toLowerCase()
  531. loweredCase = true
  532. }
  533. }
  534. }
  535. // The property is used by `Buffer.isBuffer` and `is-buffer` (in Safari 5-7) to detect
  536. // Buffer instances.
  537. Buffer.prototype._isBuffer = true
  538. function swap (b, n, m) {
  539. var i = b[n]
  540. b[n] = b[m]
  541. b[m] = i
  542. }
  543. Buffer.prototype.swap16 = function swap16 () {
  544. var len = this.length
  545. if (len % 2 !== 0) {
  546. throw new RangeError('Buffer size must be a multiple of 16-bits')
  547. }
  548. for (var i = 0; i < len; i += 2) {
  549. swap(this, i, i + 1)
  550. }
  551. return this
  552. }
  553. Buffer.prototype.swap32 = function swap32 () {
  554. var len = this.length
  555. if (len % 4 !== 0) {
  556. throw new RangeError('Buffer size must be a multiple of 32-bits')
  557. }
  558. for (var i = 0; i < len; i += 4) {
  559. swap(this, i, i + 3)
  560. swap(this, i + 1, i + 2)
  561. }
  562. return this
  563. }
  564. Buffer.prototype.toString = function toString () {
  565. var length = this.length | 0
  566. if (length === 0) return ''
  567. if (arguments.length === 0) return utf8Slice(this, 0, length)
  568. return slowToString.apply(this, arguments)
  569. }
  570. Buffer.prototype.equals = function equals (b) {
  571. if (!Buffer.isBuffer(b)) throw new TypeError('Argument must be a Buffer')
  572. if (this === b) return true
  573. return Buffer.compare(this, b) === 0
  574. }
  575. Buffer.prototype.inspect = function inspect () {
  576. var str = ''
  577. var max = exports.INSPECT_MAX_BYTES
  578. if (this.length > 0) {
  579. str = this.toString('hex', 0, max).match(/.{2}/g).join(' ')
  580. if (this.length > max) str += ' ... '
  581. }
  582. return '<Buffer ' + str + '>'
  583. }
  584. Buffer.prototype.compare = function compare (target, start, end, thisStart, thisEnd) {
  585. if (!Buffer.isBuffer(target)) {
  586. throw new TypeError('Argument must be a Buffer')
  587. }
  588. if (start === undefined) {
  589. start = 0
  590. }
  591. if (end === undefined) {
  592. end = target ? target.length : 0
  593. }
  594. if (thisStart === undefined) {
  595. thisStart = 0
  596. }
  597. if (thisEnd === undefined) {
  598. thisEnd = this.length
  599. }
  600. if (start < 0 || end > target.length || thisStart < 0 || thisEnd > this.length) {
  601. throw new RangeError('out of range index')
  602. }
  603. if (thisStart >= thisEnd && start >= end) {
  604. return 0
  605. }
  606. if (thisStart >= thisEnd) {
  607. return -1
  608. }
  609. if (start >= end) {
  610. return 1
  611. }
  612. start >>>= 0
  613. end >>>= 0
  614. thisStart >>>= 0
  615. thisEnd >>>= 0
  616. if (this === target) return 0
  617. var x = thisEnd - thisStart
  618. var y = end - start
  619. var len = Math.min(x, y)
  620. var thisCopy = this.slice(thisStart, thisEnd)
  621. var targetCopy = target.slice(start, end)
  622. for (var i = 0; i < len; ++i) {
  623. if (thisCopy[i] !== targetCopy[i]) {
  624. x = thisCopy[i]
  625. y = targetCopy[i]
  626. break
  627. }
  628. }
  629. if (x < y) return -1
  630. if (y < x) return 1
  631. return 0
  632. }
  633. function arrayIndexOf (arr, val, byteOffset, encoding) {
  634. var indexSize = 1
  635. var arrLength = arr.length
  636. var valLength = val.length
  637. if (encoding !== undefined) {
  638. encoding = String(encoding).toLowerCase()
  639. if (encoding === 'ucs2' || encoding === 'ucs-2' ||
  640. encoding === 'utf16le' || encoding === 'utf-16le') {
  641. if (arr.length < 2 || val.length < 2) {
  642. return -1
  643. }
  644. indexSize = 2
  645. arrLength /= 2
  646. valLength /= 2
  647. byteOffset /= 2
  648. }
  649. }
  650. function read (buf, i) {
  651. if (indexSize === 1) {
  652. return buf[i]
  653. } else {
  654. return buf.readUInt16BE(i * indexSize)
  655. }
  656. }
  657. var foundIndex = -1
  658. for (var i = 0; byteOffset + i < arrLength; i++) {
  659. if (read(arr, byteOffset + i) === read(val, foundIndex === -1 ? 0 : i - foundIndex)) {
  660. if (foundIndex === -1) foundIndex = i
  661. if (i - foundIndex + 1 === valLength) return (byteOffset + foundIndex) * indexSize
  662. } else {
  663. if (foundIndex !== -1) i -= i - foundIndex
  664. foundIndex = -1
  665. }
  666. }
  667. return -1
  668. }
  669. Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) {
  670. if (typeof byteOffset === 'string') {
  671. encoding = byteOffset
  672. byteOffset = 0
  673. } else if (byteOffset > 0x7fffffff) {
  674. byteOffset = 0x7fffffff
  675. } else if (byteOffset < -0x80000000) {
  676. byteOffset = -0x80000000
  677. }
  678. byteOffset >>= 0
  679. if (this.length === 0) return -1
  680. if (byteOffset >= this.length) return -1
  681. // Negative offsets start from the end of the buffer
  682. if (byteOffset < 0) byteOffset = Math.max(this.length + byteOffset, 0)
  683. if (typeof val === 'string') {
  684. val = Buffer.from(val, encoding)
  685. }
  686. if (Buffer.isBuffer(val)) {
  687. // special case: looking for empty string/buffer always fails
  688. if (val.length === 0) {
  689. return -1
  690. }
  691. return arrayIndexOf(this, val, byteOffset, encoding)
  692. }
  693. if (typeof val === 'number') {
  694. if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') {
  695. return Uint8Array.prototype.indexOf.call(this, val, byteOffset)
  696. }
  697. return arrayIndexOf(this, [ val ], byteOffset, encoding)
  698. }
  699. throw new TypeError('val must be string, number or Buffer')
  700. }
  701. Buffer.prototype.includes = function includes (val, byteOffset, encoding) {
  702. return this.indexOf(val, byteOffset, encoding) !== -1
  703. }
  704. function hexWrite (buf, string, offset, length) {
  705. offset = Number(offset) || 0
  706. var remaining = buf.length - offset
  707. if (!length) {
  708. length = remaining
  709. } else {
  710. length = Number(length)
  711. if (length > remaining) {
  712. length = remaining
  713. }
  714. }
  715. // must be an even number of digits
  716. var strLen = string.length
  717. if (strLen % 2 !== 0) throw new Error('Invalid hex string')
  718. if (length > strLen / 2) {
  719. length = strLen / 2
  720. }
  721. for (var i = 0; i < length; i++) {
  722. var parsed = parseInt(string.substr(i * 2, 2), 16)
  723. if (isNaN(parsed)) return i
  724. buf[offset + i] = parsed
  725. }
  726. return i
  727. }
  728. function utf8Write (buf, string, offset, length) {
  729. return blitBuffer(utf8ToBytes(string, buf.length - offset), buf, offset, length)
  730. }
  731. function asciiWrite (buf, string, offset, length) {
  732. return blitBuffer(asciiToBytes(string), buf, offset, length)
  733. }
  734. function binaryWrite (buf, string, offset, length) {
  735. return asciiWrite(buf, string, offset, length)
  736. }
  737. function base64Write (buf, string, offset, length) {
  738. return blitBuffer(base64ToBytes(string), buf, offset, length)
  739. }
  740. function ucs2Write (buf, string, offset, length) {
  741. return blitBuffer(utf16leToBytes(string, buf.length - offset), buf, offset, length)
  742. }
  743. Buffer.prototype.write = function write (string, offset, length, encoding) {
  744. // Buffer#write(string)
  745. if (offset === undefined) {
  746. encoding = 'utf8'
  747. length = this.length
  748. offset = 0
  749. // Buffer#write(string, encoding)
  750. } else if (length === undefined && typeof offset === 'string') {
  751. encoding = offset
  752. length = this.length
  753. offset = 0
  754. // Buffer#write(string, offset[, length][, encoding])
  755. } else if (isFinite(offset)) {
  756. offset = offset | 0
  757. if (isFinite(length)) {
  758. length = length | 0
  759. if (encoding === undefined) encoding = 'utf8'
  760. } else {
  761. encoding = length
  762. length = undefined
  763. }
  764. // legacy write(string, encoding, offset, length) - remove in v0.13
  765. } else {
  766. throw new Error(
  767. 'Buffer.write(string, encoding, offset[, length]) is no longer supported'
  768. )
  769. }
  770. var remaining = this.length - offset
  771. if (length === undefined || length > remaining) length = remaining
  772. if ((string.length > 0 && (length < 0 || offset < 0)) || offset > this.length) {
  773. throw new RangeError('Attempt to write outside buffer bounds')
  774. }
  775. if (!encoding) encoding = 'utf8'
  776. var loweredCase = false
  777. for (;;) {
  778. switch (encoding) {
  779. case 'hex':
  780. return hexWrite(this, string, offset, length)
  781. case 'utf8':
  782. case 'utf-8':
  783. return utf8Write(this, string, offset, length)
  784. case 'ascii':
  785. return asciiWrite(this, string, offset, length)
  786. case 'binary':
  787. return binaryWrite(this, string, offset, length)
  788. case 'base64':
  789. // Warning: maxLength not taken into account in base64Write
  790. return base64Write(this, string, offset, length)
  791. case 'ucs2':
  792. case 'ucs-2':
  793. case 'utf16le':
  794. case 'utf-16le':
  795. return ucs2Write(this, string, offset, length)
  796. default:
  797. if (loweredCase) throw new TypeError('Unknown encoding: ' + encoding)
  798. encoding = ('' + encoding).toLowerCase()
  799. loweredCase = true
  800. }
  801. }
  802. }
  803. Buffer.prototype.toJSON = function toJSON () {
  804. return {
  805. type: 'Buffer',
  806. data: Array.prototype.slice.call(this._arr || this, 0)
  807. }
  808. }
  809. function base64Slice (buf, start, end) {
  810. if (start === 0 && end === buf.length) {
  811. return base64.fromByteArray(buf)
  812. } else {
  813. return base64.fromByteArray(buf.slice(start, end))
  814. }
  815. }
  816. function utf8Slice (buf, start, end) {
  817. end = Math.min(buf.length, end)
  818. var res = []
  819. var i = start
  820. while (i < end) {
  821. var firstByte = buf[i]
  822. var codePoint = null
  823. var bytesPerSequence = (firstByte > 0xEF) ? 4
  824. : (firstByte > 0xDF) ? 3
  825. : (firstByte > 0xBF) ? 2
  826. : 1
  827. if (i + bytesPerSequence <= end) {
  828. var secondByte, thirdByte, fourthByte, tempCodePoint
  829. switch (bytesPerSequence) {
  830. case 1:
  831. if (firstByte < 0x80) {
  832. codePoint = firstByte
  833. }
  834. break
  835. case 2:
  836. secondByte = buf[i + 1]
  837. if ((secondByte & 0xC0) === 0x80) {
  838. tempCodePoint = (firstByte & 0x1F) << 0x6 | (secondByte & 0x3F)
  839. if (tempCodePoint > 0x7F) {
  840. codePoint = tempCodePoint
  841. }
  842. }
  843. break
  844. case 3:
  845. secondByte = buf[i + 1]
  846. thirdByte = buf[i + 2]
  847. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80) {
  848. tempCodePoint = (firstByte & 0xF) << 0xC | (secondByte & 0x3F) << 0x6 | (thirdByte & 0x3F)
  849. if (tempCodePoint > 0x7FF && (tempCodePoint < 0xD800 || tempCodePoint > 0xDFFF)) {
  850. codePoint = tempCodePoint
  851. }
  852. }
  853. break
  854. case 4:
  855. secondByte = buf[i + 1]
  856. thirdByte = buf[i + 2]
  857. fourthByte = buf[i + 3]
  858. if ((secondByte & 0xC0) === 0x80 && (thirdByte & 0xC0) === 0x80 && (fourthByte & 0xC0) === 0x80) {
  859. tempCodePoint = (firstByte & 0xF) << 0x12 | (secondByte & 0x3F) << 0xC | (thirdByte & 0x3F) << 0x6 | (fourthByte & 0x3F)
  860. if (tempCodePoint > 0xFFFF && tempCodePoint < 0x110000) {
  861. codePoint = tempCodePoint
  862. }
  863. }
  864. }
  865. }
  866. if (codePoint === null) {
  867. // we did not generate a valid codePoint so insert a
  868. // replacement char (U+FFFD) and advance only 1 byte
  869. codePoint = 0xFFFD
  870. bytesPerSequence = 1
  871. } else if (codePoint > 0xFFFF) {
  872. // encode to utf16 (surrogate pair dance)
  873. codePoint -= 0x10000
  874. res.push(codePoint >>> 10 & 0x3FF | 0xD800)
  875. codePoint = 0xDC00 | codePoint & 0x3FF
  876. }
  877. res.push(codePoint)
  878. i += bytesPerSequence
  879. }
  880. return decodeCodePointsArray(res)
  881. }
  882. // Based on http://stackoverflow.com/a/22747272/680742, the browser with
  883. // the lowest limit is Chrome, with 0x10000 args.
  884. // We go 1 magnitude less, for safety
  885. var MAX_ARGUMENTS_LENGTH = 0x1000
  886. function decodeCodePointsArray (codePoints) {
  887. var len = codePoints.length
  888. if (len <= MAX_ARGUMENTS_LENGTH) {
  889. return String.fromCharCode.apply(String, codePoints) // avoid extra slice()
  890. }
  891. // Decode in chunks to avoid "call stack size exceeded".
  892. var res = ''
  893. var i = 0
  894. while (i < len) {
  895. res += String.fromCharCode.apply(
  896. String,
  897. codePoints.slice(i, i += MAX_ARGUMENTS_LENGTH)
  898. )
  899. }
  900. return res
  901. }
  902. function asciiSlice (buf, start, end) {
  903. var ret = ''
  904. end = Math.min(buf.length, end)
  905. for (var i = start; i < end; i++) {
  906. ret += String.fromCharCode(buf[i] & 0x7F)
  907. }
  908. return ret
  909. }
  910. function binarySlice (buf, start, end) {
  911. var ret = ''
  912. end = Math.min(buf.length, end)
  913. for (var i = start; i < end; i++) {
  914. ret += String.fromCharCode(buf[i])
  915. }
  916. return ret
  917. }
  918. function hexSlice (buf, start, end) {
  919. var len = buf.length
  920. if (!start || start < 0) start = 0
  921. if (!end || end < 0 || end > len) end = len
  922. var out = ''
  923. for (var i = start; i < end; i++) {
  924. out += toHex(buf[i])
  925. }
  926. return out
  927. }
  928. function utf16leSlice (buf, start, end) {
  929. var bytes = buf.slice(start, end)
  930. var res = ''
  931. for (var i = 0; i < bytes.length; i += 2) {
  932. res += String.fromCharCode(bytes[i] + bytes[i + 1] * 256)
  933. }
  934. return res
  935. }
  936. Buffer.prototype.slice = function slice (start, end) {
  937. var len = this.length
  938. start = ~~start
  939. end = end === undefined ? len : ~~end
  940. if (start < 0) {
  941. start += len
  942. if (start < 0) start = 0
  943. } else if (start > len) {
  944. start = len
  945. }
  946. if (end < 0) {
  947. end += len
  948. if (end < 0) end = 0
  949. } else if (end > len) {
  950. end = len
  951. }
  952. if (end < start) end = start
  953. var newBuf
  954. if (Buffer.TYPED_ARRAY_SUPPORT) {
  955. newBuf = this.subarray(start, end)
  956. newBuf.__proto__ = Buffer.prototype
  957. } else {
  958. var sliceLen = end - start
  959. newBuf = new Buffer(sliceLen, undefined)
  960. for (var i = 0; i < sliceLen; i++) {
  961. newBuf[i] = this[i + start]
  962. }
  963. }
  964. return newBuf
  965. }
  966. /*
  967. * Need to make sure that buffer isn't trying to write out of bounds.
  968. */
  969. function checkOffset (offset, ext, length) {
  970. if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint')
  971. if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length')
  972. }
  973. Buffer.prototype.readUIntLE = function readUIntLE (offset, byteLength, noAssert) {
  974. offset = offset | 0
  975. byteLength = byteLength | 0
  976. if (!noAssert) checkOffset(offset, byteLength, this.length)
  977. var val = this[offset]
  978. var mul = 1
  979. var i = 0
  980. while (++i < byteLength && (mul *= 0x100)) {
  981. val += this[offset + i] * mul
  982. }
  983. return val
  984. }
  985. Buffer.prototype.readUIntBE = function readUIntBE (offset, byteLength, noAssert) {
  986. offset = offset | 0
  987. byteLength = byteLength | 0
  988. if (!noAssert) {
  989. checkOffset(offset, byteLength, this.length)
  990. }
  991. var val = this[offset + --byteLength]
  992. var mul = 1
  993. while (byteLength > 0 && (mul *= 0x100)) {
  994. val += this[offset + --byteLength] * mul
  995. }
  996. return val
  997. }
  998. Buffer.prototype.readUInt8 = function readUInt8 (offset, noAssert) {
  999. if (!noAssert) checkOffset(offset, 1, this.length)
  1000. return this[offset]
  1001. }
  1002. Buffer.prototype.readUInt16LE = function readUInt16LE (offset, noAssert) {
  1003. if (!noAssert) checkOffset(offset, 2, this.length)
  1004. return this[offset] | (this[offset + 1] << 8)
  1005. }
  1006. Buffer.prototype.readUInt16BE = function readUInt16BE (offset, noAssert) {
  1007. if (!noAssert) checkOffset(offset, 2, this.length)
  1008. return (this[offset] << 8) | this[offset + 1]
  1009. }
  1010. Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
  1011. if (!noAssert) checkOffset(offset, 4, this.length)
  1012. return ((this[offset]) |
  1013. (this[offset + 1] << 8) |
  1014. (this[offset + 2] << 16)) +
  1015. (this[offset + 3] * 0x1000000)
  1016. }
  1017. Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) {
  1018. if (!noAssert) checkOffset(offset, 4, this.length)
  1019. return (this[offset] * 0x1000000) +
  1020. ((this[offset + 1] << 16) |
  1021. (this[offset + 2] << 8) |
  1022. this[offset + 3])
  1023. }
  1024. Buffer.prototype.readIntLE = function readIntLE (offset, byteLength, noAssert) {
  1025. offset = offset | 0
  1026. byteLength = byteLength | 0
  1027. if (!noAssert) checkOffset(offset, byteLength, this.length)
  1028. var val = this[offset]
  1029. var mul = 1
  1030. var i = 0
  1031. while (++i < byteLength && (mul *= 0x100)) {
  1032. val += this[offset + i] * mul
  1033. }
  1034. mul *= 0x80
  1035. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  1036. return val
  1037. }
  1038. Buffer.prototype.readIntBE = function readIntBE (offset, byteLength, noAssert) {
  1039. offset = offset | 0
  1040. byteLength = byteLength | 0
  1041. if (!noAssert) checkOffset(offset, byteLength, this.length)
  1042. var i = byteLength
  1043. var mul = 1
  1044. var val = this[offset + --i]
  1045. while (i > 0 && (mul *= 0x100)) {
  1046. val += this[offset + --i] * mul
  1047. }
  1048. mul *= 0x80
  1049. if (val >= mul) val -= Math.pow(2, 8 * byteLength)
  1050. return val
  1051. }
  1052. Buffer.prototype.readInt8 = function readInt8 (offset, noAssert) {
  1053. if (!noAssert) checkOffset(offset, 1, this.length)
  1054. if (!(this[offset] & 0x80)) return (this[offset])
  1055. return ((0xff - this[offset] + 1) * -1)
  1056. }
  1057. Buffer.prototype.readInt16LE = function readInt16LE (offset, noAssert) {
  1058. if (!noAssert) checkOffset(offset, 2, this.length)
  1059. var val = this[offset] | (this[offset + 1] << 8)
  1060. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1061. }
  1062. Buffer.prototype.readInt16BE = function readInt16BE (offset, noAssert) {
  1063. if (!noAssert) checkOffset(offset, 2, this.length)
  1064. var val = this[offset + 1] | (this[offset] << 8)
  1065. return (val & 0x8000) ? val | 0xFFFF0000 : val
  1066. }
  1067. Buffer.prototype.readInt32LE = function readInt32LE (offset, noAssert) {
  1068. if (!noAssert) checkOffset(offset, 4, this.length)
  1069. return (this[offset]) |
  1070. (this[offset + 1] << 8) |
  1071. (this[offset + 2] << 16) |
  1072. (this[offset + 3] << 24)
  1073. }
  1074. Buffer.prototype.readInt32BE = function readInt32BE (offset, noAssert) {
  1075. if (!noAssert) checkOffset(offset, 4, this.length)
  1076. return (this[offset] << 24) |
  1077. (this[offset + 1] << 16) |
  1078. (this[offset + 2] << 8) |
  1079. (this[offset + 3])
  1080. }
  1081. Buffer.prototype.readFloatLE = function readFloatLE (offset, noAssert) {
  1082. if (!noAssert) checkOffset(offset, 4, this.length)
  1083. return ieee754.read(this, offset, true, 23, 4)
  1084. }
  1085. Buffer.prototype.readFloatBE = function readFloatBE (offset, noAssert) {
  1086. if (!noAssert) checkOffset(offset, 4, this.length)
  1087. return ieee754.read(this, offset, false, 23, 4)
  1088. }
  1089. Buffer.prototype.readDoubleLE = function readDoubleLE (offset, noAssert) {
  1090. if (!noAssert) checkOffset(offset, 8, this.length)
  1091. return ieee754.read(this, offset, true, 52, 8)
  1092. }
  1093. Buffer.prototype.readDoubleBE = function readDoubleBE (offset, noAssert) {
  1094. if (!noAssert) checkOffset(offset, 8, this.length)
  1095. return ieee754.read(this, offset, false, 52, 8)
  1096. }
  1097. function checkInt (buf, value, offset, ext, max, min) {
  1098. if (!Buffer.isBuffer(buf)) throw new TypeError('"buffer" argument must be a Buffer instance')
  1099. if (value > max || value < min) throw new RangeError('"value" argument is out of bounds')
  1100. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1101. }
  1102. Buffer.prototype.writeUIntLE = function writeUIntLE (value, offset, byteLength, noAssert) {
  1103. value = +value
  1104. offset = offset | 0
  1105. byteLength = byteLength | 0
  1106. if (!noAssert) {
  1107. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  1108. checkInt(this, value, offset, byteLength, maxBytes, 0)
  1109. }
  1110. var mul = 1
  1111. var i = 0
  1112. this[offset] = value & 0xFF
  1113. while (++i < byteLength && (mul *= 0x100)) {
  1114. this[offset + i] = (value / mul) & 0xFF
  1115. }
  1116. return offset + byteLength
  1117. }
  1118. Buffer.prototype.writeUIntBE = function writeUIntBE (value, offset, byteLength, noAssert) {
  1119. value = +value
  1120. offset = offset | 0
  1121. byteLength = byteLength | 0
  1122. if (!noAssert) {
  1123. var maxBytes = Math.pow(2, 8 * byteLength) - 1
  1124. checkInt(this, value, offset, byteLength, maxBytes, 0)
  1125. }
  1126. var i = byteLength - 1
  1127. var mul = 1
  1128. this[offset + i] = value & 0xFF
  1129. while (--i >= 0 && (mul *= 0x100)) {
  1130. this[offset + i] = (value / mul) & 0xFF
  1131. }
  1132. return offset + byteLength
  1133. }
  1134. Buffer.prototype.writeUInt8 = function writeUInt8 (value, offset, noAssert) {
  1135. value = +value
  1136. offset = offset | 0
  1137. if (!noAssert) checkInt(this, value, offset, 1, 0xff, 0)
  1138. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  1139. this[offset] = (value & 0xff)
  1140. return offset + 1
  1141. }
  1142. function objectWriteUInt16 (buf, value, offset, littleEndian) {
  1143. if (value < 0) value = 0xffff + value + 1
  1144. for (var i = 0, j = Math.min(buf.length - offset, 2); i < j; i++) {
  1145. buf[offset + i] = (value & (0xff << (8 * (littleEndian ? i : 1 - i)))) >>>
  1146. (littleEndian ? i : 1 - i) * 8
  1147. }
  1148. }
  1149. Buffer.prototype.writeUInt16LE = function writeUInt16LE (value, offset, noAssert) {
  1150. value = +value
  1151. offset = offset | 0
  1152. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  1153. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1154. this[offset] = (value & 0xff)
  1155. this[offset + 1] = (value >>> 8)
  1156. } else {
  1157. objectWriteUInt16(this, value, offset, true)
  1158. }
  1159. return offset + 2
  1160. }
  1161. Buffer.prototype.writeUInt16BE = function writeUInt16BE (value, offset, noAssert) {
  1162. value = +value
  1163. offset = offset | 0
  1164. if (!noAssert) checkInt(this, value, offset, 2, 0xffff, 0)
  1165. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1166. this[offset] = (value >>> 8)
  1167. this[offset + 1] = (value & 0xff)
  1168. } else {
  1169. objectWriteUInt16(this, value, offset, false)
  1170. }
  1171. return offset + 2
  1172. }
  1173. function objectWriteUInt32 (buf, value, offset, littleEndian) {
  1174. if (value < 0) value = 0xffffffff + value + 1
  1175. for (var i = 0, j = Math.min(buf.length - offset, 4); i < j; i++) {
  1176. buf[offset + i] = (value >>> (littleEndian ? i : 3 - i) * 8) & 0xff
  1177. }
  1178. }
  1179. Buffer.prototype.writeUInt32LE = function writeUInt32LE (value, offset, noAssert) {
  1180. value = +value
  1181. offset = offset | 0
  1182. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  1183. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1184. this[offset + 3] = (value >>> 24)
  1185. this[offset + 2] = (value >>> 16)
  1186. this[offset + 1] = (value >>> 8)
  1187. this[offset] = (value & 0xff)
  1188. } else {
  1189. objectWriteUInt32(this, value, offset, true)
  1190. }
  1191. return offset + 4
  1192. }
  1193. Buffer.prototype.writeUInt32BE = function writeUInt32BE (value, offset, noAssert) {
  1194. value = +value
  1195. offset = offset | 0
  1196. if (!noAssert) checkInt(this, value, offset, 4, 0xffffffff, 0)
  1197. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1198. this[offset] = (value >>> 24)
  1199. this[offset + 1] = (value >>> 16)
  1200. this[offset + 2] = (value >>> 8)
  1201. this[offset + 3] = (value & 0xff)
  1202. } else {
  1203. objectWriteUInt32(this, value, offset, false)
  1204. }
  1205. return offset + 4
  1206. }
  1207. Buffer.prototype.writeIntLE = function writeIntLE (value, offset, byteLength, noAssert) {
  1208. value = +value
  1209. offset = offset | 0
  1210. if (!noAssert) {
  1211. var limit = Math.pow(2, 8 * byteLength - 1)
  1212. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  1213. }
  1214. var i = 0
  1215. var mul = 1
  1216. var sub = 0
  1217. this[offset] = value & 0xFF
  1218. while (++i < byteLength && (mul *= 0x100)) {
  1219. if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
  1220. sub = 1
  1221. }
  1222. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  1223. }
  1224. return offset + byteLength
  1225. }
  1226. Buffer.prototype.writeIntBE = function writeIntBE (value, offset, byteLength, noAssert) {
  1227. value = +value
  1228. offset = offset | 0
  1229. if (!noAssert) {
  1230. var limit = Math.pow(2, 8 * byteLength - 1)
  1231. checkInt(this, value, offset, byteLength, limit - 1, -limit)
  1232. }
  1233. var i = byteLength - 1
  1234. var mul = 1
  1235. var sub = 0
  1236. this[offset + i] = value & 0xFF
  1237. while (--i >= 0 && (mul *= 0x100)) {
  1238. if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
  1239. sub = 1
  1240. }
  1241. this[offset + i] = ((value / mul) >> 0) - sub & 0xFF
  1242. }
  1243. return offset + byteLength
  1244. }
  1245. Buffer.prototype.writeInt8 = function writeInt8 (value, offset, noAssert) {
  1246. value = +value
  1247. offset = offset | 0
  1248. if (!noAssert) checkInt(this, value, offset, 1, 0x7f, -0x80)
  1249. if (!Buffer.TYPED_ARRAY_SUPPORT) value = Math.floor(value)
  1250. if (value < 0) value = 0xff + value + 1
  1251. this[offset] = (value & 0xff)
  1252. return offset + 1
  1253. }
  1254. Buffer.prototype.writeInt16LE = function writeInt16LE (value, offset, noAssert) {
  1255. value = +value
  1256. offset = offset | 0
  1257. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  1258. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1259. this[offset] = (value & 0xff)
  1260. this[offset + 1] = (value >>> 8)
  1261. } else {
  1262. objectWriteUInt16(this, value, offset, true)
  1263. }
  1264. return offset + 2
  1265. }
  1266. Buffer.prototype.writeInt16BE = function writeInt16BE (value, offset, noAssert) {
  1267. value = +value
  1268. offset = offset | 0
  1269. if (!noAssert) checkInt(this, value, offset, 2, 0x7fff, -0x8000)
  1270. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1271. this[offset] = (value >>> 8)
  1272. this[offset + 1] = (value & 0xff)
  1273. } else {
  1274. objectWriteUInt16(this, value, offset, false)
  1275. }
  1276. return offset + 2
  1277. }
  1278. Buffer.prototype.writeInt32LE = function writeInt32LE (value, offset, noAssert) {
  1279. value = +value
  1280. offset = offset | 0
  1281. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  1282. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1283. this[offset] = (value & 0xff)
  1284. this[offset + 1] = (value >>> 8)
  1285. this[offset + 2] = (value >>> 16)
  1286. this[offset + 3] = (value >>> 24)
  1287. } else {
  1288. objectWriteUInt32(this, value, offset, true)
  1289. }
  1290. return offset + 4
  1291. }
  1292. Buffer.prototype.writeInt32BE = function writeInt32BE (value, offset, noAssert) {
  1293. value = +value
  1294. offset = offset | 0
  1295. if (!noAssert) checkInt(this, value, offset, 4, 0x7fffffff, -0x80000000)
  1296. if (value < 0) value = 0xffffffff + value + 1
  1297. if (Buffer.TYPED_ARRAY_SUPPORT) {
  1298. this[offset] = (value >>> 24)
  1299. this[offset + 1] = (value >>> 16)
  1300. this[offset + 2] = (value >>> 8)
  1301. this[offset + 3] = (value & 0xff)
  1302. } else {
  1303. objectWriteUInt32(this, value, offset, false)
  1304. }
  1305. return offset + 4
  1306. }
  1307. function checkIEEE754 (buf, value, offset, ext, max, min) {
  1308. if (offset + ext > buf.length) throw new RangeError('Index out of range')
  1309. if (offset < 0) throw new RangeError('Index out of range')
  1310. }
  1311. function writeFloat (buf, value, offset, littleEndian, noAssert) {
  1312. if (!noAssert) {
  1313. checkIEEE754(buf, value, offset, 4, 3.4028234663852886e+38, -3.4028234663852886e+38)
  1314. }
  1315. ieee754.write(buf, value, offset, littleEndian, 23, 4)
  1316. return offset + 4
  1317. }
  1318. Buffer.prototype.writeFloatLE = function writeFloatLE (value, offset, noAssert) {
  1319. return writeFloat(this, value, offset, true, noAssert)
  1320. }
  1321. Buffer.prototype.writeFloatBE = function writeFloatBE (value, offset, noAssert) {
  1322. return writeFloat(this, value, offset, false, noAssert)
  1323. }
  1324. function writeDouble (buf, value, offset, littleEndian, noAssert) {
  1325. if (!noAssert) {
  1326. checkIEEE754(buf, value, offset, 8, 1.7976931348623157E+308, -1.7976931348623157E+308)
  1327. }
  1328. ieee754.write(buf, value, offset, littleEndian, 52, 8)
  1329. return offset + 8
  1330. }
  1331. Buffer.prototype.writeDoubleLE = function writeDoubleLE (value, offset, noAssert) {
  1332. return writeDouble(this, value, offset, true, noAssert)
  1333. }
  1334. Buffer.prototype.writeDoubleBE = function writeDoubleBE (value, offset, noAssert) {
  1335. return writeDouble(this, value, offset, false, noAssert)
  1336. }
  1337. // copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
  1338. Buffer.prototype.copy = function copy (target, targetStart, start, end) {
  1339. if (!start) start = 0
  1340. if (!end && end !== 0) end = this.length
  1341. if (targetStart >= target.length) targetStart = target.length
  1342. if (!targetStart) targetStart = 0
  1343. if (end > 0 && end < start) end = start
  1344. // Copy 0 bytes; we're done
  1345. if (end === start) return 0
  1346. if (target.length === 0 || this.length === 0) return 0
  1347. // Fatal error conditions
  1348. if (targetStart < 0) {
  1349. throw new RangeError('targetStart out of bounds')
  1350. }
  1351. if (start < 0 || start >= this.length) throw new RangeError('sourceStart out of bounds')
  1352. if (end < 0) throw new RangeError('sourceEnd out of bounds')
  1353. // Are we oob?
  1354. if (end > this.length) end = this.length
  1355. if (target.length - targetStart < end - start) {
  1356. end = target.length - targetStart + start
  1357. }
  1358. var len = end - start
  1359. var i
  1360. if (this === target && start < targetStart && targetStart < end) {
  1361. // descending copy from end
  1362. for (i = len - 1; i >= 0; i--) {
  1363. target[i + targetStart] = this[i + start]
  1364. }
  1365. } else if (len < 1000 || !Buffer.TYPED_ARRAY_SUPPORT) {
  1366. // ascending copy from start
  1367. for (i = 0; i < len; i++) {
  1368. target[i + targetStart] = this[i + start]
  1369. }
  1370. } else {
  1371. Uint8Array.prototype.set.call(
  1372. target,
  1373. this.subarray(start, start + len),
  1374. targetStart
  1375. )
  1376. }
  1377. return len
  1378. }
  1379. // Usage:
  1380. // buffer.fill(number[, offset[, end]])
  1381. // buffer.fill(buffer[, offset[, end]])
  1382. // buffer.fill(string[, offset[, end]][, encoding])
  1383. Buffer.prototype.fill = function fill (val, start, end, encoding) {
  1384. // Handle string cases:
  1385. if (typeof val === 'string') {
  1386. if (typeof start === 'string') {
  1387. encoding = start
  1388. start = 0
  1389. end = this.length
  1390. } else if (typeof end === 'string') {
  1391. encoding = end
  1392. end = this.length
  1393. }
  1394. if (val.length === 1) {
  1395. var code = val.charCodeAt(0)
  1396. if (code < 256) {
  1397. val = code
  1398. }
  1399. }
  1400. if (encoding !== undefined && typeof encoding !== 'string') {
  1401. throw new TypeError('encoding must be a string')
  1402. }
  1403. if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
  1404. throw new TypeError('Unknown encoding: ' + encoding)
  1405. }
  1406. } else if (typeof val === 'number') {
  1407. val = val & 255
  1408. }
  1409. // Invalid ranges are not set to a default, so can range check early.
  1410. if (start < 0 || this.length < start || this.length < end) {
  1411. throw new RangeError('Out of range index')
  1412. }
  1413. if (end <= start) {
  1414. return this
  1415. }
  1416. start = start >>> 0
  1417. end = end === undefined ? this.length : end >>> 0
  1418. if (!val) val = 0
  1419. var i
  1420. if (typeof val === 'number') {
  1421. for (i = start; i < end; i++) {
  1422. this[i] = val
  1423. }
  1424. } else {
  1425. var bytes = Buffer.isBuffer(val)
  1426. ? val
  1427. : utf8ToBytes(new Buffer(val, encoding).toString())
  1428. var len = bytes.length
  1429. for (i = 0; i < end - start; i++) {
  1430. this[i + start] = bytes[i % len]
  1431. }
  1432. }
  1433. return this
  1434. }
  1435. // HELPER FUNCTIONS
  1436. // ================
  1437. var INVALID_BASE64_RE = /[^+\/0-9A-Za-z-_]/g
  1438. function base64clean (str) {
  1439. // Node strips out invalid characters like \n and \t from the string, base64-js does not
  1440. str = stringtrim(str).replace(INVALID_BASE64_RE, '')
  1441. // Node converts strings with length < 2 to ''
  1442. if (str.length < 2) return ''
  1443. // Node allows for non-padded base64 strings (missing trailing ===), base64-js does not
  1444. while (str.length % 4 !== 0) {
  1445. str = str + '='
  1446. }
  1447. return str
  1448. }
  1449. function stringtrim (str) {
  1450. if (str.trim) return str.trim()
  1451. return str.replace(/^\s+|\s+$/g, '')
  1452. }
  1453. function toHex (n) {
  1454. if (n < 16) return '0' + n.toString(16)
  1455. return n.toString(16)
  1456. }
  1457. function utf8ToBytes (string, units) {
  1458. units = units || Infinity
  1459. var codePoint
  1460. var length = string.length
  1461. var leadSurrogate = null
  1462. var bytes = []
  1463. for (var i = 0; i < length; i++) {
  1464. codePoint = string.charCodeAt(i)
  1465. // is surrogate component
  1466. if (codePoint > 0xD7FF && codePoint < 0xE000) {
  1467. // last char was a lead
  1468. if (!leadSurrogate) {
  1469. // no lead yet
  1470. if (codePoint > 0xDBFF) {
  1471. // unexpected trail
  1472. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1473. continue
  1474. } else if (i + 1 === length) {
  1475. // unpaired lead
  1476. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1477. continue
  1478. }
  1479. // valid lead
  1480. leadSurrogate = codePoint
  1481. continue
  1482. }
  1483. // 2 leads in a row
  1484. if (codePoint < 0xDC00) {
  1485. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1486. leadSurrogate = codePoint
  1487. continue
  1488. }
  1489. // valid surrogate pair
  1490. codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000
  1491. } else if (leadSurrogate) {
  1492. // valid bmp char, but last char was a lead
  1493. if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
  1494. }
  1495. leadSurrogate = null
  1496. // encode utf8
  1497. if (codePoint < 0x80) {
  1498. if ((units -= 1) < 0) break
  1499. bytes.push(codePoint)
  1500. } else if (codePoint < 0x800) {
  1501. if ((units -= 2) < 0) break
  1502. bytes.push(
  1503. codePoint >> 0x6 | 0xC0,
  1504. codePoint & 0x3F | 0x80
  1505. )
  1506. } else if (codePoint < 0x10000) {
  1507. if ((units -= 3) < 0) break
  1508. bytes.push(
  1509. codePoint >> 0xC | 0xE0,
  1510. codePoint >> 0x6 & 0x3F | 0x80,
  1511. codePoint & 0x3F | 0x80
  1512. )
  1513. } else if (codePoint < 0x110000) {
  1514. if ((units -= 4) < 0) break
  1515. bytes.push(
  1516. codePoint >> 0x12 | 0xF0,
  1517. codePoint >> 0xC & 0x3F | 0x80,
  1518. codePoint >> 0x6 & 0x3F | 0x80,
  1519. codePoint & 0x3F | 0x80
  1520. )
  1521. } else {
  1522. throw new Error('Invalid code point')
  1523. }
  1524. }
  1525. return bytes
  1526. }
  1527. function asciiToBytes (str) {
  1528. var byteArray = []
  1529. for (var i = 0; i < str.length; i++) {
  1530. // Node's code seems to be doing this and not & 0x7F..
  1531. byteArray.push(str.charCodeAt(i) & 0xFF)
  1532. }
  1533. return byteArray
  1534. }
  1535. function utf16leToBytes (str, units) {
  1536. var c, hi, lo
  1537. var byteArray = []
  1538. for (var i = 0; i < str.length; i++) {
  1539. if ((units -= 2) < 0) break
  1540. c = str.charCodeAt(i)
  1541. hi = c >> 8
  1542. lo = c % 256
  1543. byteArray.push(lo)
  1544. byteArray.push(hi)
  1545. }
  1546. return byteArray
  1547. }
  1548. function base64ToBytes (str) {
  1549. return base64.toByteArray(base64clean(str))
  1550. }
  1551. function blitBuffer (src, dst, offset, length) {
  1552. for (var i = 0; i < length; i++) {
  1553. if ((i + offset >= dst.length) || (i >= src.length)) break
  1554. dst[i + offset] = src[i]
  1555. }
  1556. return i
  1557. }
  1558. function isnan (val) {
  1559. return val !== val // eslint-disable-line no-self-compare
  1560. }
  1561. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  1562. },{"base64-js":1,"ieee754":4,"isarray":5}],4:[function(require,module,exports){
  1563. exports.read = function (buffer, offset, isLE, mLen, nBytes) {
  1564. var e, m
  1565. var eLen = nBytes * 8 - mLen - 1
  1566. var eMax = (1 << eLen) - 1
  1567. var eBias = eMax >> 1
  1568. var nBits = -7
  1569. var i = isLE ? (nBytes - 1) : 0
  1570. var d = isLE ? -1 : 1
  1571. var s = buffer[offset + i]
  1572. i += d
  1573. e = s & ((1 << (-nBits)) - 1)
  1574. s >>= (-nBits)
  1575. nBits += eLen
  1576. for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  1577. m = e & ((1 << (-nBits)) - 1)
  1578. e >>= (-nBits)
  1579. nBits += mLen
  1580. for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8) {}
  1581. if (e === 0) {
  1582. e = 1 - eBias
  1583. } else if (e === eMax) {
  1584. return m ? NaN : ((s ? -1 : 1) * Infinity)
  1585. } else {
  1586. m = m + Math.pow(2, mLen)
  1587. e = e - eBias
  1588. }
  1589. return (s ? -1 : 1) * m * Math.pow(2, e - mLen)
  1590. }
  1591. exports.write = function (buffer, value, offset, isLE, mLen, nBytes) {
  1592. var e, m, c
  1593. var eLen = nBytes * 8 - mLen - 1
  1594. var eMax = (1 << eLen) - 1
  1595. var eBias = eMax >> 1
  1596. var rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0)
  1597. var i = isLE ? 0 : (nBytes - 1)
  1598. var d = isLE ? 1 : -1
  1599. var s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0
  1600. value = Math.abs(value)
  1601. if (isNaN(value) || value === Infinity) {
  1602. m = isNaN(value) ? 1 : 0
  1603. e = eMax
  1604. } else {
  1605. e = Math.floor(Math.log(value) / Math.LN2)
  1606. if (value * (c = Math.pow(2, -e)) < 1) {
  1607. e--
  1608. c *= 2
  1609. }
  1610. if (e + eBias >= 1) {
  1611. value += rt / c
  1612. } else {
  1613. value += rt * Math.pow(2, 1 - eBias)
  1614. }
  1615. if (value * c >= 2) {
  1616. e++
  1617. c /= 2
  1618. }
  1619. if (e + eBias >= eMax) {
  1620. m = 0
  1621. e = eMax
  1622. } else if (e + eBias >= 1) {
  1623. m = (value * c - 1) * Math.pow(2, mLen)
  1624. e = e + eBias
  1625. } else {
  1626. m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen)
  1627. e = 0
  1628. }
  1629. }
  1630. for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8) {}
  1631. e = (e << mLen) | m
  1632. eLen += mLen
  1633. for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8) {}
  1634. buffer[offset + i - d] |= s * 128
  1635. }
  1636. },{}],5:[function(require,module,exports){
  1637. var toString = {}.toString;
  1638. module.exports = Array.isArray || function (arr) {
  1639. return toString.call(arr) == '[object Array]';
  1640. };
  1641. },{}],6:[function(require,module,exports){
  1642. (function (Buffer,__dirname){
  1643. 'use strict';
  1644. /**
  1645. * Typo is a JavaScript implementation of a spellchecker using hunspell-style
  1646. * dictionaries.
  1647. */
  1648. /**
  1649. * Typo constructor.
  1650. *
  1651. * @param {String} [dictionary] The locale code of the dictionary being used. e.g.,
  1652. * "en_US". This is only used to auto-load dictionaries.
  1653. * @param {String} [affData] The data from the dictionary's .aff file. If omitted
  1654. * and Typo.js is being used in a Chrome extension, the .aff
  1655. * file will be loaded automatically from
  1656. * lib/typo/dictionaries/[dictionary]/[dictionary].aff
  1657. * In other environments, it will be loaded from
  1658. * [settings.dictionaryPath]/dictionaries/[dictionary]/[dictionary].aff
  1659. * @param {String} [wordsData] The data from the dictionary's .dic file. If omitted
  1660. * and Typo.js is being used in a Chrome extension, the .dic
  1661. * file will be loaded automatically from
  1662. * lib/typo/dictionaries/[dictionary]/[dictionary].dic
  1663. * In other environments, it will be loaded from
  1664. * [settings.dictionaryPath]/dictionaries/[dictionary]/[dictionary].dic
  1665. * @param {Object} [settings] Constructor settings. Available properties are:
  1666. * {String} [dictionaryPath]: path to load dictionary from in non-chrome
  1667. * environment.
  1668. * {Object} [flags]: flag information.
  1669. *
  1670. *
  1671. * @returns {Typo} A Typo object.
  1672. */
  1673. var Typo = function (dictionary, affData, wordsData, settings) {
  1674. settings = settings || {};
  1675. this.dictionary = null;
  1676. this.rules = {};
  1677. this.dictionaryTable = {};
  1678. this.compoundRules = [];
  1679. this.compoundRuleCodes = {};
  1680. this.replacementTable = [];
  1681. this.flags = settings.flags || {};
  1682. if (dictionary) {
  1683. this.dictionary = dictionary;
  1684. if (typeof window !== 'undefined' && 'chrome' in window && 'extension' in window.chrome && 'getURL' in window.chrome.extension) {
  1685. if (!affData) affData = this._readFile(chrome.extension.getURL("lib/typo/dictionaries/" + dictionary + "/" + dictionary + ".aff"));
  1686. if (!wordsData) wordsData = this._readFile(chrome.extension.getURL("lib/typo/dictionaries/" + dictionary + "/" + dictionary + ".dic"));
  1687. } else {
  1688. if (settings.dictionaryPath) {
  1689. var path = settings.dictionaryPath;
  1690. }
  1691. else if (typeof __dirname !== 'undefined') {
  1692. var path = __dirname + '/dictionaries';
  1693. }
  1694. else {
  1695. var path = './dictionaries';
  1696. }
  1697. if (!affData) affData = this._readFile(path + "/" + dictionary + "/" + dictionary + ".aff");
  1698. if (!wordsData) wordsData = this._readFile(path + "/" + dictionary + "/" + dictionary + ".dic");
  1699. }
  1700. this.rules = this._parseAFF(affData);
  1701. // Save the rule codes that are used in compound rules.
  1702. this.compoundRuleCodes = {};
  1703. for (var i = 0, _len = this.compoundRules.length; i < _len; i++) {
  1704. var rule = this.compoundRules[i];
  1705. for (var j = 0, _jlen = rule.length; j < _jlen; j++) {
  1706. this.compoundRuleCodes[rule[j]] = [];
  1707. }
  1708. }
  1709. // If we add this ONLYINCOMPOUND flag to this.compoundRuleCodes, then _parseDIC
  1710. // will do the work of saving the list of words that are compound-only.
  1711. if ("ONLYINCOMPOUND" in this.flags) {
  1712. this.compoundRuleCodes[this.flags.ONLYINCOMPOUND] = [];
  1713. }
  1714. this.dictionaryTable = this._parseDIC(wordsData);
  1715. // Get rid of any codes from the compound rule codes that are never used
  1716. // (or that were special regex characters). Not especially necessary...
  1717. for (var i in this.compoundRuleCodes) {
  1718. if (this.compoundRuleCodes[i].length == 0) {
  1719. delete this.compoundRuleCodes[i];
  1720. }
  1721. }
  1722. // Build the full regular expressions for each compound rule.
  1723. // I have a feeling (but no confirmation yet) that this method of
  1724. // testing for compound words is probably slow.
  1725. for (var i = 0, _len = this.compoundRules.length; i < _len; i++) {
  1726. var ruleText = this.compoundRules[i];
  1727. var expressionText = "";
  1728. for (var j = 0, _jlen = ruleText.length; j < _jlen; j++) {
  1729. var character = ruleText[j];
  1730. if (character in this.compoundRuleCodes) {
  1731. expressionText += "(" + this.compoundRuleCodes[character].join("|") + ")";
  1732. }
  1733. else {
  1734. expressionText += character;
  1735. }
  1736. }
  1737. this.compoundRules[i] = new RegExp(expressionText, "i");
  1738. }
  1739. }
  1740. return this;
  1741. };
  1742. Typo.prototype = {
  1743. /**
  1744. * Loads a Typo instance from a hash of all of the Typo properties.
  1745. *
  1746. * @param object obj A hash of Typo properties, probably gotten from a JSON.parse(JSON.stringify(typo_instance)).
  1747. */
  1748. load : function (obj) {
  1749. for (var i in obj) {
  1750. this[i] = obj[i];
  1751. }
  1752. return this;
  1753. },
  1754. /**
  1755. * Read the contents of a file.
  1756. *
  1757. * @param {String} path The path (relative) to the file.
  1758. * @param {String} [charset="ISO8859-1"] The expected charset of the file
  1759. * @returns string The file data.
  1760. */
  1761. _readFile : function (path, charset) {
  1762. if (!charset) charset = "utf8";
  1763. if (typeof XMLHttpRequest !== 'undefined') {
  1764. var req = new XMLHttpRequest();
  1765. req.open("GET", path, false);
  1766. if (req.overrideMimeType)
  1767. req.overrideMimeType("text/plain; charset=" + charset);
  1768. req.send(null);
  1769. return req.responseText;
  1770. }
  1771. else if (typeof require !== 'undefined') {
  1772. // Node.js
  1773. var fs = require("fs");
  1774. try {
  1775. if (fs.existsSync(path)) {
  1776. var stats = fs.statSync(path);
  1777. var fileDescriptor = fs.openSync(path, 'r');
  1778. var buffer = new Buffer(stats.size);
  1779. fs.readSync(fileDescriptor, buffer, 0, buffer.length, null);
  1780. return buffer.toString(charset, 0, buffer.length);
  1781. }
  1782. else {
  1783. console.log("Path " + path + " does not exist.");
  1784. }
  1785. } catch (e) {
  1786. console.log(e);
  1787. return '';
  1788. }
  1789. }
  1790. },
  1791. /**
  1792. * Parse the rules out from a .aff file.
  1793. *
  1794. * @param {String} data The contents of the affix file.
  1795. * @returns object The rules from the file.
  1796. */
  1797. _parseAFF : function (data) {
  1798. var rules = {};
  1799. // Remove comment lines
  1800. data = this._removeAffixComments(data);
  1801. var lines = data.split("\n");
  1802. for (var i = 0, _len = lines.length; i < _len; i++) {
  1803. var line = lines[i];
  1804. var definitionParts = line.split(/\s+/);
  1805. var ruleType = definitionParts[0];
  1806. if (ruleType == "PFX" || ruleType == "SFX") {
  1807. var ruleCode = definitionParts[1];
  1808. var combineable = definitionParts[2];
  1809. var numEntries = parseInt(definitionParts[3], 10);
  1810. var entries = [];
  1811. for (var j = i + 1, _jlen = i + 1 + numEntries; j < _jlen; j++) {
  1812. var line = lines[j];
  1813. var lineParts = line.split(/\s+/);
  1814. var charactersToRemove = lineParts[2];
  1815. var additionParts = lineParts[3].split("/");
  1816. var charactersToAdd = additionParts[0];
  1817. if (charactersToAdd === "0") charactersToAdd = "";
  1818. var continuationClasses = this.parseRuleCodes(additionParts[1]);
  1819. var regexToMatch = lineParts[4];
  1820. var entry = {};
  1821. entry.add = charactersToAdd;
  1822. if (continuationClasses.length > 0) entry.continuationClasses = continuationClasses;
  1823. if (regexToMatch !== ".") {
  1824. if (ruleType === "SFX") {
  1825. entry.match = new RegExp(regexToMatch + "$");
  1826. }
  1827. else {
  1828. entry.match = new RegExp("^" + regexToMatch);
  1829. }
  1830. }
  1831. if (charactersToRemove != "0") {
  1832. if (ruleType === "SFX") {
  1833. entry.remove = new RegExp(charactersToRemove + "$");
  1834. }
  1835. else {
  1836. entry.remove = charactersToRemove;
  1837. }
  1838. }
  1839. entries.push(entry);
  1840. }
  1841. rules[ruleCode] = { "type" : ruleType, "combineable" : (combineable == "Y"), "entries" : entries };
  1842. i += numEntries;
  1843. }
  1844. else if (ruleType === "COMPOUNDRULE") {
  1845. var numEntries = parseInt(definitionParts[1], 10);
  1846. for (var j = i + 1, _jlen = i + 1 + numEntries; j < _jlen; j++) {
  1847. var line = lines[j];
  1848. var lineParts = line.split(/\s+/);
  1849. this.compoundRules.push(lineParts[1]);
  1850. }
  1851. i += numEntries;
  1852. }
  1853. else if (ruleType === "REP") {
  1854. var lineParts = line.split(/\s+/);
  1855. if (lineParts.length === 3) {
  1856. this.replacementTable.push([ lineParts[1], lineParts[2] ]);
  1857. }
  1858. }
  1859. else {
  1860. // ONLYINCOMPOUND
  1861. // COMPOUNDMIN
  1862. // FLAG
  1863. // KEEPCASE
  1864. // NEEDAFFIX
  1865. this.flags[ruleType] = definitionParts[1];
  1866. }
  1867. }
  1868. return rules;
  1869. },
  1870. /**
  1871. * Removes comment lines and then cleans up blank lines and trailing whitespace.
  1872. *
  1873. * @param {String} data The data from an affix file.
  1874. * @return {String} The cleaned-up data.
  1875. */
  1876. _removeAffixComments : function (data) {
  1877. // Remove comments
  1878. data = data.replace(/#.*$/mg, "");
  1879. // Trim each line
  1880. data = data.replace(/^\s\s*/m, '').replace(/\s\s*$/m, '');
  1881. // Remove blank lines.
  1882. data = data.replace(/\n{2,}/g, "\n");
  1883. // Trim the entire string
  1884. data = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  1885. return data;
  1886. },
  1887. /**
  1888. * Parses the words out from the .dic file.
  1889. *
  1890. * @param {String} data The data from the dictionary file.
  1891. * @returns object The lookup table containing all of the words and
  1892. * word forms from the dictionary.
  1893. */
  1894. _parseDIC : function (data) {
  1895. data = this._removeDicComments(data);
  1896. var lines = data.split("\n");
  1897. var dictionaryTable = {};
  1898. function addWord(word, rules) {
  1899. // Some dictionaries will list the same word multiple times with different rule sets.
  1900. if (!(word in dictionaryTable) || typeof dictionaryTable[word] != 'object') {
  1901. dictionaryTable[word] = [];
  1902. }
  1903. dictionaryTable[word].push(rules);
  1904. }
  1905. // The first line is the number of words in the dictionary.
  1906. for (var i = 1, _len = lines.length; i < _len; i++) {
  1907. var line = lines[i];
  1908. var parts = line.split("/", 2);
  1909. var word = parts[0];
  1910. // Now for each affix rule, generate that form of the word.
  1911. if (parts.length > 1) {
  1912. var ruleCodesArray = this.parseRuleCodes(parts[1]);
  1913. // Save the ruleCodes for compound word situations.
  1914. if (!("NEEDAFFIX" in this.flags) || ruleCodesArray.indexOf(this.flags.NEEDAFFIX) == -1) {
  1915. addWord(word, ruleCodesArray);
  1916. }
  1917. for (var j = 0, _jlen = ruleCodesArray.length; j < _jlen; j++) {
  1918. var code = ruleCodesArray[j];
  1919. var rule = this.rules[code];
  1920. if (rule) {
  1921. var newWords = this._applyRule(word, rule);
  1922. for (var ii = 0, _iilen = newWords.length; ii < _iilen; ii++) {
  1923. var newWord = newWords[ii];
  1924. addWord(newWord, []);
  1925. if (rule.combineable) {
  1926. for (var k = j + 1; k < _jlen; k++) {
  1927. var combineCode = ruleCodesArray[k];
  1928. var combineRule = this.rules[combineCode];
  1929. if (combineRule) {
  1930. if (combineRule.combineable && (rule.type != combineRule.type)) {
  1931. var otherNewWords = this._applyRule(newWord, combineRule);
  1932. for (var iii = 0, _iiilen = otherNewWords.length; iii < _iiilen; iii++) {
  1933. var otherNewWord = otherNewWords[iii];
  1934. addWord(otherNewWord, []);
  1935. }
  1936. }
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. if (code in this.compoundRuleCodes) {
  1943. this.compoundRuleCodes[code].push(word);
  1944. }
  1945. }
  1946. }
  1947. else {
  1948. addWord(word.trim(), []);
  1949. }
  1950. }
  1951. return dictionaryTable;
  1952. },
  1953. /**
  1954. * Removes comment lines and then cleans up blank lines and trailing whitespace.
  1955. *
  1956. * @param {String} data The data from a .dic file.
  1957. * @return {String} The cleaned-up data.
  1958. */
  1959. _removeDicComments : function (data) {
  1960. // I can't find any official documentation on it, but at least the de_DE
  1961. // dictionary uses tab-indented lines as comments.
  1962. // Remove comments
  1963. data = data.replace(/^\t.*$/mg, "");
  1964. return data;
  1965. },
  1966. parseRuleCodes : function (textCodes) {
  1967. if (!textCodes) {
  1968. return [];
  1969. }
  1970. else if (!("FLAG" in this.flags)) {
  1971. return textCodes.split("");
  1972. }
  1973. else if (this.flags.FLAG === "long") {
  1974. var flags = [];
  1975. for (var i = 0, _len = textCodes.length; i < _len; i += 2) {
  1976. flags.push(textCodes.substr(i, 2));
  1977. }
  1978. return flags;
  1979. }
  1980. else if (this.flags.FLAG === "num") {
  1981. return textCode.split(",");
  1982. }
  1983. },
  1984. /**
  1985. * Applies an affix rule to a word.
  1986. *
  1987. * @param {String} word The base word.
  1988. * @param {Object} rule The affix rule.
  1989. * @returns {String[]} The new words generated by the rule.
  1990. */
  1991. _applyRule : function (word, rule) {
  1992. var entries = rule.entries;
  1993. var newWords = [];
  1994. for (var i = 0, _len = entries.length; i < _len; i++) {
  1995. var entry = entries[i];
  1996. if (!entry.match || word.match(entry.match)) {
  1997. var newWord = word;
  1998. if (entry.remove) {
  1999. newWord = newWord.replace(entry.remove, "");
  2000. }
  2001. if (rule.type === "SFX") {
  2002. newWord = newWord + entry.add;
  2003. }
  2004. else {
  2005. newWord = entry.add + newWord;
  2006. }
  2007. newWords.push(newWord);
  2008. if ("continuationClasses" in entry) {
  2009. for (var j = 0, _jlen = entry.continuationClasses.length; j < _jlen; j++) {
  2010. var continuationRule = this.rules[entry.continuationClasses[j]];
  2011. if (continuationRule) {
  2012. newWords = newWords.concat(this._applyRule(newWord, continuationRule));
  2013. }
  2014. /*
  2015. else {
  2016. // This shouldn't happen, but it does, at least in the de_DE dictionary.
  2017. // I think the author mistakenly supplied lower-case rule codes instead
  2018. // of upper-case.
  2019. }
  2020. */
  2021. }
  2022. }
  2023. }
  2024. }
  2025. return newWords;
  2026. },
  2027. /**
  2028. * Checks whether a word or a capitalization variant exists in the current dictionary.
  2029. * The word is trimmed and several variations of capitalizations are checked.
  2030. * If you want to check a word without any changes made to it, call checkExact()
  2031. *
  2032. * @see http://blog.stevenlevithan.com/archives/faster-trim-javascript re:trimming function
  2033. *
  2034. * @param {String} aWord The word to check.
  2035. * @returns {Boolean}
  2036. */
  2037. check : function (aWord) {
  2038. // Remove leading and trailing whitespace
  2039. var trimmedWord = aWord.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  2040. if (this.checkExact(trimmedWord)) {
  2041. return true;
  2042. }
  2043. // The exact word is not in the dictionary.
  2044. if (trimmedWord.toUpperCase() === trimmedWord) {
  2045. // The word was supplied in all uppercase.
  2046. // Check for a capitalized form of the word.
  2047. var capitalizedWord = trimmedWord[0] + trimmedWord.substring(1).toLowerCase();
  2048. if (this.hasFlag(capitalizedWord, "KEEPCASE")) {
  2049. // Capitalization variants are not allowed for this word.
  2050. return false;
  2051. }
  2052. if (this.checkExact(capitalizedWord)) {
  2053. return true;
  2054. }
  2055. }
  2056. var lowercaseWord = trimmedWord.toLowerCase();
  2057. if (lowercaseWord !== trimmedWord) {
  2058. if (this.hasFlag(lowercaseWord, "KEEPCASE")) {
  2059. // Capitalization variants are not allowed for this word.
  2060. return false;
  2061. }
  2062. // Check for a lowercase form
  2063. if (this.checkExact(lowercaseWord)) {
  2064. return true;
  2065. }
  2066. }
  2067. return false;
  2068. },
  2069. /**
  2070. * Checks whether a word exists in the current dictionary.
  2071. *
  2072. * @param {String} word The word to check.
  2073. * @returns {Boolean}
  2074. */
  2075. checkExact : function (word) {
  2076. var ruleCodes = this.dictionaryTable[word];
  2077. if (typeof ruleCodes === 'undefined') {
  2078. // Check if this might be a compound word.
  2079. if ("COMPOUNDMIN" in this.flags && word.length >= this.flags.COMPOUNDMIN) {
  2080. for (var i = 0, _len = this.compoundRules.length; i < _len; i++) {
  2081. if (word.match(this.compoundRules[i])) {
  2082. return true;
  2083. }
  2084. }
  2085. }
  2086. return false;
  2087. }
  2088. else if (typeof ruleCodes === 'object') { // this.dictionary['hasOwnProperty'] will be a function.
  2089. for (var i = 0, _len = ruleCodes.length; i < _len; i++) {
  2090. if (!this.hasFlag(word, "ONLYINCOMPOUND", ruleCodes[i])) {
  2091. return true;
  2092. }
  2093. }
  2094. return false;
  2095. }
  2096. },
  2097. /**
  2098. * Looks up whether a given word is flagged with a given flag.
  2099. *
  2100. * @param {String} word The word in question.
  2101. * @param {String} flag The flag in question.
  2102. * @return {Boolean}
  2103. */
  2104. hasFlag : function (word, flag, wordFlags) {
  2105. if (flag in this.flags) {
  2106. if (typeof wordFlags === 'undefined') {
  2107. var wordFlags = Array.prototype.concat.apply([], this.dictionaryTable[word]);
  2108. }
  2109. if (wordFlags && wordFlags.indexOf(this.flags[flag]) !== -1) {
  2110. return true;
  2111. }
  2112. }
  2113. return false;
  2114. },
  2115. /**
  2116. * Returns a list of suggestions for a misspelled word.
  2117. *
  2118. * @see http://www.norvig.com/spell-correct.html for the basis of this suggestor.
  2119. * This suggestor is primitive, but it works.
  2120. *
  2121. * @param {String} word The misspelling.
  2122. * @param {Number} [limit=5] The maximum number of suggestions to return.
  2123. * @returns {String[]} The array of suggestions.
  2124. */
  2125. alphabet : "",
  2126. suggest : function (word, limit) {
  2127. if (!limit) limit = 5;
  2128. if (this.check(word)) return [];
  2129. // Check the replacement table.
  2130. for (var i = 0, _len = this.replacementTable.length; i < _len; i++) {
  2131. var replacementEntry = this.replacementTable[i];
  2132. if (word.indexOf(replacementEntry[0]) !== -1) {
  2133. var correctedWord = word.replace(replacementEntry[0], replacementEntry[1]);
  2134. if (this.check(correctedWord)) {
  2135. return [ correctedWord ];
  2136. }
  2137. }
  2138. }
  2139. var self = this;
  2140. self.alphabet = "abcdefghijklmnopqrstuvwxyz";
  2141. /*
  2142. if (!self.alphabet) {
  2143. // Use the alphabet as implicitly defined by the words in the dictionary.
  2144. var alphaHash = {};
  2145. for (var i in self.dictionaryTable) {
  2146. for (var j = 0, _len = i.length; j < _len; j++) {
  2147. alphaHash[i[j]] = true;
  2148. }
  2149. }
  2150. for (var i in alphaHash) {
  2151. self.alphabet += i;
  2152. }
  2153. var alphaArray = self.alphabet.split("");
  2154. alphaArray.sort();
  2155. self.alphabet = alphaArray.join("");
  2156. }
  2157. */
  2158. function edits1(words) {
  2159. var rv = [];
  2160. for (var ii = 0, _iilen = words.length; ii < _iilen; ii++) {
  2161. var word = words[ii];
  2162. var splits = [];
  2163. for (var i = 0, _len = word.length + 1; i < _len; i++) {
  2164. splits.push([ word.substring(0, i), word.substring(i, word.length) ]);
  2165. }
  2166. var deletes = [];
  2167. for (var i = 0, _len = splits.length; i < _len; i++) {
  2168. var s = splits[i];
  2169. if (s[1]) {
  2170. deletes.push(s[0] + s[1].substring(1));
  2171. }
  2172. }
  2173. var transposes = [];
  2174. for (var i = 0, _len = splits.length; i < _len; i++) {
  2175. var s = splits[i];
  2176. if (s[1].length > 1) {
  2177. transposes.push(s[0] + s[1][1] + s[1][0] + s[1].substring(2));
  2178. }
  2179. }
  2180. var replaces = [];
  2181. for (var i = 0, _len = splits.length; i < _len; i++) {
  2182. var s = splits[i];
  2183. if (s[1]) {
  2184. for (var j = 0, _jlen = self.alphabet.length; j < _jlen; j++) {
  2185. replaces.push(s[0] + self.alphabet[j] + s[1].substring(1));
  2186. }
  2187. }
  2188. }
  2189. var inserts = [];
  2190. for (var i = 0, _len = splits.length; i < _len; i++) {
  2191. var s = splits[i];
  2192. if (s[1]) {
  2193. for (var j = 0, _jlen = self.alphabet.length; j < _jlen; j++) {
  2194. replaces.push(s[0] + self.alphabet[j] + s[1]);
  2195. }
  2196. }
  2197. }
  2198. rv = rv.concat(deletes);
  2199. rv = rv.concat(transposes);
  2200. rv = rv.concat(replaces);
  2201. rv = rv.concat(inserts);
  2202. }
  2203. return rv;
  2204. }
  2205. function known(words) {
  2206. var rv = [];
  2207. for (var i = 0; i < words.length; i++) {
  2208. if (self.check(words[i])) {
  2209. rv.push(words[i]);
  2210. }
  2211. }
  2212. return rv;
  2213. }
  2214. function correct(word) {
  2215. // Get the edit-distance-1 and edit-distance-2 forms of this word.
  2216. var ed1 = edits1([word]);
  2217. var ed2 = edits1(ed1);
  2218. var corrections = known(ed1).concat(known(ed2));
  2219. // Sort the edits based on how many different ways they were created.
  2220. var weighted_corrections = {};
  2221. for (var i = 0, _len = corrections.length; i < _len; i++) {
  2222. if (!(corrections[i] in weighted_corrections)) {
  2223. weighted_corrections[corrections[i]] = 1;
  2224. }
  2225. else {
  2226. weighted_corrections[corrections[i]] += 1;
  2227. }
  2228. }
  2229. var sorted_corrections = [];
  2230. for (var i in weighted_corrections) {
  2231. sorted_corrections.push([ i, weighted_corrections[i] ]);
  2232. }
  2233. function sorter(a, b) {
  2234. if (a[1] < b[1]) {
  2235. return -1;
  2236. }
  2237. return 1;
  2238. }
  2239. sorted_corrections.sort(sorter).reverse();
  2240. var rv = [];
  2241. for (var i = 0, _len = Math.min(limit, sorted_corrections.length); i < _len; i++) {
  2242. if (!self.hasFlag(sorted_corrections[i][0], "NOSUGGEST")) {
  2243. rv.push(sorted_corrections[i][0]);
  2244. }
  2245. }
  2246. return rv;
  2247. }
  2248. return correct(word);
  2249. }
  2250. };
  2251. // Support for use as a node.js module.
  2252. if (typeof module !== 'undefined') {
  2253. module.exports = Typo;
  2254. }
  2255. }).call(this,require("buffer").Buffer,"/node_modules/typo-js")
  2256. },{"buffer":3,"fs":2}],7:[function(require,module,exports){
  2257. // Use strict mode (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode)
  2258. "use strict";
  2259. // Requires
  2260. var Typo = require("typo-js");
  2261. // Create function
  2262. function CodeMirrorSpellChecker(options) {
  2263. // Initialize
  2264. options = options || {};
  2265. // Verify
  2266. if(typeof options.codeMirrorInstance !== "function" || typeof options.codeMirrorInstance.defineMode !== "function") {
  2267. console.log("CodeMirror Spell Checker: You must provide an instance of CodeMirror via the option `codeMirrorInstance`");
  2268. return;
  2269. }
  2270. // Because some browsers don't support this functionality yet
  2271. if(!String.prototype.includes) {
  2272. String.prototype.includes = function() {
  2273. "use strict";
  2274. return String.prototype.indexOf.apply(this, arguments) !== -1;
  2275. };
  2276. }
  2277. // Define the new mode
  2278. options.codeMirrorInstance.defineMode("spell-checker", function(config) {
  2279. // Load AFF/DIC data
  2280. if(!CodeMirrorSpellChecker.aff_loading) {
  2281. CodeMirrorSpellChecker.aff_loading = true;
  2282. var xhr_aff = new XMLHttpRequest();
  2283. xhr_aff.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.aff", true);
  2284. xhr_aff.onload = function() {
  2285. if(xhr_aff.readyState === 4 && xhr_aff.status === 200) {
  2286. CodeMirrorSpellChecker.aff_data = xhr_aff.responseText;
  2287. CodeMirrorSpellChecker.num_loaded++;
  2288. if(CodeMirrorSpellChecker.num_loaded == 2) {
  2289. CodeMirrorSpellChecker.typo = new Typo("en_US", CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
  2290. platform: "any"
  2291. });
  2292. }
  2293. }
  2294. };
  2295. xhr_aff.send(null);
  2296. }
  2297. if(!CodeMirrorSpellChecker.dic_loading) {
  2298. CodeMirrorSpellChecker.dic_loading = true;
  2299. var xhr_dic = new XMLHttpRequest();
  2300. xhr_dic.open("GET", "https://cdn.jsdelivr.net/codemirror.spell-checker/latest/en_US.dic", true);
  2301. xhr_dic.onload = function() {
  2302. if(xhr_dic.readyState === 4 && xhr_dic.status === 200) {
  2303. CodeMirrorSpellChecker.dic_data = xhr_dic.responseText;
  2304. CodeMirrorSpellChecker.num_loaded++;
  2305. if(CodeMirrorSpellChecker.num_loaded == 2) {
  2306. CodeMirrorSpellChecker.typo = new Typo("en_US", CodeMirrorSpellChecker.aff_data, CodeMirrorSpellChecker.dic_data, {
  2307. platform: "any"
  2308. });
  2309. }
  2310. }
  2311. };
  2312. xhr_dic.send(null);
  2313. }
  2314. // Define what separates a word
  2315. var rx_word = "!\"#$%&()*+,-./:;<=>?@[\\]^_`{|}~ ";
  2316. // Create the overlay and such
  2317. var overlay = {
  2318. token: function(stream) {
  2319. var ch = stream.peek();
  2320. var word = "";
  2321. if(rx_word.includes(ch)) {
  2322. stream.next();
  2323. return null;
  2324. }
  2325. while((ch = stream.peek()) != null && !rx_word.includes(ch)) {
  2326. word += ch;
  2327. stream.next();
  2328. }
  2329. if(CodeMirrorSpellChecker.typo && !CodeMirrorSpellChecker.typo.check(word))
  2330. return "spell-error"; // CSS class: cm-spell-error
  2331. return null;
  2332. }
  2333. };
  2334. var mode = options.codeMirrorInstance.getMode(
  2335. config, config.backdrop || "text/plain"
  2336. );
  2337. return options.codeMirrorInstance.overlayMode(mode, overlay, true);
  2338. });
  2339. }
  2340. // Initialize data globally to reduce memory consumption
  2341. CodeMirrorSpellChecker.num_loaded = 0;
  2342. CodeMirrorSpellChecker.aff_loading = false;
  2343. CodeMirrorSpellChecker.dic_loading = false;
  2344. CodeMirrorSpellChecker.aff_data = "";
  2345. CodeMirrorSpellChecker.dic_data = "";
  2346. CodeMirrorSpellChecker.typo;
  2347. // Export
  2348. module.exports = CodeMirrorSpellChecker;
  2349. },{"typo-js":6}]},{},[7])(7)
  2350. });