From 87c1e16834a21ac1e42321811cb0efcda4b91b9a Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Fri, 24 Jul 2015 14:09:39 +0100 Subject: [PATCH] Made RegEx API similar to old version --- bin/tests/test_string.cpp | 4 ++-- demos/misc/regex/regex.gd | 4 ++-- demos/misc/regex/regex.scn | Bin 1770 -> 1772 bytes drivers/nrex/regex.cpp | 20 +++++++++++--------- drivers/nrex/regex.h | 4 ++-- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/bin/tests/test_string.cpp b/bin/tests/test_string.cpp index 7b512a5d99..93b1835b78 100644 --- a/bin/tests/test_string.cpp +++ b/bin/tests/test_string.cpp @@ -464,8 +464,8 @@ bool test_26() { OS::get_singleton()->print("\n\nTest 26: RegEx\n"); RegEx regexp("(.*):(.*)"); - bool res = regexp.match("name:password"); - printf("\tmatch: %s\n", res?"true":"false"); + int res = regexp.find("name:password"); + printf("\tmatch: %s\n", (res>=0)?"true":"false"); printf("\t%i captures:\n", regexp.get_capture_count()); for (int i = 0; ig3S0>c4t(yL9eW%Yj?}tN zb`W;(br5#g$9oRp zP7Mz3ju#!8UAi62A22gGs(T)G$aXMxFMhyY>`?Et&Y}K+c=ZEz26xlRsjP}pp3IHC z4!sV#4h#(93M^hF4o@8z4zN3jJ8js!fYp>ybRr*vr>mdKI;Vst28N*EAZJi80RWE% BSc(7u delta 302 zcmaFE`-)dEDA?JV0R#jX7y@`17%oi|RAgkFXq?5xazH`Eftf){fqmohXUr^_xehs# zC$dP?e^K*te#p$g;NjB1&afl(p?jTxl7@!8RZNUtO>~@6tfrQxQi6d4LxU#c4aWp_ zriR7N4pSN!7^gWm2s12A<#V)h4`V83P*9L#h*Mx@_`iTbfh&Q*fzN%tV}}F7kxGYI z4#Ez;4#Ef4IRrXLyIMI&J6JnNKVW8X=yeovh_-d$c9(N7cl2{Gcd&mT9PN0^A>66P z!QJt!L$gb}gZTqy21j+z{SMg<#_stK*oz(NomM&2KM=2ez|P=qI60kFQOe_gW3NN6 ygRTPugSY~VSAoM*2ZjUe4&qL0HZNi|Wfbk@WAJqKb6Mw<(8RzH6ddFX3M2qePgfHF diff --git a/drivers/nrex/regex.cpp b/drivers/nrex/regex.cpp index d7c12bd544..708e68cc26 100644 --- a/drivers/nrex/regex.cpp +++ b/drivers/nrex/regex.cpp @@ -16,13 +16,16 @@ void RegEx::_bind_methods() { ObjectTypeDB::bind_method(_MD("compile","pattern"),&RegEx::compile); - ObjectTypeDB::bind_method(_MD("match","text","start","end"),&RegEx::match, DEFVAL(0), DEFVAL(-1)); + ObjectTypeDB::bind_method(_MD("find","text","start","end"),&RegEx::find, DEFVAL(0), DEFVAL(-1)); + ObjectTypeDB::bind_method(_MD("clear"),&RegEx::clear); + ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid); + ObjectTypeDB::bind_method(_MD("get_capture_count"),&RegEx::get_capture_count); ObjectTypeDB::bind_method(_MD("get_capture","capture"),&RegEx::get_capture); - ObjectTypeDB::bind_method(_MD("get_capture_list"),&RegEx::_bind_get_capture_list); + ObjectTypeDB::bind_method(_MD("get_captures"),&RegEx::_bind_get_captures); }; -StringArray RegEx::_bind_get_capture_list() const { +StringArray RegEx::_bind_get_captures() const { StringArray ret; int count = get_capture_count(); @@ -64,22 +67,21 @@ String RegEx::get_capture(int capture) const { } Error RegEx::compile(const String& p_pattern) { - + clear(); exp.compile(p_pattern.c_str()); ERR_FAIL_COND_V( !exp.valid(), FAILED ); - + captures.resize(exp.capture_size()); return OK; }; -bool RegEx::match(const String& p_text, int p_start, int p_end) const { +int RegEx::find(const String& p_text, int p_start, int p_end) const { - ERR_FAIL_COND_V( !exp.valid(), false ); ERR_FAIL_COND_V( p_text.length() < p_start, false ); ERR_FAIL_COND_V( p_text.length() < p_end, false ); @@ -88,10 +90,10 @@ bool RegEx::match(const String& p_text, int p_start, int p_end) const { if (res) { text = p_text; - return true; + return captures[0].start; } text.clear(); - return false; + return -1; }; diff --git a/drivers/nrex/regex.h b/drivers/nrex/regex.h index 3ef9ca3425..0626029705 100644 --- a/drivers/nrex/regex.h +++ b/drivers/nrex/regex.h @@ -28,7 +28,7 @@ class RegEx : public Reference { protected: static void _bind_methods(); - StringArray _bind_get_capture_list() const; + StringArray _bind_get_captures() const; public: @@ -37,7 +37,7 @@ public: int get_capture_count() const; String get_capture(int capture) const; Error compile(const String& p_pattern); - bool match(const String& p_text, int p_start = 0, int p_end = -1) const; + int find(const String& p_text, int p_start = 0, int p_end = -1) const; RegEx(); RegEx(const String& p_pattern);