12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502 |
- /*************************************************************************/
- /* regex.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "regex.h"
- #include <wctype.h>
- #include <wchar.h>
- static int RegEx_hex2int(const CharType c)
- {
- if ('0' <= c && c <= '9')
- return int(c - '0');
- else if ('a' <= c && c <= 'f')
- return int(c - 'a') + 10;
- else if ('A' <= c && c <= 'F')
- return int(c - 'A') + 10;
- return -1;
- }
- struct RegExSearch {
- Ref<RegExMatch> match;
- const CharType* str;
- int end;
- int eof;
- // For standard quantifier behaviour, test_parent is used to check the
- // rest of the pattern. If the pattern matches, to prevent the parent
- // from testing again, the complete flag is used as a shortcut out.
- bool complete;
- // With lookahead, the position needs to rewind to its starting position
- // when test_parent is used. Due to functional programming, this state
- // has to be kept as a parameter.
- Vector<int> lookahead_pos;
- CharType at(int p_pos) {
- return str[p_pos];
- }
- RegExSearch(Ref<RegExMatch>& p_match, int p_end, int p_lookahead) : match(p_match) {
- str = p_match->string.c_str();
- end = p_end;
- eof = p_match->string.length();
- complete = false;
- lookahead_pos.resize(p_lookahead);
- }
- };
- struct RegExNode {
- RegExNode* next;
- RegExNode* previous;
- RegExNode* parent;
- bool quantifiable;
- int length;
- RegExNode() {
- next = NULL;
- previous = NULL;
- parent = NULL;
- quantifiable = false;
- length = -1;
- }
- virtual ~RegExNode() {
- if (next)
- memdelete(next);
- }
- virtual int test(RegExSearch& s, int pos) const {
- return next ? next->test(s, pos) : -1;
- }
- virtual int test_parent(RegExSearch& s, int pos) const {
- if (next)
- pos = next->test(s, pos);
- if (pos >= 0) {
- s.complete = true;
- if (parent)
- pos = parent->test_parent(s, pos);
- }
- if (pos < 0)
- s.complete = false;
- return pos;
- }
- void increment_length(int amount, bool subtract = false) {
- if (amount >= 0 && length >= 0) {
- if (!subtract)
- length += amount;
- else
- length -= amount;
- } else {
- length = -1;
- }
- if (parent)
- parent->increment_length(amount, subtract);
- }
- };
- struct RegExNodeChar : public RegExNode {
- CharType ch;
- RegExNodeChar(CharType p_char) {
- length = 1;
- quantifiable = true;
- ch = p_char;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (s.end <= pos || 0 > pos || s.at(pos) != ch)
- return -1;
- return next ? next->test(s, pos + 1) : pos + 1;
- }
- static CharType parse_escape(const CharType*& c) {
- int point = 0;
- switch (c[1]) {
- case 'x':
- for (int i = 2; i <= 3; ++i) {
- int res = RegEx_hex2int(c[i]);
- if (res == -1)
- return '\0';
- point = (point << 4) + res;
- }
- c = &c[3];
- return CharType(point);
- case 'u':
- for (int i = 2; i <= 5; ++i) {
- int res = RegEx_hex2int(c[i]);
- if (res == -1)
- return '\0';
- point = (point << 4) + res;
- }
- c = &c[5];
- return CharType(point);
- case '0': ++c; return '\0';
- case 'a': ++c; return '\a';
- case 'e': ++c; return '\e';
- case 'f': ++c; return '\f';
- case 'n': ++c; return '\n';
- case 'r': ++c; return '\r';
- case 't': ++c; return '\t';
- case 'v': ++c; return '\v';
- case 'b': ++c; return '\b';
- default: break;
- }
- return (++c)[0];
- }
- };
- struct RegExNodeRange : public RegExNode {
- CharType start;
- CharType end;
- RegExNodeRange(CharType p_start, CharType p_end) {
- length = 1;
- quantifiable = true;
- start = p_start;
- end = p_end;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (s.end <= pos || 0 > pos)
- return -1;
- CharType c = s.at(pos);
- if (c < start || end < c)
- return -1;
- return next ? next->test(s, pos + 1) : pos + 1;
- }
- };
- struct RegExNodeShorthand : public RegExNode {
- CharType repr;
- RegExNodeShorthand(CharType p_repr) {
- length = 1;
- quantifiable = true;
- repr = p_repr;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (s.end <= pos || 0 > pos)
- return -1;
- bool found = false;
- bool invert = false;
- CharType c = s.at(pos);
- switch (repr) {
- case '.':
- found = true;
- break;
- case 'W':
- invert = true;
- case 'w':
- found = (c == '_' || iswalnum(c) != 0);
- break;
- case 'D':
- invert = true;
- case 'd':
- found = ('0' <= c && c <= '9');
- break;
- case 'S':
- invert = true;
- case 's':
- found = (iswspace(c) != 0);
- break;
- default:
- break;
- }
- if (found == invert)
- return -1;
- return next ? next->test(s, pos + 1) : pos + 1;
- }
- };
- struct RegExNodeClass : public RegExNode {
- enum Type {
- Type_none,
- Type_alnum,
- Type_alpha,
- Type_ascii,
- Type_blank,
- Type_cntrl,
- Type_digit,
- Type_graph,
- Type_lower,
- Type_print,
- Type_punct,
- Type_space,
- Type_upper,
- Type_xdigit,
- Type_word
- };
- Type type;
- bool test_class(CharType c) const {
- static Vector<CharType> REGEX_NODE_SPACE = String(" \t\r\n\f");
- static Vector<CharType> REGEX_NODE_PUNCT = String("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
- switch (type) {
- case Type_alnum:
- if ('0' <= c && c <= '9') return true;
- if ('a' <= c && c <= 'z') return true;
- if ('A' <= c && c <= 'Z') return true;
- return false;
- case Type_alpha:
- if ('a' <= c && c <= 'z') return true;
- if ('A' <= c && c <= 'Z') return true;
- return false;
- case Type_ascii:
- return (0x00 <= c && c <= 0x7F);
- case Type_blank:
- return (c == ' ' || c == '\t');
- case Type_cntrl:
- return ((0x00 <= c && c <= 0x1F) || c == 0x7F);
- case Type_digit:
- return ('0' <= c && c <= '9');
- case Type_graph:
- return (0x20 < c && c < 0x7F);
- case Type_lower:
- return ('a' <= c && c <= 'z');
- case Type_print:
- return (0x1F < c && c < 0x1F);
- case Type_punct:
- return (REGEX_NODE_PUNCT.find(c) >= 0);
- case Type_space:
- return (REGEX_NODE_SPACE.find(c) >= 0);
- case Type_upper:
- return ('A' <= c && c <= 'Z');
- case Type_xdigit:
- if ('0' <= c && c <= '9') return true;
- if ('a' <= c && c <= 'f') return true;
- if ('A' <= c && c <= 'F') return true;
- return false;
- case Type_word:
- if ('0' <= c && c <= '9') return true;
- if ('a' <= c && c <= 'z') return true;
- if ('A' <= c && c <= 'Z') return true;
- return (c == '_');
- default:
- return false;
- }
- return false;
- }
- RegExNodeClass(Type p_type) {
- length = 1;
- quantifiable = true;
- type = p_type;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (s.end <= pos || 0 > pos)
- return -1;
- if (!test_class(s.at(pos)))
- return -1;
- return next ? next->test(s, pos + 1) : pos + 1;
- }
- #define REGEX_CMP_CLASS(POS, NAME) if (cmp_class(POS, #NAME)) return Type_ ## NAME
- static Type parse_type(const CharType*& p_pos) {
- REGEX_CMP_CLASS(p_pos, alnum);
- REGEX_CMP_CLASS(p_pos, alpha);
- REGEX_CMP_CLASS(p_pos, ascii);
- REGEX_CMP_CLASS(p_pos, blank);
- REGEX_CMP_CLASS(p_pos, cntrl);
- REGEX_CMP_CLASS(p_pos, digit);
- REGEX_CMP_CLASS(p_pos, graph);
- REGEX_CMP_CLASS(p_pos, lower);
- REGEX_CMP_CLASS(p_pos, print);
- REGEX_CMP_CLASS(p_pos, punct);
- REGEX_CMP_CLASS(p_pos, space);
- REGEX_CMP_CLASS(p_pos, upper);
- REGEX_CMP_CLASS(p_pos, xdigit);
- REGEX_CMP_CLASS(p_pos, word);
- return Type_none;
- }
- static bool cmp_class(const CharType*& p_pos, const char* p_text) {
- unsigned int i = 0;
- for (i = 0; p_text[i] != '\0'; ++i)
- if (p_pos[i] != p_text[i])
- return false;
- if (p_pos[i++] != ':' || p_pos[i] != ']')
- return false;
- p_pos = &p_pos[i];
- return true;
- }
- };
- struct RegExNodeAnchorStart : public RegExNode {
- RegExNodeAnchorStart() {
- length = 0;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (pos != 0)
- return -1;
- return next ? next->test(s, pos) : pos;
- }
- };
- struct RegExNodeAnchorEnd : public RegExNode {
- RegExNodeAnchorEnd() {
- length = 0;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (pos != s.eof)
- return -1;
- return next ? next->test(s, pos) : pos;
- }
- };
- struct RegExNodeWordBoundary : public RegExNode {
- bool inverse;
- RegExNodeWordBoundary(bool p_inverse) {
- length = 0;
- inverse = p_inverse;
- }
- virtual int test(RegExSearch& s, int pos) const {
- bool left = false;
- bool right = false;
- if (pos != 0) {
- CharType c = s.at(pos - 1);
- if (c == '_' || iswalnum(c))
- left = true;
- }
- if (pos != s.eof) {
- CharType c = s.at(pos);
- if (c == '_' || iswalnum(c))
- right = true;
- }
- if ((left == right) != inverse)
- return -1;
- return next ? next->test(s, pos) : pos;
- }
- };
- struct RegExNodeQuantifier : public RegExNode {
- int min;
- int max;
- bool greedy;
- RegExNode* child;
- RegExNodeQuantifier(int p_min, int p_max) {
- min = p_min;
- max = p_max;
- greedy = true;
- child = NULL;
- }
- ~RegExNodeQuantifier() {
- if (child)
- memdelete(child);
- }
- virtual int test(RegExSearch& s, int pos) const {
- return test_step(s, pos, 0, pos);
- }
- virtual int test_parent(RegExSearch& s, int pos) const {
- s.complete = false;
- return pos;
- }
- int test_step(RegExSearch& s, int pos, int level, int start) const {
- if (pos > s.end)
- return -1;
- if (!greedy && level > min) {
- int res = next ? next->test(s, pos) : pos;
- if (s.complete)
- return res;
- if (res >= 0 && parent->test_parent(s, res) >= 0)
- return res;
- }
- if (max >= 0 && level > max)
- return -1;
- int res = pos;
- if (level >= 1) {
- if (level > min + 1 && pos == start)
- return -1;
- res = child->test(s, pos);
- if (s.complete)
- return res;
- }
- if (res >= 0) {
- int res_step = test_step(s, res, level + 1, start);
- if (res_step >= 0)
- return res_step;
- if (greedy && level >= min) {
- if (next)
- res = next->test(s, res);
- if (s.complete)
- return res;
- if (res >= 0 && parent->test_parent(s, res) >= 0)
- return res;
- }
- }
- return -1;
- }
- };
- struct RegExNodeBackReference : public RegExNode {
- int id;
- RegExNodeBackReference(int p_id) {
- length = -1;
- quantifiable = true;
- id = p_id;
- }
- virtual int test(RegExSearch& s, int pos) const {
- RegExMatch::Group& ref = s.match->captures[id];
- for (int i = 0; i < ref.length; ++i) {
- if (pos + i >= s.end)
- return -1;
- if (s.at(ref.start + i) != s.at(pos + i))
- return -1;
- }
- return next ? next->test(s, pos + ref.length) : pos + ref.length;
- }
- };
- struct RegExNodeGroup : public RegExNode {
- bool inverse;
- bool reset_pos;
- Vector<RegExNode*> childset;
- RegExNode* back;
- RegExNodeGroup() {
- length = 0;
- quantifiable = true;
- inverse = false;
- reset_pos = false;
- back = NULL;
- }
- virtual ~RegExNodeGroup() {
- for (int i = 0; i < childset.size(); ++i)
- memdelete(childset[i]);
- }
- virtual int test(RegExSearch& s, int pos) const {
- for (int i = 0; i < childset.size(); ++i) {
- s.complete = false;
- int res = childset[i]->test(s, pos);
- if (s.complete)
- return res;
- if (inverse) {
- if (res < 0)
- res = pos + 1;
- else
- return -1;
- if (i + 1 < childset.size())
- continue;
- }
- if (res >= 0) {
- if (reset_pos)
- res = pos;
- return next ? next->test(s, res) : res;
- }
- }
- return -1;
- }
- void add_child(RegExNode* node) {
- node->parent = this;
- node->previous = back;
- if (back)
- back->next = node;
- else
- childset.push_back(node);
- increment_length(node->length);
- back = node;
- }
- void add_childset() {
- if (childset.size() > 0)
- length = -1;
- back = NULL;
- }
- RegExNode* swap_back(RegExNode* node) {
- RegExNode* old = back;
- if (old) {
- if (!old->previous)
- childset.remove(childset.size() - 1);
- back = old->previous;
- increment_length(old->length, true);
- }
- add_child(node);
- return old;
- }
- };
- struct RegExNodeCapturing : public RegExNodeGroup {
- int id;
- RegExNodeCapturing(int p_id = 0) {
- id = p_id;
- }
- virtual int test(RegExSearch& s, int pos) const {
- RegExMatch::Group& ref = s.match->captures[id];
- int old_start = ref.start;
- ref.start = pos;
- int res = RegExNodeGroup::test(s, pos);
- if (res >= 0) {
- if (!s.complete)
- ref.length = res - pos;
- } else {
- ref.start = old_start;
- }
- return res;
- }
- virtual int test_parent(RegExSearch& s, int pos) const {
- RegExMatch::Group& ref = s.match->captures[id];
- ref.length = pos - ref.start;
- return RegExNode::test_parent(s, pos);
- }
- static Variant parse_name(const CharType*& c, bool p_allow_numeric) {
- if (c[1] == '0') {
- return -1;
- } else if ('1' <= c[1] && c[1] <= '9') {
- if (!p_allow_numeric)
- return -1;
- int res = (++c)[0] - '0';
- while ('0' <= c[1] && c[1] <= '9')
- res = res * 10 + int((++c)[0] - '0');
- if ((++c)[0] != '>')
- return -1;
- return res;
- } else if (iswalnum(c[1])) {
- String res(++c, 1);
- while (iswalnum(c[1]))
- res += String(++c, 1);
- if ((++c)[0] != '>')
- return -1;
- return res;
- }
- return -1;
- }
- };
- struct RegExNodeLookAhead : public RegExNodeGroup {
- int id;
- RegExNodeLookAhead(bool p_inverse, int p_id = 0) {
- quantifiable = false;
- inverse = p_inverse;
- reset_pos = true;
- id = p_id;
- }
- virtual int test(RegExSearch& s, int pos) const {
- s.lookahead_pos[id] = pos;
- return RegExNodeGroup::test(s, pos);
- }
- virtual int test_parent(RegExSearch& s, int pos) const {
- return RegExNode::test_parent(s, s.lookahead_pos[id]);
- }
- };
- struct RegExNodeLookBehind : public RegExNodeGroup {
- RegExNodeLookBehind(bool p_inverse, int p_id = 0) {
- quantifiable = false;
- inverse = p_inverse;
- reset_pos = true;
- }
- virtual int test(RegExSearch& s, int pos) const {
- if (pos < length)
- return -1;
- return RegExNodeGroup::test(s, pos - length);
- }
- };
- struct RegExNodeBracket : public RegExNode {
- bool inverse;
- Vector<RegExNode*> children;
- RegExNodeBracket() {
- length = 1;
- quantifiable = true;
- inverse = false;
- }
- virtual ~RegExNodeBracket() {
- for (int i = 0; i < children.size(); ++i)
- memdelete(children[i]);
- }
- virtual int test(RegExSearch& s, int pos) const {
- for (int i = 0; i < children.size(); ++i) {
- int res = children[i]->test(s, pos);
- if (inverse) {
- if (res < 0)
- res = pos + 1;
- else
- return -1;
- if (i + 1 < children.size())
- continue;
- }
- if (res >= 0)
- return next ? next->test(s, res) : res;
- }
- return -1;
- }
- void add_child(RegExNode* node) {
- node->parent = this;
- children.push_back(node);
- }
- void pop_back() {
- memdelete(children[children.size() - 1]);
- children.remove(children.size() - 1);
- }
- };
- #define REGEX_EXPAND_FAIL(MSG)\
- {\
- ERR_PRINT(MSG);\
- return String();\
- }
- String RegExMatch::expand(const String& p_template) const {
- String res;
- for (const CharType* c = p_template.c_str(); *c != '\0'; ++c) {
- if (c[0] == '\\') {
- if (('1' <= c[1] && c[1] <= '9') || (c[1] == 'g' && c[2] == '{')) {
- int ref = 0;
- bool unclosed = false;
- if (c[1] == 'g') {
- unclosed = true;
- c = &c[2];
- }
- while ('0' <= c[1] && c[1] <= '9') {
- ref = ref * 10 + int(c[1] - '0');
- ++c;
- }
- if (unclosed) {
- if (c[1] != '}')
- REGEX_EXPAND_FAIL("unclosed backreference '{'");
- ++c;
- }
- res += get_string(ref);
- } else if (c[1] =='g' && c[2] == '<') {
- const CharType* d = &c[2];
- Variant name = RegExNodeCapturing::parse_name(d, true);
- if (name == Variant(-1))
- REGEX_EXPAND_FAIL("unrecognised character for group name");
- c = d;
- res += get_string(name);
- } else {
- const CharType* d = c;
- CharType ch = RegExNodeChar::parse_escape(d);
- if (c == d)
- REGEX_EXPAND_FAIL("invalid escape token");
- res += String(&ch, 1);
- c = d;
- }
- } else {
- res += String(c, 1);
- }
- }
- return res;
- }
- int RegExMatch::get_group_count() const {
- int count = 0;
- for (int i = 1; i < captures.size(); ++i)
- if (captures[i].name.get_type() == Variant::INT)
- ++count;
- return count;
- }
- Array RegExMatch::get_group_array() const {
- Array res;
- for (int i = 1; i < captures.size(); ++i) {
- const RegExMatch::Group& capture = captures[i];
- if (capture.name.get_type() != Variant::INT)
- continue;
- if (capture.start >= 0)
- res.push_back(string.substr(capture.start, capture.length));
- else
- res.push_back(String());
- }
- return res;
- }
- Array RegExMatch::get_names() const {
- Array res;
- for (int i = 1; i < captures.size(); ++i)
- if (captures[i].name.get_type() == Variant::STRING)
- res.push_back(captures[i].name);
- return res;
- }
- Dictionary RegExMatch::get_name_dict() const {
- Dictionary res;
- for (int i = 1; i < captures.size(); ++i) {
- const RegExMatch::Group& capture = captures[i];
- if (capture.name.get_type() != Variant::STRING)
- continue;
- if (capture.start >= 0)
- res[capture.name] = string.substr(capture.start, capture.length);
- else
- res[capture.name] = String();
- }
- return res;
- }
- String RegExMatch::get_string(const Variant& p_name) const {
- for (int i = 0; i < captures.size(); ++i) {
- const RegExMatch::Group& capture = captures[i];
- if (capture.name != p_name)
- continue;
- if (capture.start == -1)
- return String();
- return string.substr(capture.start, capture.length);
- }
- return String();
- }
- int RegExMatch::get_start(const Variant& p_name) const {
- for (int i = 0; i < captures.size(); ++i)
- if (captures[i].name == p_name)
- return captures[i].start;
- return -1;
- }
- int RegExMatch::get_end(const Variant& p_name) const {
- for (int i = 0; i < captures.size(); ++i)
- if (captures[i].name == p_name)
- return captures[i].start + captures[i].length;
- return -1;
- }
- RegExMatch::RegExMatch() {
- }
- static bool RegEx_is_shorthand(CharType ch) {
- switch (ch) {
- case 'w':
- case 'W':
- case 'd':
- case 'D':
- case 's':
- case 'S':
- return true;
- default:
- break;
- }
- return false;
- }
- #define REGEX_COMPILE_FAIL(MSG)\
- {\
- ERR_PRINT(MSG);\
- clear();\
- return FAILED;\
- }
- Error RegEx::compile(const String& p_pattern) {
- ERR_FAIL_COND_V(p_pattern.length() == 0, FAILED);
- if (pattern == p_pattern && root)
- return OK;
- clear();
- pattern = p_pattern;
- group_names.push_back(0);
- RegExNodeGroup* root_group = memnew(RegExNodeCapturing(0));
- root = root_group;
- Vector<RegExNodeGroup*> stack;
- stack.push_back(root_group);
- int lookahead_level = 0;
- int numeric_groups = 0;
- const int numeric_max = 9;
- for (const CharType* c = p_pattern.c_str(); *c != '\0'; ++c) {
- switch (c[0]) {
- case '(':
- if (c[1] == '?') {
- RegExNodeGroup* group = NULL;
- switch (c[2]) {
- case ':':
- c = &c[2];
- group = memnew(RegExNodeGroup());
- break;
- case '!':
- case '=':
- group = memnew(RegExNodeLookAhead((c[2] == '!'), lookahead_level++));
- if (lookahead_depth < lookahead_level)
- lookahead_depth = lookahead_level;
- c = &c[2];
- break;
- case '<':
- if (c[3] == '!' || c[3] == '=') {
- group = memnew(RegExNodeLookBehind((c[3] == '!'), lookahead_level++));
- c = &c[3];
- }
- break;
- case 'P':
- if (c[3] == '<') {
- const CharType* d = &c[3];
- Variant name = RegExNodeCapturing::parse_name(d, false);
- if (name == Variant(-1))
- REGEX_COMPILE_FAIL("unrecognised character for group name");
- group = memnew(RegExNodeCapturing(group_names.size()));
- group_names.push_back(name);
- c = d;
- }
- default:
- break;
- }
- if (!group)
- REGEX_COMPILE_FAIL("unrecognised qualifier for group");
- stack[0]->add_child(group);
- stack.insert(0, group);
- } else if (numeric_groups < numeric_max) {
- RegExNodeCapturing* group = memnew(RegExNodeCapturing(group_names.size()));
- group_names.push_back(++numeric_groups);
- stack[0]->add_child(group);
- stack.insert(0, group);
- } else {
- RegExNodeGroup* group = memnew(RegExNodeGroup());
- stack[0]->add_child(group);
- stack.insert(0, group);
- }
- break;
- case ')':
- if (stack.size() == 1)
- REGEX_COMPILE_FAIL("unexpected ')'");
- stack.remove(0);
- break;
- case '\\':
- if (('1' <= c[1] && c[1] <= '9') || (c[1] == 'g' && c[2] == '{')) {
- int ref = 0;
- bool unclosed = false;
- if (c[1] == 'g') {
- unclosed = true;
- c = &c[2];
- }
- while ('0' <= c[1] && c[1] <= '9') {
- ref = ref * 10 + int(c[1] - '0');
- ++c;
- }
- if (unclosed) {
- if (c[1] != '}')
- REGEX_COMPILE_FAIL("unclosed backreference '{'");
- ++c;
- }
- if (ref > numeric_groups || ref <= 0)
- REGEX_COMPILE_FAIL("backreference not found");
- for (int i = 0; i < stack.size(); ++i)
- if (dynamic_cast<RegExNodeLookBehind*>(stack[i]))
- REGEX_COMPILE_FAIL("backreferences inside lookbehind not supported");
- for (int i = 0; i < group_names.size(); ++i) {
- if (group_names[i].get_type() == Variant::INT && int(group_names[i]) == ref) {
- ref = group_names[i];
- break;
- }
- }
- stack[0]->add_child(memnew(RegExNodeBackReference(ref)));
- } if (c[1] =='g' && c[2] == '<') {
- const CharType* d = &c[2];
- Variant name = RegExNodeCapturing::parse_name(d, true);
- if (name == Variant(-1))
- REGEX_COMPILE_FAIL("unrecognised character for group name");
- c = d;
- for (int i = 0; i < stack.size(); ++i)
- if (dynamic_cast<RegExNodeLookBehind*>(stack[i]))
- REGEX_COMPILE_FAIL("backreferences inside lookbehind not supported");
- int ref = -1;
- for (int i = 0; i < group_names.size(); ++i) {
- if (group_names[i].get_type() == Variant::INT && int(group_names[i]) == ref) {
- ref = group_names[i];
- break;
- }
- }
- if (ref == -1)
- REGEX_COMPILE_FAIL("backreference not found");
- stack[0]->add_child(memnew(RegExNodeBackReference(ref)));
- } else if (c[1] == 'b' || c[1] == 'B') {
- stack[0]->add_child(memnew(RegExNodeWordBoundary(*(++c) == 'B')));
- } else if (RegEx_is_shorthand(c[1])) {
- stack[0]->add_child(memnew(RegExNodeShorthand(*(++c))));
- } else {
- const CharType* d = c;
- CharType ch = RegExNodeChar::parse_escape(d);
- if (c == d)
- REGEX_COMPILE_FAIL("invalid escape token");
- stack[0]->add_child(memnew(RegExNodeChar(ch)));
- c = d;
- }
- break;
- case '[':
- {
- RegExNodeBracket* bracket = memnew(RegExNodeBracket());
- stack[0]->add_child(bracket);
- if (c[1] == '^') {
- bracket->inverse = true;
- ++c;
- }
- bool first_child = true;
- CharType previous_child;
- bool previous_child_single = false;
- while (true) {
- ++c;
- if (!first_child && c[0] == ']') {
- break;
- } else if (c[0] == '\0') {
- REGEX_COMPILE_FAIL("unclosed bracket expression '['");
- } else if (c[0] == '\\') {
- if (RegEx_is_shorthand(c[1])) {
- bracket->add_child(memnew(RegExNodeShorthand(*(++c))));
- } else {
- const CharType* d = c;
- CharType ch = RegExNodeChar::parse_escape(d);
- if (c == d)
- REGEX_COMPILE_FAIL("invalid escape token");
- bracket->add_child(memnew(RegExNodeChar(ch)));
- c = d;
- previous_child = ch;
- previous_child_single = true;
- }
- } else if (c[0] == ']' && c[1] == ':') {
- const CharType* d = &c[2];
- RegExNodeClass::Type type = RegExNodeClass::parse_type(d);
- if (type != RegExNodeClass::Type_none) {
- c = d;
- previous_child_single = false;
- } else {
- bracket->add_child(memnew(RegExNodeChar('[')));
- previous_child = '[';
- previous_child_single = true;
- }
- } else if (previous_child_single && c[0] == '-') {
- if (c[1] != '\0' && c[1] != ']') {
- CharType next;
- if (c[1] == '\\') {
- const CharType* d = ++c;
- next = RegExNodeChar::parse_escape(d);
- if (c == d)
- REGEX_COMPILE_FAIL("invalid escape token");
- } else {
- next = *(++c);
- }
- if (next < previous_child)
- REGEX_COMPILE_FAIL("text range out of order");
- bracket->pop_back();
- bracket->add_child(memnew(RegExNodeRange(previous_child, next)));
- previous_child_single = false;
- } else {
- bracket->add_child(memnew(RegExNodeChar('-')));
- previous_child = '-';
- previous_child_single = true;
- }
- } else {
- bracket->add_child(memnew(RegExNodeChar(c[0])));
- previous_child = c[0];
- previous_child_single = true;
- }
- first_child = false;
- }
- }
- break;
- case '|':
- for (int i = 0; i < stack.size(); ++i)
- if (dynamic_cast<RegExNodeLookBehind*>(stack[i]))
- REGEX_COMPILE_FAIL("alternations inside lookbehind not supported");
- stack[0]->add_childset();
- break;
- case '^':
- stack[0]->add_child(memnew(RegExNodeAnchorStart()));
- break;
- case '$':
- stack[0]->add_child(memnew(RegExNodeAnchorEnd()));
- break;
- case '.':
- stack[0]->add_child(memnew(RegExNodeShorthand('.')));
- break;
- case '?':
- case '*':
- case '+':
- case '{':
- {
- int min_val = 0;
- int max_val = -1;
- bool valid = true;
- const CharType* d = c;
- bool max_set = true;
- switch (c[0]) {
- case '?':
- min_val = 0;
- max_val = 1;
- break;
- case '*':
- min_val = 0;
- max_val = -1;
- break;
- case '+':
- min_val = 1;
- max_val = -1;
- break;
- case '{':
- max_set = false;
- while (valid) {
- ++d;
- if (d[0] == '}') {
- break;
- } else if (d[0] == ',') {
- max_set = true;
- } else if ('0' <= d[0] && d[0] <= '9') {
- if (max_set) {
- if (max_val < 0)
- max_val = int(d[0] - '0');
- else
- max_val = max_val * 10 + int(d[0] - '0');
- } else {
- min_val = min_val * 10 + int(d[0] - '0');
- }
- } else {
- valid = false;
- }
- }
- break;
- default:
- break;
- }
- if (!max_set)
- max_val = min_val;
- if (valid) {
- c = d;
- if (stack[0]->back == NULL || !stack[0]->back->quantifiable)
- REGEX_COMPILE_FAIL("element not quantifiable");
- if (min_val != max_val)
- for (int i = 0; i < stack.size(); ++i)
- if (dynamic_cast<RegExNodeLookBehind*>(stack[i]))
- REGEX_COMPILE_FAIL("variable length quantifiers inside lookbehind not supported");
- RegExNodeQuantifier* quant = memnew(RegExNodeQuantifier(min_val, max_val));
- quant->child = stack[0]->swap_back(quant);
- quant->child->previous = NULL;
- quant->child->parent = quant;
- if (min_val == max_val && quant->child->length >= 0)
- quant->length = max_val * quant->child->length;
- if (c[1] == '?') {
- quant->greedy = false;
- ++c;
- }
- break;
- }
- }
- default:
- stack[0]->add_child(memnew(RegExNodeChar(c[0])));
- break;
- }
- }
- if (stack.size() > 1)
- REGEX_COMPILE_FAIL("unclosed group '('");
- return OK;
- }
- Ref<RegExMatch> RegEx::search(const String& p_text, int p_start, int p_end) const {
- ERR_FAIL_COND_V(!is_valid(), NULL);
- ERR_FAIL_COND_V(p_start < 0, NULL);
- ERR_FAIL_COND_V(p_start >= p_text.length(), NULL);
- ERR_FAIL_COND_V(p_end > p_text.length(), NULL);
- ERR_FAIL_COND_V(p_end != -1 && p_end < p_start, NULL);
- Ref<RegExMatch> res = memnew(RegExMatch());
- for (int i = 0; i < group_names.size(); ++i) {
- RegExMatch::Group group;
- group.name = group_names[i];
- res->captures.push_back(group);
- }
- res->string = p_text;
- if (p_end == -1)
- p_end = p_text.length();
- RegExSearch s(res, p_end, lookahead_depth);
- for (int i = p_start; i <= s.end; ++i) {
- for (int c = 0; c < group_names.size(); ++c) {
- res->captures[c].start = -1;
- res->captures[c].length = 0;
- }
- if (root->test(s, i) >= 0)
- break;
- }
- if (res->captures[0].start >= 0)
- return res;
- return NULL;
- }
- String RegEx::sub(const String& p_text, const String& p_replacement, bool p_all, int p_start, int p_end) const {
- ERR_FAIL_COND_V(!is_valid(), p_text);
- ERR_FAIL_COND_V(p_start < 0, p_text);
- ERR_FAIL_COND_V(p_start >= p_text.length(), p_text);
- ERR_FAIL_COND_V(p_end > p_text.length(), p_text);
- ERR_FAIL_COND_V(p_end != -1 && p_end < p_start, p_text);
- String text = p_text;
- int start = p_start;
- if (p_end == -1)
- p_end = p_text.length();
- while (start < text.length() && (p_all || start == p_start)) {
- Ref<RegExMatch> m = search(text, start, p_end);
- RegExMatch::Group& s = m->captures[0];
- if (s.start < 0)
- break;
- String res = text.substr(0, s.start) + m->expand(p_replacement);
- start = res.length();
- if (s.length == 0)
- ++start;
- int sub_end = s.start + s.length;
- if (sub_end < text.length())
- res += text.substr(sub_end, text.length() - sub_end);
- p_end += res.length() - text.length();
- text = res;
- }
- return text;
- }
- void RegEx::clear() {
- if (root)
- memdelete(root);
- root = NULL;
- group_names.clear();
- lookahead_depth = 0;
- }
- bool RegEx::is_valid() const {
- return (root != NULL);
- }
- String RegEx::get_pattern() const {
- return pattern;
- }
- int RegEx::get_group_count() const {
- int count = 0;
- for (int i = 1; i < group_names.size(); ++i)
- if (group_names[i].get_type() == Variant::INT)
- ++count;
- return count;
- }
- Array RegEx::get_names() const {
- Array res;
- for (int i = 1; i < group_names.size(); ++i)
- if (group_names[i].get_type() == Variant::STRING)
- res.push_back(group_names[i]);
- return res;
- }
- RegEx::RegEx() {
- root = NULL;
- lookahead_depth = 0;
- }
- RegEx::RegEx(const String& p_pattern) {
- root = NULL;
- compile(p_pattern);
- }
- RegEx::~RegEx() {
- if (root)
- memdelete(root);
- }
- void RegExMatch::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("expand","template"),&RegExMatch::expand);
- ObjectTypeDB::bind_method(_MD("get_group_count"),&RegExMatch::get_group_count);
- ObjectTypeDB::bind_method(_MD("get_group_array"),&RegExMatch::get_group_array);
- ObjectTypeDB::bind_method(_MD("get_names"),&RegExMatch::get_names);
- ObjectTypeDB::bind_method(_MD("get_name_dict"),&RegExMatch::get_name_dict);
- ObjectTypeDB::bind_method(_MD("get_string","name"),&RegExMatch::get_string, DEFVAL(0));
- ObjectTypeDB::bind_method(_MD("get_start","name"),&RegExMatch::get_start, DEFVAL(0));
- ObjectTypeDB::bind_method(_MD("get_end","name"),&RegExMatch::get_end, DEFVAL(0));
- }
- void RegEx::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("clear"),&RegEx::clear);
- ObjectTypeDB::bind_method(_MD("compile","pattern"),&RegEx::compile);
- ObjectTypeDB::bind_method(_MD("search","text","start","end"),&RegEx::search, DEFVAL(0), DEFVAL(-1));
- ObjectTypeDB::bind_method(_MD("sub","text","replacement","all","start","end"),&RegEx::sub, DEFVAL(false), DEFVAL(0), DEFVAL(-1));
- ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid);
- ObjectTypeDB::bind_method(_MD("get_pattern"),&RegEx::get_pattern);
- ObjectTypeDB::bind_method(_MD("get_group_count"),&RegEx::get_group_count);
- ObjectTypeDB::bind_method(_MD("get_names"),&RegEx::get_names);
- ADD_PROPERTY(PropertyInfo(Variant::STRING, "pattern"), _SCS("compile"), _SCS("get_pattern"));
- }
|