UNPKG

zitcher

Version:

字符串匹配算法相关js实现

1 lines 2.98 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.zitcher=e():t.zitcher=e()}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.r=function(t){Object.defineProperty(t,"__esModule",{value:!0})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=2)}([function(t,e){function r(t){var e=new n(0);this.lastState=1;for(var r=0;r<t.length;r++){for(var o=t[r],i=o.split(""),s=e,h=0;h<i.length;h++){var f=i[h];s=s.next(f,this.lastState++)}s.finish=!0,s.mode=o}var a=new Array(this.lastState),u=[],c=e.nextChars();for(r=0;r<c.length;r++){var l=e.next(c[r]);a[l.state]=e,u.push(l)}for(;0!=u.length;){var p=u.shift();for(c=p.nextChars(),h=0;h<c.length;h++){var d=p.next(c[h]);for(s=a[p.state];s!=e&&s.next(c[h]);)s=a[s.state];s.next[c[h]]?a[d.state]=s.next[c[h]]:a[d.state]=e,u.push(d)}}this.root=e,this.failure=a}function n(t){this.state=t,this.finish=!1}r.prototype.findFirst=function(t){for(var e=this.root,r=t.split(""),n=0;n<r.length;n++){var o=e.next(r[n]);if(null!=o)e=o;else{if(0==n||e==this.root)continue;--n,e=this.failure[e.state]}if(e.finish)return{mode:e.mode,start:n-e.mode.length+1}}},r.prototype.findAll=function(t){for(var e=[],r=this.root,n=t.split(""),o=0;o<n.length;o++){var i=r.next(n[o]);if(null!=i)r=i;else{if(0==o||r==this.root)continue;--o,r=this.failure[r.state]}if(r.finish){if(e.push({mode:r.mode,start:o-r.mode.length+1}),0==o||r==this.root)continue;o=o-r.mode.length+1,r=this.failure[r.state]}}return e},n.prototype.next=function(t,e){return this.nodes||(this.nodes={}),e&&!this.nodes[t]&&(this.nodes[t]=new n(e)),this.nodes[t]},n.prototype.nextChars=function(){var t=[];for(var e in this.nodes)this.nodes.hasOwnProperty(e)&&t.push(e);return t},t.exports=r},function(t,e){function r(t){var e=t.length,r=new Array(e);r[0]=-1;for(var n=-1,o=1;o<e;o++){for(;n>-1&&t.charAt(n+1)!=t.charAt(o);)n=r[n];t.charAt(n+1)==t.charAt(o)&&n++,r[o]=n}this.next=r,this.keyword=t}r.prototype.findFirst=function(t){for(var e=-1,r=t.length,n=this.keyword.length,o=0;o<r;o++){for(;e>-1&&this.keyword.charAt(e+1)!=t.charAt(o);)e=this.next[e];if(this.keyword.charAt(e+1)==t.charAt(o)&&e++,e==n-1)return o-n+1}return-1},r.prototype.findAll=function(t){for(var e=[],r=-1,n=t.length,o=this.keyword.length,i=1;i<n;i++){for(;r>-1&&this.keyword.charAt(r+1)!=t.charAt(i);)r=this.next[r];this.keyword.charAt(r+1)==t.charAt(i)&&r++,r==o-1&&(e.push(i-o+1),r=-1)}return e},t.exports=r},function(t,e,r){var n=r(1),o=r(0);t.exports=function(t){return"string"==typeof t?new n(t):new o(t)}}])});