Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rob Pike's regex algorithm
- Copyright (C) 2015 Victor C. Martins "dxhj"
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #if defined _sregex_included
- #endinput
- #endif
- #define _sregex_included
- stock sregex_match(regexp[], text[])
- {
- new i = 0;
- if (regexp[0] == '^')
- return _matchhere(regexp, 1, text, 0);
- do
- if (_matchhere(regexp, 0, text[i], i))
- return 1;
- while (text[i++]);
- return 0;
- }
- stock _matchhere(regexp[], iregex, text[], itext)
- {
- if (!regexp[iregex]) return 1;
- if (regexp[iregex+1] == '*')
- return _matchstar(regexp[iregex], regexp, iregex+2, text, itext);
- if (regexp[iregex] == '$' && !regexp[iregex+1])
- return text[itext] == '\0';
- if (text[itext] && (regexp[iregex] == '.' || regexp[iregex] == text[itext]))
- return _matchhere(regexp, ++iregex, text, ++itext);
- return 0;
- }
- stock _matchstar(chr, regexp[], iregex, text[], itext)
- {
- do {
- if (_matchhere(regexp, iregex, text, itext)) return 1;
- } while (text[itext] && (text[itext++] == chr || chr == '.'));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment