Guest User

sregex

a guest
Dec 8th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 1.64 KB | None | 0 0
  1. /*
  2.     Rob Pike's regex algorithm
  3.     Copyright (C) 2015  Victor C. Martins "dxhj"
  4.  
  5.     This program is free software: you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation, either version 3 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #if defined _sregex_included
  19.     #endinput
  20. #endif
  21. #define _sregex_included
  22.  
  23. stock sregex_match(regexp[], text[])
  24. {  
  25.     new i = 0;
  26.     if (regexp[0] == '^')
  27.         return _matchhere(regexp, 1, text, 0);
  28.     do
  29.         if (_matchhere(regexp, 0, text[i], i))
  30.             return 1;
  31.     while (text[i++]);
  32.     return 0;
  33. }
  34.  
  35. stock _matchhere(regexp[], iregex, text[], itext)
  36. {
  37.     if (!regexp[iregex]) return 1;
  38.     if (regexp[iregex+1] == '*')
  39.         return _matchstar(regexp[iregex], regexp, iregex+2, text, itext);
  40.     if (regexp[iregex] == '$' && !regexp[iregex+1])
  41.         return text[itext] == '\0';
  42.     if (text[itext] && (regexp[iregex] == '.' || regexp[iregex] == text[itext]))
  43.         return _matchhere(regexp, ++iregex, text, ++itext);
  44.     return 0;
  45. }
  46.  
  47. stock _matchstar(chr, regexp[], iregex, text[], itext)
  48. {
  49.     do {
  50.         if (_matchhere(regexp, iregex, text, itext)) return 1;
  51.     } while (text[itext] && (text[itext++] == chr || chr == '.'));
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment