PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Engine/RegExp.as

http://xerte.googlecode.com/
ActionScript | 815 lines | 779 code | 2 blank | 34 comment | 76 complexity | 9f64ba3d74f71ccb0e26ada29c8dc422 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, GPL-3.0
  1. //---------------------------------------------------------------------------
  2. // RegExp object for Flash5 ActionScript Ver1.01
  3. // Author: Pavils Jurjans
  4. // Email : pavils@mailbox.riga.lv
  5. // Default source for this file can be found at:
  6. // http://www.jurjans.lv/flash/RegExp.html
  7. //---------------------------------------------------------------------------
  8. // This class is provided for flash community for free with a kind request
  9. // to keep the copyright lines in AS file untouched. However, debugging and
  10. // development of class takes much time limiting my opportunities to earn
  11. // some income on other projects. To overcome this, I have set up an account
  12. // with PayPal (http://www.paypal.com). Please, if you find my work valuable,
  13. // especially if you use it in commercial projects, make a donation
  14. // to pavils@mailbox.riga.lv of amount you feel is right. Please provide your
  15. // E-mail address upon payment submission so I can enlist you in my upgrade newslist.
  16. //---------------------------------------------------------------------------
  17. class RegExp {
  18. public var const:String = null;
  19. public var source:String = null;
  20. public var global:Boolean = false;
  21. public var ignoreCase:Boolean = false;
  22. public var multiline:Boolean = false;
  23. public var lastIndex:Number = null;
  24. public static var _xrStatic:Number = null;
  25. public var _xr:Number = null;
  26. public static var _xp:Number = null;
  27. public static var _xxa:Array = null;
  28. public static var _xxlp:Number = null;
  29. public var _xq:Number = null;
  30. public var _xqc:Number = null;
  31. public static var d:Number = null;
  32. public static var _xiStatic:Number = null;
  33. public var _xi:Number = 0;
  34. public static var _xxlm:String = null;
  35. public static var _xxlc:String = null;
  36. public static var _xxrc:String = null;
  37. public static var lastMatch:String = null;
  38. public static var leftContext:String = null;
  39. public static var rightContext:String = null;
  40. public static var _xa:Array = new Array();
  41. public static var lastParen:String = null;
  42. public static var _xaStatic:Array = new Array();
  43. public static var $1:String = null;
  44. public static var $2:String = null;
  45. public static var $3:String = null;
  46. public static var $4:String = null;
  47. public static var $5:String = null;
  48. public static var $6:String = null;
  49. public static var $7:String = null;
  50. public static var $8:String = null;
  51. public static var $9:String = null;
  52. private static var _setString:Boolean = RegExp.setStringMethods();
  53. function RegExp() {
  54. if (arguments[0] == null) {
  55. } else {
  56. const = "RegExp";
  57. compile.apply(this, arguments);
  58. }
  59. }
  60. public function invStr(sVal:String):String {
  61. var s = sVal;
  62. var l = length(s);
  63. var j;
  64. var c;
  65. var r = "";
  66. for (var i = 1; i<255; i++) {
  67. c = chr(i);
  68. j = 0;
  69. while (j<=l && substring(s, 1+j++, 1) != c) {
  70. }
  71. if (j>l) {
  72. r += c;
  73. }
  74. }
  75. return s;
  76. }
  77. public function compile() {
  78. this.source = arguments[0];
  79. if (arguments.length>1) {
  80. var flags = (arguments[1]+'').toLowerCase();
  81. for (var i = 0; i<length(flags); i++) {
  82. if (substring(flags, i+1, 1) == "g") {
  83. this.global = true;
  84. }
  85. if (substring(flags, i+1, 1) == "i") {
  86. this.ignoreCase = true;
  87. }
  88. if (substring(flags, i+1, 1) == "m") {
  89. this.multiline = true;
  90. }
  91. }
  92. }
  93. if (arguments.length < 3) {
  94. var root = true;
  95. RegExp._xrStatic = 1;
  96. //Paren counter
  97. var i = 0;
  98. } else {
  99. var root = false;
  100. this._xr = RegExp._xrStatic++;
  101. var i = arguments[2];
  102. }
  103. this.lastIndex = 0;
  104. /*
  105. Compile the regular expression
  106. The array of character definition objects will be created:
  107. q[n].t --> type of match required: 0 = exact
  108. 1 = in char set
  109. 2 = not in char set
  110. 3 = paren
  111. 4 = ref to paren
  112. 7 = new "OR" section
  113. 9 = beginning of line
  114. 10 = end of line
  115. q[n].s --> character or character set
  116. q[n].a --> character has to repeat at least a times
  117. q[n].b --> character has to repeat at most b times
  118. */
  119. var re = this.source;
  120. var ex;
  121. var l = length(re);
  122. var q = [];
  123. var qc = 0;
  124. var s;
  125. var range = false;
  126. var ca;
  127. var cb;
  128. var atEnd = false;
  129. var char;
  130. for (i=i; i<l; ++i) {
  131. var thischar = substring(re, i+1, 1);
  132. if (thischar == "\\") {
  133. i++;
  134. char = false;
  135. thischar = substring(re, i+1, 1);
  136. } else {
  137. char = true;
  138. }
  139. var nextchar = substring(re, i+2, 1);
  140. q[qc] = new Object();
  141. q[qc].t = 0;
  142. q[qc].a = 0;
  143. q[qc].b = 999;
  144. q[qc].c = -10;
  145. if (char) {
  146. // Handle special characters
  147. if (thischar == "(") {
  148. //Opening paren
  149. ex = new RegExp(re, (this.ignoreCase ? "gi" : "g"), i+1);
  150. i = RegExp._xiStatic;
  151. q[qc].t = 3;
  152. thischar = ex;
  153. nextchar = substring(re, i+2, 1);
  154. } else if (!root && thischar == ")") {
  155. //Closing paren
  156. break;
  157. } else if (thischar == "^") {
  158. //Must be located at the beginning of string/line
  159. if (qc == 0 || q[qc-1].t == 7) {
  160. q[qc].t = 9;
  161. q[qc].a = 1;
  162. q[qc].b = 1;
  163. qc++;
  164. }
  165. continue;
  166. } else if (thischar == "$") {
  167. //Must be located at the end of string/line
  168. if (root) {
  169. atEnd = true;
  170. }
  171. continue;
  172. } else if (thischar == "[") {
  173. //This is a character set
  174. i++;
  175. if (nextchar == "^") {
  176. q[qc].t = 2;
  177. i++;
  178. } else {
  179. q[qc].t = 1;
  180. }
  181. thischar = "";
  182. range = false;
  183. while (i<l && (s=substring(re, 1+i++, 1)) != "]") {
  184. if (range) {
  185. //Previous char was "-", so create a range
  186. if (s == "\\") {
  187. }
  188. cb = s == "\\" ? (s == "b" ? chr(8) : substring(re, 1+i++, 1)) : s;
  189. ca = ord(substring(thischar, length(thischar), 1))+1;
  190. while (cb>=(s=chr(ca++))) {
  191. thischar += s;
  192. }
  193. range = false;
  194. } else {
  195. if (s == "-" && length(thischar)>0) {
  196. //Character range is being defined
  197. range = true;
  198. } else {
  199. if (s == "\\") {
  200. //Predefined char set may follow
  201. s = substring(re, 1+i++, 1);
  202. if (s == "d") {
  203. thischar += "0123456789";
  204. } else if (s == "D") {
  205. thischar += invStr("0123456789");
  206. } else if (s == "s") {
  207. thischar += " \f\n\r\t\v";
  208. } else if (s == "S") {
  209. thischar += invStr(" \f\n\r\t\v");
  210. } else if (s == "w") {
  211. thischar += "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  212. } else if (s == "W") {
  213. thischar += invStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_");
  214. } else if (s == "b") {
  215. thischar += chr(8);
  216. } else if (s == "\\") {
  217. thischar += s;
  218. }
  219. } else {
  220. thischar += s;
  221. }
  222. }
  223. }
  224. }
  225. if (range) thischar += "-";
  226. i--;
  227. var nextchar = substring(re, i+2, 1);
  228. } else if (thischar == "|") {
  229. //OR section
  230. if (atEnd) {
  231. q[qc].t = 10;
  232. q[qc].a = 1;
  233. q[qc].b = 1;
  234. qc++;
  235. q[qc] = new Object();
  236. atEnd = false;
  237. }
  238. q[qc].t = 7;
  239. q[qc].a = 1;
  240. q[qc].b = 1;
  241. qc++;
  242. continue;
  243. } else if (thischar == ".") {
  244. q[qc].t = 2;
  245. thischar = "\n";
  246. } else if (thischar == "*" || thischar == "?" || thischar == "+") {
  247. continue;
  248. }
  249. } else {
  250. if (thischar>="1" && thischar<="9") {
  251. q[qc].t = 4;
  252. } else if (thischar == "b") {
  253. q[qc].t = 1;
  254. thischar = "--wb--";
  255. } else if (thischar == "B") {
  256. q[qc].t = 2;
  257. thischar = "--wb--";
  258. } else if (thischar == "d") {
  259. q[qc].t = 1;
  260. thischar = "0123456789";
  261. } else if (thischar == "D") {
  262. q[qc].t = 2;
  263. thischar = "0123456789";
  264. } else if (thischar == "s") {
  265. q[qc].t = 1;
  266. thischar = " \f\n\r\t\v";
  267. } else if (thischar == "S") {
  268. q[qc].t = 2;
  269. thischar = " \f\n\r\t\v";
  270. } else if (thischar == "w") {
  271. q[qc].t = 1;
  272. thischar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  273. } else if (thischar == "W") {
  274. q[qc].t = 2;
  275. thischar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
  276. }
  277. }
  278. //Counting metacharacters
  279. if (nextchar == "*") {
  280. q[qc].s = thischar;
  281. qc++;
  282. i++;
  283. } else if (nextchar == "?") {
  284. q[qc].s = thischar;
  285. q[qc].b = 1;
  286. qc++;
  287. i++;
  288. } else if (nextchar == "+") {
  289. q[qc].s = thischar;
  290. q[qc].a = 1;
  291. qc++;
  292. i++;
  293. } else if (nextchar == "{") {
  294. var comma = false;
  295. var rangeA = 0;
  296. range = "";
  297. i++;
  298. while (i+1<l && (s=substring(re, 2+i++, 1)) != "}") {
  299. if (!comma && s == ",") {
  300. comma = true;
  301. rangeA = Number(range);
  302. rangeA = Math.floor(isNaN(rangeA) ? 0 : rangeA);
  303. if (rangeA<0) {
  304. rangeA = 0;
  305. }
  306. range = "";
  307. } else {
  308. range += s;
  309. }
  310. }
  311. var rangeB = Number(range);
  312. rangeB = Math.floor(isNaN(rangeB) ? 0 : rangeB);
  313. if (rangeB<1) {
  314. rangeB = 999;
  315. }
  316. if (rangeB<rangeA) {
  317. rangeB = rangeA;
  318. }
  319. q[qc].s = thischar;
  320. q[qc].b = rangeB;
  321. q[qc].a = comma ? rangeA : rangeB;
  322. qc++;
  323. } else {
  324. q[qc].s = thischar;
  325. q[qc].a = 1;
  326. q[qc].b = 1;
  327. qc++;
  328. }
  329. }
  330. if (root && atEnd) {
  331. q[qc] = new Object();
  332. q[qc].t = 10;
  333. q[qc].a = 1;
  334. q[qc].b = 1;
  335. qc++;
  336. }
  337. if (!root) {
  338. RegExp._xiStatic = i;
  339. this.source = substring(re, arguments[2]+1, i-arguments[2]);
  340. }
  341. if (RegExp.d) {
  342. for (var i = 0; i<qc; i++) {
  343. trace("xr"+this._xr+' '+q[i].t+" : "+q[i].a+" : "+q[i].b+" : "+q[i].s);
  344. }
  345. }
  346. this._xq = q;
  347. this._xqc = qc;
  348. RegExp._xp = 0;
  349. }
  350. public function test() {
  351. if (RegExp._xp++ == 0) {
  352. RegExp._xxa = [];
  353. //Temp array for storing paren matches
  354. RegExp._xxlp = 0;
  355. //Last paren
  356. }
  357. // q[n].c --> count of matches
  358. // q[n].i --> index within the string
  359. var str = arguments[0]+'';
  360. var re;
  361. var q = this._xq;
  362. var qc = this._xqc;
  363. var qb;
  364. var c;
  365. var cl;
  366. var ct;
  367. var s;
  368. var l = length(str);
  369. var ix = this.global ? this.lastIndex : 0;
  370. var ix_ = ix;
  371. var str_ = str;
  372. if (this.ignoreCase) {
  373. str = str.toLowerCase();
  374. }
  375. var r = new Object();
  376. r.i = -1;
  377. var i = -1;
  378. while (i<qc-1) {
  379. i++;
  380. if (RegExp.d) {
  381. trace("New section started at i="+i);
  382. }
  383. ix = ix_;
  384. qb = i;
  385. q[qb].c = -10;
  386. var atEnd = false;
  387. while (i>qb || ix<l+1) {
  388. if (q[i].t == 7) {
  389. //New "OR" section coming
  390. break;
  391. } else if (q[i].t == 9) {
  392. i++;
  393. if (i == qb+1) {
  394. var atStart = true;
  395. qb = i;
  396. }
  397. q[qb].c = -10;
  398. continue;
  399. } else {
  400. if (r.i>=0 && ix>=r.i) {
  401. //There is already better match, so quit searching
  402. break;
  403. }
  404. if (q[i].c == -10) {
  405. if (RegExp.d) {
  406. trace("Lookup #"+i+" at index "+ix+" for \\\\\\\\\\\\\\\\'"+q[i].s+"\\\\\\\\\\\\\\\\' type "+q[i].t);
  407. }
  408. //Count the # of matches
  409. var m = 0;
  410. q[i].i = ix;
  411. if (q[i].t == 0) {
  412. //Exact match
  413. c = this.ignoreCase ? q[i].s.toLowerCase() : q[i].s;
  414. while (m<q[i].b && ix<l) {
  415. if (substring(str, 1+ix, 1) == c) {
  416. m++;
  417. ix++;
  418. } else {
  419. break;
  420. }
  421. }
  422. } else if (q[i].t == 1) {
  423. //In char set
  424. if (q[i].s == "--wb--") {
  425. q[i].a = 1;
  426. if (ix>0 && ix<l) {
  427. ct = substring(str, ix, 1);
  428. if (ct == " " || ct == "\\\\\\\\\\\\\\\\n") {
  429. m = 1;
  430. }
  431. if (m == 0) {
  432. ct = substring(str, 1+ix, 1);
  433. if (ct == " " || ct == "\\\\\\\\\\\\\\\\n") {
  434. m = 1;
  435. }
  436. }
  437. } else {
  438. m = 1;
  439. }
  440. } else {
  441. c = this.ignoreCase ? q[i].s.toLowerCase() : q[i].s;
  442. cl = length(c);
  443. var cs;
  444. while (m<q[i].b && ix<l) {
  445. ct = substring(str, 1+ix, 1);
  446. cs = 0;
  447. while (cs<=cl && substring(c, 1+cs++, 1) != ct) {
  448. }
  449. if (cs<=cl) {
  450. m++;
  451. ix++;
  452. } else {
  453. break;
  454. }
  455. }
  456. }
  457. } else if (q[i].t == 2) {
  458. //Not in char set
  459. c = this.ignoreCase ? q[i].s.toLowerCase() : q[i].s;
  460. cl = length(c);
  461. if (q[i].s == "--wb--") {
  462. q[i].a = 1;
  463. if (ix>0 && ix<l) {
  464. ct = substring(str, ix, 1);
  465. s = substring(str, 1+ix, 1);
  466. if (ct != " " && ct != "\\\\\\\\\\\\\\\\n" && s != " " && s != "\\\\\\\\\\\\\\\\n") {
  467. m = 1;
  468. }
  469. } else {
  470. m = 0;
  471. }
  472. } else {
  473. while (m<q[i].b && ix<l) {
  474. ct = substring(str, 1+ix, 1);
  475. cs = 0;
  476. while (cs<=cl && substring(c, 1+cs++, 1) != ct) {
  477. }
  478. if (cs<=cl) {
  479. break;
  480. } else {
  481. m++;
  482. ix++;
  483. }
  484. }
  485. }
  486. } else if (q[i].t == 10) {
  487. //End of string/line must be next
  488. s = substring(str, 1+ix, 1);
  489. m = (this.multiline && (s == "\\\\\\\\\\\\\\\\n" || s == "\\\\\\\\\\\\\\\\r")) || ix == l ? 1 : 0;
  490. } else if (q[i].t == 3) {
  491. //Regular expression in parens
  492. re = q[i].s;
  493. q[i].ix = [];
  494. q[i].ix[m] = ix;
  495. //Save index if need to retreat
  496. re.lastIndex = ix;
  497. while (m<q[i].b && re.test(str_)) {
  498. cl = length(RegExp._xxlm);
  499. if (cl>0) {
  500. ix += cl;
  501. m++;
  502. q[i].ix[m] = ix;
  503. } else {
  504. m = q[i].a;
  505. q[i].ix[m-1] = ix;
  506. break;
  507. }
  508. }
  509. if (m == 0) {
  510. RegExp._xxlm = "";
  511. }
  512. if (re._xr>RegExp._xxlp) {
  513. RegExp._xxlp = re._xr;
  514. }
  515. RegExp._xxa[Number(re._xr)] = RegExp._xxlm;
  516. } else if (q[i].t == 4) {
  517. //Back reference to paren
  518. if (RegExp._xp>=(c=Number(q[i].s))) {
  519. c = RegExp._xxa[c];
  520. c = this.ignoreCase ? c.toLowerCase() : c;
  521. cl = length(c);
  522. q[i].ix = [];
  523. q[i].ix[m] = ix;
  524. if (cl>0) {
  525. while (m<q[i].b && ix<l) {
  526. if (substring(str, 1+ix, cl) == c) {
  527. m++;
  528. ix += cl;
  529. q[i].ix[m] = ix;
  530. } else {
  531. break;
  532. }
  533. }
  534. } else {
  535. m = 0;
  536. q[i].a = 0;
  537. }
  538. } else {
  539. //Paren is not ready, treat number as charcode
  540. c = chr(c);
  541. q[i].ix = [];
  542. q[i].ix[m] = ix;
  543. while (m<q[i].b && ix<l) {
  544. if (substring(str, 1+ix, 1) == c) {
  545. m++;
  546. ix++;
  547. q[i].ix[m] = ix;
  548. } else {
  549. break;
  550. }
  551. }
  552. }
  553. }
  554. q[i].c = m;
  555. if (RegExp.d) {
  556. trace(" "+m+" matches found");
  557. }
  558. }
  559. if (q[i].c<q[i].a) {
  560. if (RegExp.d) {
  561. trace(" not enough matches");
  562. }
  563. //Not enough matches
  564. if (i>qb) {
  565. //Retreat back and decrease # of assumed matches
  566. i--;
  567. q[i].c--;
  568. if (q[i].c>=0) {
  569. ix = (q[i].t == 3 || q[i].t == 4) ? q[i].ix[q[i].c] : (q[i].i+q[i].c);
  570. }
  571. if (RegExp.d) {
  572. trace("Retreat to #"+i+" c="+q[i].c+" index="+ix);
  573. }
  574. } else {
  575. if (RegExp._xp>1) {
  576. //If this is a paren, failing to find first match is fatal
  577. break;
  578. }
  579. if (atStart) {
  580. //Match must be at the start of string/line
  581. if (this.multiline) {
  582. //Jump to the beginning of the next line
  583. while (ix<=l) {
  584. s = substring(str, 1+ix++, 1);
  585. if (s == "\\\\\\\\\\\\\\\\n" || s == "\\\\\\\\\\\\\\\\r") {
  586. break;
  587. }
  588. }
  589. q[i].c = -10;
  590. } else {
  591. //No match
  592. break;
  593. }
  594. } else {
  595. //Start a new search from next position
  596. ix++;
  597. q[i].c = -10;
  598. }
  599. }
  600. } else {
  601. if (RegExp.d) {
  602. trace(" enough matches!");
  603. }
  604. //# of matches ok, proceed to next
  605. i++;
  606. if (i == qc || q[i].t == 7) {
  607. if (RegExp.d) {
  608. trace("Saving better result: r.i = q["+qb+"].i = "+q[qb].i);
  609. }
  610. r.i = q[qb].i;
  611. r.li = ix;
  612. break;
  613. } else {
  614. q[i].c = -10;
  615. }
  616. }
  617. }
  618. }
  619. while (i<qc && q[i].t != 7) {
  620. i++;
  621. }
  622. //Jump to the next "OR" section
  623. }
  624. if (r.i<0) {
  625. this.lastIndex = 0;
  626. if (RegExp._xp-- == 1) {
  627. RegExp._xxa = [];
  628. RegExp._xxlp = 0;
  629. }
  630. return false;
  631. } else {
  632. ix = r.li;
  633. this._xi = r.i;
  634. RegExp._xxlm = substring(str_, r.i+1, ix-r.i);
  635. RegExp._xxlc = substring(str_, 1, r.i);
  636. RegExp._xxrc = substring(str_, ix+1, l-ix);
  637. if (ix == r.i) {
  638. ix++;
  639. }
  640. this.lastIndex = ix;
  641. if (RegExp._xp-- == 1) {
  642. RegExp.lastMatch = RegExp._xxlm;
  643. RegExp.leftContext = RegExp._xxlc;
  644. RegExp.rightContext = RegExp._xxrc;
  645. RegExp._xaStatic = RegExp._xxa;
  646. RegExp.lastParen = RegExp._xxa[Number(RegExp._xxlp)];
  647. for (i=1; i<10; i++) {
  648. RegExp["$"+i] = RegExp._xaStatic[Number(i)];
  649. }
  650. }
  651. return true;
  652. }
  653. }
  654. public function exec() {
  655. var str = arguments[0]+'';
  656. if (str == '') {
  657. return false;
  658. }
  659. var t = this.test(str);
  660. if (t) {
  661. var ra = new Array();
  662. ra.index = this._xi;
  663. ra.input = str;
  664. ra[0] = RegExp.lastMatch;
  665. var l = RegExp._xaStatic.length;
  666. for (var i = 1; i<l; i++) {
  667. ra[i] = RegExp._xaStatic[Number(i)];
  668. }
  669. } else {
  670. var ra = null;
  671. }
  672. return ra;
  673. }
  674. public static function setStringMethods() {
  675. if(String.prototype.match != undefined) {
  676. return;
  677. }
  678. String.prototype.match = function() {
  679. if (typeof (arguments[0]) != "object") {
  680. return null;
  681. }
  682. if (arguments[0].const != "RegExp") {
  683. return null;
  684. }
  685. var re = arguments[0];
  686. var s = this.valueOf();
  687. var ip = 0;
  688. var rc = 0;
  689. if (re.global) {
  690. re.lastIndex = 0;
  691. while (re.test(s)) {
  692. if (rc == 0) {
  693. var ra = new Array();
  694. }
  695. ra[rc++] = RegExp.lastMatch;
  696. ip = re.lastIndex;
  697. }
  698. re.lastIndex = ip;
  699. } else {
  700. var ra = re.exec(s);
  701. rc++;
  702. }
  703. return (rc == 0) ? null : ra;
  704. };
  705. String.prototype.replace = function() {
  706. if (typeof (arguments[0]) != "object") {
  707. return null;
  708. }
  709. if (arguments[0].const != "RegExp") {
  710. return null;
  711. }
  712. var re = arguments[0];
  713. var rs = arguments[1]+'';
  714. var s = this;
  715. var r = "";
  716. re.lastIndex = 0;
  717. if (re.global) {
  718. var ip = 0;
  719. var ix = 0;
  720. while (re.test(s)) {
  721. //Replace backreferences in rs
  722. var i = 0;
  723. var l = length(rs);
  724. var c = "";
  725. var pc = "";
  726. var nrs = "";
  727. while (i<l) {
  728. c = substring(rs, 1+i++, 1);
  729. if (c == "$" && pc != "\\") {
  730. c = substring(rs, 1+i++, 1);
  731. if (isNaN(Number(c)) || Number(c)>9) {
  732. nrs += "$"+c;
  733. } else {
  734. nrs += RegExp._xaStatic[Number(c)];
  735. }
  736. } else {
  737. nrs += c;
  738. }
  739. pc = c;
  740. }
  741. r += substring(s, ix+1, re._xi-ix)+nrs;
  742. ix = re._xi+length(RegExp.lastMatch);
  743. ip = re.lastIndex;
  744. }
  745. re.lastIndex = ip;
  746. } else {
  747. if (re.test(s)) {
  748. r += RegExp.leftContext+rs;
  749. }
  750. }
  751. r += re.lastIndex == 0 ? s : RegExp.rightContext;
  752. return r;
  753. };
  754. String.prototype.search = function() {
  755. if (typeof (arguments[0]) != "object") {
  756. return null;
  757. }
  758. if (arguments[0].const != "RegExp") {
  759. return null;
  760. }
  761. var re = arguments[0];
  762. var s = this;
  763. re.lastIndex = 0;
  764. var t = re.test(s);
  765. return t ? re._xi : -1;
  766. };
  767. String.prototype.old_split = String.prototype.split;
  768. String.prototype.split = function() {
  769. if (typeof (arguments[0]) == "object" && arguments[0].const == "RegExp") {
  770. var re = arguments[0];
  771. var lm = arguments[1] == null ? 9999 : Number(arguments[1]);
  772. if (isNaN(lm)) {
  773. lm = 9999;
  774. }
  775. var s = this;
  776. var ra = new Array();
  777. var rc = 0;
  778. var gs = re.global;
  779. re.global = true;
  780. re.lastIndex = 0;
  781. var ip = 0;
  782. var ipp = 0;
  783. var ix = 0;
  784. while (rc<lm && re.test(s)) {
  785. //trace(re._xi + " " + ix + " " + RegExp.lastMatch);
  786. if (re._xi != ix) {
  787. ra[rc++] = substring(s, ix+1, re._xi-ix);
  788. }
  789. ix = re._xi+length(RegExp.lastMatch);
  790. ipp = ip;
  791. ip = re.lastIndex;
  792. }
  793. if (rc == lm) {
  794. re.lastIndex = ipp;
  795. } else {
  796. re.lastIndex = ip;
  797. }
  798. if (rc == 0) {
  799. ra[rc] = s;
  800. } else {
  801. if (rc<lm && length(RegExp.rightContext)>0) {
  802. ra[rc++] = RegExp.rightContext;
  803. }
  804. }
  805. re.global = gs;
  806. return ra;
  807. } else {
  808. return this.old_split(arguments[0], arguments[1]);
  809. }
  810. };
  811. return true;
  812. }
  813. }