From 1efeb8b95662c8583c90724846e6e47a79dd63b5 Mon Sep 17 00:00:00 2001 From: romulox_x Date: Thu, 9 Jul 2015 17:34:12 -0700 Subject: [PATCH 01/14] when copy images enabled, 'images' path prefix was only being written to file if the destination file didn't exist. so it only worked the first time. --- tools/export/blender25/io_scene_dae/export_dae.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/export/blender25/io_scene_dae/export_dae.py b/tools/export/blender25/io_scene_dae/export_dae.py index 5245f32b82b..b846f0e2d8e 100644 --- a/tools/export/blender25/io_scene_dae/export_dae.py +++ b/tools/export/blender25/io_scene_dae/export_dae.py @@ -190,7 +190,7 @@ class DaeExporter: if (not os.path.isfile(dstfile)): shutil.copy(imgpath,dstfile) - imgpath="images/"+os.path.basename(imgpath) + imgpath="images/"+os.path.basename(imgpath) else: ### if file is not found save it as png file in the destination folder img_tmp_path = image.filepath @@ -204,7 +204,7 @@ class DaeExporter: if (not os.path.isfile(dstfile)): image.save() - imgpath="images/"+os.path.basename(image.filepath) + imgpath="images/"+os.path.basename(image.filepath) image.filepath = img_tmp_path else: From 8027a3f00489cd4a5a6d4c85d1ab943cfef1c9db Mon Sep 17 00:00:00 2001 From: Jari Ronkainen Date: Thu, 23 Jul 2015 19:32:10 +0300 Subject: [PATCH 02/14] Fix audio driver setup if the first driver fails. --- platform/x11/os_x11.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index f8c570a5c04..2f35cebee8b 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -267,6 +267,7 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi for(int i=0;iset_singleton(); if (AudioDriverManagerSW::get_driver(i)->init()==OK) { success=true; print_line("Audio Driver Failed: "+String(AudioDriverManagerSW::get_driver(p_audio_driver)->get_name())); From 983fd0dfc3d9bb9c99ebbbd6dd9d312561980ecb Mon Sep 17 00:00:00 2001 From: Jari Ronkainen Date: Thu, 23 Jul 2015 19:35:47 +0300 Subject: [PATCH 03/14] Use tabs instead of spaces. --- platform/x11/os_x11.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 2f35cebee8b..b34d1ba7c8c 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -267,7 +267,7 @@ void OS_X11::initialize(const VideoMode& p_desired,int p_video_driver,int p_audi for(int i=0;iset_singleton(); + AudioDriverManagerSW::get_driver(i)->set_singleton(); if (AudioDriverManagerSW::get_driver(i)->init()==OK) { success=true; print_line("Audio Driver Failed: "+String(AudioDriverManagerSW::get_driver(p_audio_driver)->get_name())); From ef005d4f64d2ee2b35185d4fbdf2aea684cd4966 Mon Sep 17 00:00:00 2001 From: Lee Zher Huei Date: Fri, 24 Jul 2015 01:18:46 +0100 Subject: [PATCH 04/14] Regex library Nrex initial port --- bin/tests/test_string.cpp | 22 +- drivers/SCsub | 2 +- drivers/nrex/README.md | 64 +++ drivers/{trex => nrex}/SCsub | 3 +- drivers/nrex/nrex.cpp | 902 ++++++++++++++++++++++++++++++ drivers/nrex/nrex.hpp | 144 +++++ drivers/nrex/nrex_config.h | 12 + drivers/nrex/regex.cpp | 112 ++++ drivers/{trex => nrex}/regex.h | 19 +- drivers/register_driver_types.cpp | 2 +- drivers/trex/TRexpp.h | 75 --- drivers/trex/history.txt | 15 - drivers/trex/readme.txt | 171 ------ drivers/trex/regex.cpp | 163 ------ drivers/trex/test.c | 41 -- drivers/trex/trex.c | 643 --------------------- drivers/trex/trex.h | 70 --- 17 files changed, 1254 insertions(+), 1206 deletions(-) create mode 100644 drivers/nrex/README.md rename drivers/{trex => nrex}/SCsub (88%) create mode 100644 drivers/nrex/nrex.cpp create mode 100644 drivers/nrex/nrex.hpp create mode 100644 drivers/nrex/nrex_config.h create mode 100644 drivers/nrex/regex.cpp rename drivers/{trex => nrex}/regex.h (64%) delete mode 100644 drivers/trex/TRexpp.h delete mode 100644 drivers/trex/history.txt delete mode 100644 drivers/trex/readme.txt delete mode 100644 drivers/trex/regex.cpp delete mode 100644 drivers/trex/test.c delete mode 100644 drivers/trex/trex.c delete mode 100644 drivers/trex/trex.h diff --git a/bin/tests/test_string.cpp b/bin/tests/test_string.cpp index 3c8d0f7d86d..7b512a5d993 100644 --- a/bin/tests/test_string.cpp +++ b/bin/tests/test_string.cpp @@ -31,7 +31,7 @@ //#include "math_funcs.h" #include #include "os/os.h" -#include "drivers/trex/regex.h" +#include "drivers/nrex/regex.h" #include "test_string.h" @@ -463,20 +463,16 @@ bool test_26() { OS::get_singleton()->print("\n\nTest 26: RegEx\n"); RegEx regexp("(.*):(.*)"); - List captures; - bool match = regexp.match("name:password", &captures); - printf("\tmatch: %s\n", match?"true":"false"); + bool res = regexp.match("name:password"); + printf("\tmatch: %s\n", res?"true":"false"); - printf("\t%i captures:\n", captures.size()); - List::Element *I = captures.front(); - while (I) { - - printf("%ls\n", I->get().c_str()); - - I = I->next(); - }; - return captures.size(); + printf("\t%i captures:\n", regexp.get_capture_count()); + for (int i = 0; i +#include +#define NREX_ISALPHANUM iswalnum +#define NREX_STRLEN wcslen +#else +#include +#include +#define NREX_ISALPHANUM isalnum +#define NREX_STRLEN strlen +#endif + +#ifdef NREX_THROW_ERROR +#define NREX_COMPILE_ERROR(M) throw nrex_compile_error(M) +#else +#define NREX_COMPILE_ERROR(M) reset(); return false +#endif + +#ifndef NREX_NEW +#define NREX_NEW(X) new X +#define NREX_NEW_ARRAY(X, N) new X[N] +#define NREX_DELETE(X) delete X +#define NREX_DELETE_ARRAY(X) delete[] X +#endif + +template +class nrex_array +{ + private: + T* _data; + unsigned int _reserved; + unsigned int _size; + public: + nrex_array() + : _data(NREX_NEW_ARRAY(T, 2)) + , _reserved(2) + , _size(0) + { + } + + ~nrex_array() + { + NREX_DELETE_ARRAY(_data); + } + + unsigned int size() const + { + return _size; + } + + void reserve(unsigned int size) + { + T* old = _data; + _data = NREX_NEW_ARRAY(T, size); + _reserved = size; + for (unsigned int i = 0; i < _size; ++i) + { + _data[i] = old[i]; + } + NREX_DELETE_ARRAY(old); + } + + void push(T item) + { + if (_size == _reserved) + { + reserve(_reserved * 2); + } + _data[_size] = item; + _size++; + } + + T& top() + { + return _data[_size - 1]; + } + + const T& operator[] (unsigned int i) const + { + return _data[i]; + } + + void pop() + { + if (_size > 0) + { + --_size; + } + } +}; + +static nrex_char nrex_unescape(nrex_char repr) +{ + switch (repr) + { + case '^': return '^'; + case '$': return '$'; + case '(': return '('; + case ')': return ')'; + case '\\': return '\\'; + case '.': return '.'; + case '+': return '+'; + case '*': return '*'; + case '?': return '?'; + case '-': return '-'; + case 'a': return '\a'; + case 'e': return '\e'; + case 'f': return '\f'; + case 'n': return '\n'; + case 'r': return '\r'; + case 't': return '\t'; + case 'v': return '\v'; + } + return 0; +} + +struct nrex_search +{ + public: + const nrex_char* str; + nrex_result* captures; + int end; + bool complete; + + nrex_char at(int pos) + { + return str[pos]; + } + + nrex_search(const nrex_char* str, nrex_result* captures) + : str(str) + , captures(captures) + , end(0) + { + } +}; + +struct nrex_node +{ + nrex_node* next; + nrex_node* previous; + nrex_node* parent; + bool quantifiable; + + nrex_node(bool quantify = false) + : next(NULL) + , previous(NULL) + , parent(NULL) + , quantifiable(quantify) + { + } + + virtual ~nrex_node() + { + if (next) + { + delete next; + } + } + + virtual int test(nrex_search* s, int pos) const + { + return next ? next->test(s, pos) : -1; + } + + virtual int test_parent(nrex_search* s, int pos) const + { + if (next) + { + pos = next->test(s, pos); + } + if (parent && pos >= 0) + { + pos = parent->test_parent(s, pos); + } + if (pos >= 0) + { + s->complete = true; + } + return pos; + } +}; + +struct nrex_node_group : public nrex_node +{ + int capturing; + bool negate; + nrex_array childset; + nrex_node* back; + + nrex_node_group(int capturing) + : nrex_node(true) + , capturing(capturing) + , negate(false) + , back(NULL) + { + } + + virtual ~nrex_node_group() + { + for (unsigned int i = 0; i < childset.size(); ++i) + { + delete childset[i]; + } + + } + + int test(nrex_search* s, int pos) const + { + if (capturing >= 0) + { + s->captures[capturing].start = pos; + } + for (unsigned int i = 0; i < childset.size(); ++i) + { + s->complete = false; + int res = childset[i]->test(s, pos); + if (s->complete) + { + return res; + } + if ((res >= 0) != negate) + { + if (capturing >= 0) + { + s->captures[capturing].length = res - pos; + } + return next ? next->test(s, res) : res; + } + } + return -1; + } + + virtual int test_parent(nrex_search* s, int pos) const + { + if (capturing >= 0) + { + s->captures[capturing].length = pos - s->captures[capturing].start; + } + return nrex_node::test_parent(s, pos); + } + + void add_childset() + { + back = NULL; + } + + void add_child(nrex_node* node) + { + node->parent = this; + node->previous = back; + if (back) + { + back->next = node; + } + else + { + childset.push(node); + } + back = node; + } + + nrex_node* swap_back(nrex_node* node) + { + if (!back) + { + add_child(node); + return NULL; + } + nrex_node* old = back; + if (!old->previous) + { + childset.pop(); + } + back = old->previous; + add_child(node); + return old; + } +}; + +struct nrex_node_char : public nrex_node +{ + nrex_char ch; + + nrex_node_char(nrex_char c) + : nrex_node(true) + , ch(c) + { + } + + int test(nrex_search* s, int pos) const + { + if (s->end == pos || s->at(pos) != ch) + { + return -1; + } + return next ? next->test(s, pos + 1) : pos + 1; + } +}; + +struct nrex_node_range : public nrex_node +{ + nrex_char start; + nrex_char end; + + nrex_node_range(nrex_char s, nrex_char e) + : nrex_node(true) + , start(s) + , end(e) + { + } + + int test(nrex_search* s, int pos) const + { + if (s->end == pos) + { + return -1; + } + nrex_char c = s->at(pos); + if (c < start || end < c) + { + return -1; + } + return next ? next->test(s, pos + 1) : pos + 1; + } +}; + +static bool nrex_is_whitespace(nrex_char repr) +{ + switch (repr) + { + case ' ': + case '\t': + case '\r': + case '\n': + case '\f': + return true; + } + return false; +} + +static bool nrex_is_shorthand(nrex_char repr) +{ + switch (repr) + { + case 'W': + case 'w': + case 'D': + case 'd': + case 'S': + case 's': + return true; + } + return false; +} + +struct nrex_node_shorthand : public nrex_node +{ + nrex_char repr; + + nrex_node_shorthand(nrex_char c) + : nrex_node(true) + , repr(c) + { + } + + int test(nrex_search* s, int pos) const + { + if (s->end == pos) + { + return -1; + } + bool found = false; + bool invert = false; + nrex_char c = s->at(pos); + switch (repr) + { + case '.': + found = true; + break; + case 'W': + invert = true; + case 'w': + if (c == '_' || NREX_ISALPHANUM(c)) + { + found = true; + } + break; + case 'D': + invert = true; + case 'd': + if ('0' <= c && c <= '9') + { + found = true; + } + break; + case 'S': + invert = true; + case 's': + if (nrex_is_whitespace(c)) + { + found = true; + } + break; + } + if (found == invert) + { + return -1; + } + return next ? next->test(s, pos + 1) : pos + 1; + } +}; + +static bool nrex_is_quantifier(nrex_char repr) +{ + switch (repr) + { + case '?': + case '*': + case '+': + case '{': + return true; + } + return false; +} + +struct nrex_node_quantifier : public nrex_node +{ + int min; + int max; + bool greedy; + nrex_node* child; + + nrex_node_quantifier() + : nrex_node() + , min(0) + , max(0) + , greedy(true) + , child(NULL) + { + } + + virtual ~nrex_node_quantifier() + { + if (child) + { + delete child; + } + } + + int test(nrex_search* s, int pos) const + { + nrex_array backtrack; + backtrack.push(pos); + s->complete = false; + while (backtrack.top() <= s->end) + { + if (max >= 1 && backtrack.size() > (unsigned int)max) + { + break; + } + if (!greedy && (unsigned int)min < backtrack.size()) + { + int res = backtrack.top(); + if (next) + { + res = next->test(s, res); + } + if (s->complete) + { + return res; + } + if (res >= 0 && parent->test_parent(s, res) >= 0) + { + return res; + } + } + s->complete = false; + int res = child->test(s, backtrack.top()); + if (s->complete) + { + return res; + } + if (res < 0 || res == backtrack.top()) + { + break; + } + backtrack.push(res); + } + while (greedy && (unsigned int) min < backtrack.size()) + { + s->complete = false; + int res = backtrack.top(); + if (s->complete) + { + return res; + } + if (next) + { + res = next->test(s, res); + } + if (res >= 0 && parent->test_parent(s, res) >= 0) + { + return res; + } + backtrack.pop(); + } + return -1; + } +}; + +struct nrex_node_anchor : public nrex_node +{ + bool end; + + nrex_node_anchor(bool end) + : nrex_node() + , end(end) + { + } + + int test(nrex_search* s, int pos) const + { + if (!end && pos != 0) + { + return -1; + } + else if (end && pos != s->end) + { + return -1; + } + return next ? next->test(s, pos) : pos; + } +}; + +struct nrex_node_backreference : public nrex_node +{ + int ref; + + nrex_node_backreference(int ref) + : nrex_node(true) + , ref(ref) + { + } + + int test(nrex_search* s, int pos) const + { + nrex_result& r = s->captures[ref]; + for (int i = 0; i < r.length; ++i) + { + if (pos + i >= s->end) + { + return -1; + } + if (s->at(r.start + i) != s->at(pos + i)) + { + return -1; + } + } + return next ? next->test(s, pos + r.length) : pos + r.length; + } +}; + +nrex::nrex() + : _capturing(0) + , _root(NULL) +{ +} + +nrex::~nrex() +{ + if (_root) + { + delete _root; + } +} + +bool nrex::valid() const +{ + return (_root != NULL); +} + +void nrex::reset() +{ + _capturing = 0; + if (_root) + { + delete _root; + } + _root = NULL; +} + +int nrex::capture_size() const +{ + return _capturing + 1; +} + +bool nrex::compile(const nrex_char* pattern) +{ + reset(); + nrex_node_group* root = NREX_NEW(nrex_node_group(_capturing)); + nrex_array stack; + stack.push(root); + _root = root; + + for (const nrex_char* c = pattern; c[0] != '\0'; ++c) + { + if (c[0] == '(') + { + if (c[1] == '?') + { + if (c[2] == ':') + { + c = &c[2]; + nrex_node_group* group = NREX_NEW(nrex_node_group(-1)); + stack.top()->add_child(group); + stack.push(group); + } + else + { + NREX_COMPILE_ERROR("unrecognised qualifier for parenthesis"); + } + } + else if (_capturing < 99) + { + nrex_node_group* group = NREX_NEW(nrex_node_group(++_capturing)); + stack.top()->add_child(group); + stack.push(group); + } + else + { + nrex_node_group* group = NREX_NEW(nrex_node_group(-1)); + stack.top()->add_child(group); + stack.push(group); + } + } + else if (c[0] == ')') + { + if (stack.size() > 1) + { + stack.pop(); + } + else + { + NREX_COMPILE_ERROR("unexpected ')'"); + } + } + else if (c[0] == '[') + { + nrex_node_group* group = NREX_NEW(nrex_node_group(-1)); + stack.top()->add_child(group); + if (c[1] == '^') + { + group->negate = true; + ++c; + } + while (true) + { + group->add_childset(); + ++c; + if (c[0] == '\0') + { + NREX_COMPILE_ERROR("unclosed character class '[]'"); + } + if (c[0] == ']') + { + break; + } + else if (c[0] == '\\') + { + nrex_char unescaped = nrex_unescape(c[1]); + if (unescaped) + { + group->add_child(NREX_NEW(nrex_node_char(unescaped))); + ++c; + } + else if (nrex_is_shorthand(c[1])) + { + group->add_child(NREX_NEW(nrex_node_shorthand(c[1]))); + ++c; + } + else + { + NREX_COMPILE_ERROR("escape token not recognised"); + } + } + else + { + if (c[1] == '-' && c[2] != '\0') + { + bool range = false; + if ('A' <= c[0] && c[0] <= 'Z' && 'A' <= c[2] && c[2] <= 'Z') + { + range = true; + } + if ('a' <= c[0] && c[0] <= 'z' && 'a' <= c[2] && c[2] <= 'z') + { + range = true; + } + if ('0' <= c[0] && c[0] <= '9' && '0' <= c[2] && c[2] <= '9') + { + range = true; + } + if (range) + { + group->add_child(NREX_NEW(nrex_node_range(c[0], c[2]))); + c = &c[2]; + continue; + } + } + group->add_child(NREX_NEW(nrex_node_char(c[0]))); + } + + } + } + else if (nrex_is_quantifier(c[0])) + { + nrex_node_quantifier* quant = NREX_NEW(nrex_node_quantifier); + quant->child = stack.top()->swap_back(quant); + if (quant->child == NULL || !quant->child->quantifiable) + { + NREX_COMPILE_ERROR("element not quantifiable"); + } + quant->child->previous = NULL; + quant->child->next = NULL; + quant->child->parent = quant; + if (c[0] == '?') + { + quant->min = 0; + quant->max = 1; + } + else if (c[0] == '+') + { + quant->min = 1; + quant->max = -1; + } + else if (c[0] == '*') + { + quant->min = 0; + quant->max = -1; + } + else if (c[0] == '{') + { + bool max_set = false; + quant->min = 0; + quant->max = -1; + while (true) + { + ++c; + if (c[0] == '\0') + { + NREX_COMPILE_ERROR("unclosed range quantifier '{}'"); + } + else if (c[0] == '}') + { + break; + } + else if (c[0] == ',') + { + max_set = true; + continue; + } + else if (c[0] < '0' || '9' < c[0]) + { + NREX_COMPILE_ERROR("expected numeric digits, ',' or '}'"); + } + if (max_set) + { + if (quant->max < 0) + { + quant->max = int(c[0] - '0'); + } + else + { + quant->max = quant->max * 10 + int(c[0] - '0'); + } + } + else + { + quant->min = quant->min * 10 + int(c[0] - '0'); + } + } + if (!max_set) + { + quant->max = quant->min; + } + } + if (c[1] == '?') + { + quant->greedy = false; + ++c; + } + } + else if (c[0] == '|') + { + stack.top()->add_childset(); + } + else if (c[0] == '^' || c[0] == '$') + { + stack.top()->add_child(NREX_NEW(nrex_node_anchor((c[0] == '$')))); + } + else if (c[0] == '.') + { + stack.top()->add_child(NREX_NEW(nrex_node_shorthand('.'))); + } + else if (c[0] == '\\') + { + nrex_char unescaped = nrex_unescape(c[1]); + if (unescaped) + { + stack.top()->add_child(NREX_NEW(nrex_node_char(unescaped))); + ++c; + } + else if (nrex_is_shorthand(c[1])) + { + stack.top()->add_child(NREX_NEW(nrex_node_shorthand(c[1]))); + ++c; + } + else if ('1' <= c[1] && c[1] <= '9') + { + int ref = 0; + if ('0' <= c[2] && c[2] <= '9') + { + ref = int(c[1] - '0') * 10 + int(c[2] - '0'); + c = &c[2]; + } + else + { + ref = int(c[1] - '0'); + ++c; + } + if (ref > _capturing) + { + NREX_COMPILE_ERROR("backreference to non-existent capture"); + } + stack.top()->add_child(NREX_NEW(nrex_node_backreference(ref))); + } + else + { + NREX_COMPILE_ERROR("escape token not recognised"); + } + } + else + { + stack.top()->add_child(NREX_NEW(nrex_node_char(c[0]))); + } + } + return true; +} + +bool nrex::match(const nrex_char* str, nrex_result* captures, int offset, int end) const +{ + nrex_search s(str, captures); + if (end >= offset) + { + s.end = end; + } + else + { + s.end = NREX_STRLEN(str); + } + for (int i = offset; i < s.end; ++i) + { + for (int c = 0; c <= _capturing; ++c) + { + captures[c].start = 0; + captures[c].length = 0; + } + if (_root->test(&s, i) >= 0) + { + return true; + } + } + return false; +} diff --git a/drivers/nrex/nrex.hpp b/drivers/nrex/nrex.hpp new file mode 100644 index 00000000000..2a6aa08e1d4 --- /dev/null +++ b/drivers/nrex/nrex.hpp @@ -0,0 +1,144 @@ +// NREX: Node RegEx +// +// Copyright (c) 2015, Zher Huei Lee +// All rights reserved. +// +// This software is provided 'as-is', without any express or implied +// warranty. In no event will the authors be held liable for any damages +// arising from the use of this software. +// +// Permission is granted to anyone to use this software for any purpose, +// including commercial applications, and to alter it and redistribute it +// freely, subject to the following restrictions: +// +// 1. The origin of this software must not be misrepresented; you must not +// claim that you wrote the original software. If you use this software +// in a product, an acknowledgment in the product documentation would +// be appreciated but is not required. +// +// 2. Altered source versions must be plainly marked as such, and must not +// be misrepresented as being the original software. +// +// 3. This notice may not be removed or altered from any source +// distribution. +// + +#ifndef NREX_HPP +#define NREX_HPP + +#include "nrex_config.h" + +#ifdef NREX_UNICODE +typedef wchar_t nrex_char; +#else +typedef char nrex_char; +#endif + +/*! + * \brief Struct to contain the range of a capture result + * + * The range provided is relative to the begining of the searched string. + * + * \see nrex_node::match() + */ +struct nrex_result +{ + public: + int start; /*!< Start of text range */ + int length; /*!< Length of text range */ +}; + +class nrex_node; + +/*! + * \brief Holds the compiled regex pattern + */ +class nrex +{ + private: + int _capturing; + nrex_node* _root; + public: + nrex(); + ~nrex(); + + /*! + * \brief Removes the compiled regex and frees up the memory + */ + void reset(); + + /*! + * \brief Checks if there is a compiled regex being stored + * \return True if present, False if not present + */ + bool valid() const; + + /*! + * \brief Provides number of captures the compiled regex uses + * + * This is used to provide the array size of the captures needed for + * nrex::match() to work. The size is actually the number of capture + * groups + one for the matching of the entire pattern. The result is + * always capped at 100. + * + * \return The number of captures + */ + int capture_size() const; + + /*! + * \brief Compiles the provided regex pattern + * + * This automatically removes the existing compiled regex if already + * present. + * + * If the NREX_THROW_ERROR was defined it would automatically throw a + * runtime error nrex_compile_error if it encounters a problem when + * parsing the pattern. + * + * \param The regex pattern + * \return True if the pattern was succesfully compiled + */ + bool compile(const nrex_char* pattern); + + /*! + * \brief Uses the pattern to search through the provided string + * \param str The text to search through. It only needs to be + * null terminated if the end point is not provided. + * This also determines the starting anchor. + * \param captures The array of results to store the capture results. + * The size of that array needs to be the same as the + * size given in nrex::capture_size(). As it matches + * the function fills the array with the results. 0 is + * the result for the entire pattern, 1 and above + * corresponds to the regex capture group if present. + * \param offset The starting point of the search. This does not move + * the starting anchor. Defaults to 0. + * \param end The end point of the search. This also determines + * the ending anchor. If a number less than the offset + * is provided, the search would be done until null + * termination. Defaults to -1. + * \return True if a match was found. False otherwise. + */ + bool match(const nrex_char* str, nrex_result* captures, int offset = 0, int end = -1) const; +}; + +#ifdef NREX_THROW_ERROR + +#include + +class nrex_compile_error : std::runtime_error +{ + public: + nrex_compile_error(const char* message) + : std::runtime_error(message) + { + } + + ~nrex_compile_error() throw() + { + } +}; + +#endif + +#endif // NREX_HPP diff --git a/drivers/nrex/nrex_config.h b/drivers/nrex/nrex_config.h new file mode 100644 index 00000000000..540f34f8b49 --- /dev/null +++ b/drivers/nrex/nrex_config.h @@ -0,0 +1,12 @@ +// Godot-specific configuration +// To use this, replace nrex_config.h + +#include "core/os/memory.h" + +#define NREX_UNICODE +//#define NREX_THROW_ERROR + +#define NREX_NEW(X) memnew(X) +#define NREX_NEW_ARRAY(X, N) memnew_arr(X, N) +#define NREX_DELETE(X) memdelete(X) +#define NREX_DELETE_ARRAY(X) memdelete_arr(X) diff --git a/drivers/nrex/regex.cpp b/drivers/nrex/regex.cpp new file mode 100644 index 00000000000..d7c12bd5448 --- /dev/null +++ b/drivers/nrex/regex.cpp @@ -0,0 +1,112 @@ +/*************************************************/ +/* regex.cpp */ +/*************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/*************************************************/ +/* Source code within this file is: */ +/* (c) 2007-2010 Juan Linietsky, Ariel Manzur */ +/* All Rights Reserved. */ +/*************************************************/ + +#include "regex.h" +#include "nrex.hpp" +#include "core/os/memory.h" + +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("get_capture","capture"),&RegEx::get_capture); + ObjectTypeDB::bind_method(_MD("get_capture_list"),&RegEx::_bind_get_capture_list); + +}; + +StringArray RegEx::_bind_get_capture_list() const { + + StringArray ret; + int count = get_capture_count(); + for (int i=0; i captures; + nrex exp; protected: static void _bind_methods(); + StringArray _bind_get_capture_list() const; - int _bind_find(const String& p_text, int p_start = 0, int p_end = -1) const; - StringArray _bind_get_captures() const; public: void clear(); - - Error compile(const String& p_pattern); bool is_valid() const; - bool match(const String& p_text, List* p_captures = NULL, int p_start = 0, int p_end = -1) const; - bool find(const String& p_text, int& p_rstart, int &p_rend, List* p_captures = NULL, int p_start = 0, int p_end = -1) const; int get_capture_count() const; - Error get_capture_limits(int p_capture, int& p_start, int& p_len) const; - String get_capture(int p_idx) 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; RegEx(); RegEx(const String& p_pattern); diff --git a/drivers/register_driver_types.cpp b/drivers/register_driver_types.cpp index e730171fbba..01f6a8b5b00 100644 --- a/drivers/register_driver_types.cpp +++ b/drivers/register_driver_types.cpp @@ -48,7 +48,7 @@ #endif -#include "drivers/trex/regex.h" +#include "drivers/nrex/regex.h" #ifdef MUSEPACK_ENABLED #include "mpc/audio_stream_mpc.h" diff --git a/drivers/trex/TRexpp.h b/drivers/trex/TRexpp.h deleted file mode 100644 index 8391e47414e..00000000000 --- a/drivers/trex/TRexpp.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef _TREXPP_H_ -#define _TREXPP_H_ -/*************************************************************** - T-Rex a tiny regular expression library - - Copyright (C) 2003-2004 Alberto Demichelis - - This software is provided 'as-is', without any express - or implied warranty. In no event will the authors be held - liable for any damages arising from the use of this software. - - Permission is granted to anyone to use this software for - any purpose, including commercial applications, and to alter - it and redistribute it freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; - you must not claim that you wrote the original software. - If you use this software in a product, an acknowledgment - in the product documentation would be appreciated but - is not required. - - 2. Altered source versions must be plainly marked as such, - and must not be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any - source distribution. - -****************************************************************/ - -extern "C" { -#include "trex.h" -} - -struct TRexParseException{TRexParseException(const TRexChar *c):desc(c){}const TRexChar *desc;}; - -class TRexpp { -public: - TRexpp() { _exp = (TRex *)0; } - ~TRexpp() { CleanUp(); } - // compiles a regular expression - void Compile(const TRexChar *pattern) { - const TRexChar *error; - CleanUp(); - if(!(_exp = trex_compile(pattern,&error))) - throw TRexParseException(error); - } - // return true if the given text match the expression - bool Match(const TRexChar* text) { - return _exp?(trex_match(_exp,text) != 0):false; - } - // Searches for the first match of the expression in a zero terminated string - bool Search(const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) { - return _exp?(trex_search(_exp,text,out_begin,out_end) != 0):false; - } - // Searches for the first match of the expression in a string sarting at text_begin and ending at text_end - bool SearchRange(const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end) { - return _exp?(trex_searchrange(_exp,text_begin,text_end,out_begin,out_end) != 0):false; - } - bool GetSubExp(int n, const TRexChar** out_begin, int *out_len) - { - TRexMatch match; - TRexBool res = _exp?(trex_getsubexp(_exp,n,&match)):TRex_False; - if(res) { - *out_begin = match.begin; - *out_len = match.len; - return true; - } - return false; - } - int GetSubExpCount() { return _exp?trex_getsubexpcount(_exp):0; } -private: - void CleanUp() { if(_exp) trex_free(_exp); _exp = (TRex *)0; } - TRex *_exp; -}; -#endif //_TREXPP_H_ \ No newline at end of file diff --git a/drivers/trex/history.txt b/drivers/trex/history.txt deleted file mode 100644 index 5cfe8770b4a..00000000000 --- a/drivers/trex/history.txt +++ /dev/null @@ -1,15 +0,0 @@ -===version 1.3 --fixed a bug for GCC users(thx Brendan) - -===version 1.2 --added word boundary match \b and \B --added vertical tab escape \v --\w now also matches '_' (underscore) --fixed greediness for * and + - -===version 1.1 , April 1, 2004 --fixed some minor bug --added predefined character classes(\w,\W,\s,\S etc...) - -===version 1.0 , February 23, 2004 --first public realase \ No newline at end of file diff --git a/drivers/trex/readme.txt b/drivers/trex/readme.txt deleted file mode 100644 index 1d93558e921..00000000000 --- a/drivers/trex/readme.txt +++ /dev/null @@ -1,171 +0,0 @@ -T-REX 1.3 http://tiny-rex.sourceforge.net ----------------------------------------------------------------------- - T-Rex a tiny regular expression library - - Copyright (C) 2003-2006 Alberto Demichelis - - This software is provided 'as-is', without any express - or implied warranty. In no event will the authors be held - liable for any damages arising from the use of this software. - - Permission is granted to anyone to use this software for - any purpose, including commercial applications, and to alter - it and redistribute it freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; - you must not claim that you wrote the original software. - If you use this software in a product, an acknowledgment - in the product documentation would be appreciated but - is not required. - - 2. Altered source versions must be plainly marked as such, - and must not be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any - source distribution. - ----------------------------------------------------------------------- -TRex implements the following expressions - -\ Quote the next metacharacter -^ Match the beginning of the string -. Match any character -$ Match the end of the string -| Alternation -() Grouping (creates a capture) -[] Character class - -==GREEDY CLOSURES== -* Match 0 or more times -+ Match 1 or more times -? Match 1 or 0 times -{n} Match exactly n times -{n,} Match at least n times -{n,m} Match at least n but not more than m times - -==ESCAPE CHARACTERS== -\t tab (HT, TAB) -\n newline (LF, NL) -\r return (CR) -\f form feed (FF) - -==PREDEFINED CLASSES== -\l lowercase next char -\u uppercase next char -\a letters -\A non letters -\w alphanimeric [0-9a-zA-Z] -\W non alphanimeric -\s space -\S non space -\d digits -\D non nondigits -\x exadecimal digits -\X non exadecimal digits -\c control charactrs -\C non control charactrs -\p punctation -\P non punctation -\b word boundary -\B non word boundary - ----------------------------------------------------------------------- -API DOC ----------------------------------------------------------------------- -TRex *trex_compile(const TRexChar *pattern,const TRexChar **error); - -compiles an expression and returns a pointer to the compiled version. -in case of failure returns NULL.The returned object has to be deleted -through the function trex_free(). - -pattern - a pointer to a zero terminated string containing the pattern that - has to be compiled. -error - apointer to a string pointer that will be set with an error string - in case of failure. - ----------------------------------------------------------------------- -void trex_free(TRex *exp) - -deletes a expression structure created with trex_compile() - -exp - the expression structure that has to be deleted - ----------------------------------------------------------------------- -TRexBool trex_match(TRex* exp,const TRexChar* text) - -returns TRex_True if the string specified in the parameter text is an -exact match of the expression, otherwise returns TRex_False. - -exp - the compiled expression -text - the string that has to be tested - ----------------------------------------------------------------------- -TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) - -searches the first match of the expressin in the string specified in the parameter text. -if the match is found returns TRex_True and the sets out_begin to the beginning of the -match and out_end at the end of the match; otherwise returns TRex_False. - -exp - the compiled expression -text - the string that has to be tested -out_begin - a pointer to a string pointer that will be set with the beginning of the match -out_end - a pointer to a string pointer that will be set with the end of the match - ----------------------------------------------------------------------- -TREX_API TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end) - -searches the first match of the expressin in the string delimited -by the parameter text_begin and text_end. -if the match is found returns TRex_True and the sets out_begin to the beginning of the -match and out_end at the end of the match; otherwise returns TRex_False. - -exp - the compiled expression -text_begin - a pointer to the beginnning of the string that has to be tested -text_end - a pointer to the end of the string that has to be tested -out_begin - a pointer to a string pointer that will be set with the beginning of the match -out_end - a pointer to a string pointer that will be set with the end of the match - ----------------------------------------------------------------------- -int trex_getsubexpcount(TRex* exp) - -returns the number of sub expressions matched by the expression - -exp - the compiled expression - ---------------------------------------------------------------------- -TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *submatch) - -retrieve the begin and and pointer to the length of the sub expression indexed -by n. The result is passed trhough the struct TRexMatch: - -typedef struct { - const TRexChar *begin; - int len; -} TRexMatch; - -the function returns TRex_True if n is valid index otherwise TRex_False. - -exp - the compiled expression -n - the index of the submatch -submatch - a pointer to structure that will store the result - -this function works also after a match operation has been performend. - diff --git a/drivers/trex/regex.cpp b/drivers/trex/regex.cpp deleted file mode 100644 index 11cd6256e29..00000000000 --- a/drivers/trex/regex.cpp +++ /dev/null @@ -1,163 +0,0 @@ -/*************************************************/ -/* regex.cpp */ -/*************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/*************************************************/ -/* Source code within this file is: */ -/* (c) 2007-2010 Juan Linietsky, Ariel Manzur */ -/* All Rights Reserved. */ -/*************************************************/ - -#include "regex.h" - -extern "C" { - -#define _UNICODE -#include "trex.h" - -}; - -void RegEx::_bind_methods() { - - ObjectTypeDB::bind_method(_MD("compile","pattern"),&RegEx::compile); - ObjectTypeDB::bind_method(_MD("find","text", "start","end"),&RegEx::_bind_find, DEFVAL(0), DEFVAL(-1)); - ObjectTypeDB::bind_method(_MD("get_captures"),&RegEx::_bind_get_captures); -}; - -Error RegEx::compile(const String& p_pattern) { - - clear(); - const TRexChar* error; - exp = trex_compile(p_pattern.c_str(), &error); - ERR_FAIL_COND_V(!exp, FAILED); - return OK; -}; - - -int RegEx::_bind_find(const String& p_text, int p_start, int p_end) const { - - int start, end; - bool ret = find(p_text, start, end, NULL, p_start, p_end); - - return ret?start:-1; -}; - -bool RegEx::find(const String& p_text, int& p_rstart, int &p_rend, List* p_captures, int p_start, int p_end) const { - - ERR_FAIL_COND_V( !exp, false ); - text=p_text; - - const CharType* str = p_text.c_str(); - const CharType* start = str + p_start; - const CharType* end = str + (p_end == -1?p_text.size():p_end); - - const CharType* out_begin; - const CharType* out_end; - - bool ret = trex_searchrange(exp, start, end, &out_begin, &out_end); - if (ret) { - - p_rstart = out_begin - str; - p_rend = out_end - str; - - if (p_captures) { - - int count = get_capture_count(); - for (int i=0; ipush_back(p_text.substr(start, len)); - }; - }; - } else { - - p_rstart = -1; - }; - - return ret; -}; - - -bool RegEx::match(const String& p_text, List* p_captures, int p_start, int p_end) const { - - ERR_FAIL_COND_V( !exp, false ); - - int start, end; - return find(p_text, start, end, p_captures, p_start, p_end); -}; - -int RegEx::get_capture_count() const { - - ERR_FAIL_COND_V( exp == NULL, -1 ); - - return trex_getsubexpcount(exp); -}; - -Error RegEx::get_capture_limits(int p_capture, int& p_start, int& p_len) const { - - ERR_FAIL_COND_V( exp == NULL, ERR_UNCONFIGURED ); - - TRexMatch match; - TRexBool res = trex_getsubexp(exp, p_capture, &match); - ERR_FAIL_COND_V( !res, FAILED ); - p_start = (int)(match.begin - text.c_str()); - p_len = match.len; - - return OK; -}; - -String RegEx::get_capture(int p_idx) const { - - ERR_FAIL_COND_V( exp == NULL, "" ); - int start, len; - Error ret = get_capture_limits(p_idx, start, len); - ERR_FAIL_COND_V(ret != OK, ""); - if (len == 0) - return ""; - return text.substr(start, len); -}; - -StringArray RegEx::_bind_get_captures() const { - - StringArray ret; - int count = get_capture_count(); - for (int i=0; i -#include - -#ifdef _UNICODE -#define trex_sprintf swprintf -#else -#define trex_sprintf sprintf -#endif - -int main(int argc, char* argv[]) -{ - const TRexChar *begin,*end; - TRexChar sTemp[200]; - const TRexChar *error = NULL; - TRex *x = trex_compile(_TREXC("(x{1,5})xx"),&error); - if(x) { - trex_sprintf(sTemp,_TREXC("xxxxxxx")); - if(trex_search(x,sTemp,&begin,&end)) - { - int i,n = trex_getsubexpcount(x); - TRexMatch match; - for(i = 0; i < n; i++) - { - TRexChar t[200]; - trex_getsubexp(x,i,&match); - trex_sprintf(t,_TREXC("[%%d]%%.%ds\n"),match.len); - trex_printf(t,i,match.begin); - } - trex_printf(_TREXC("match! %d sub matches\n"),trex_getsubexpcount(x)); - } - else { - trex_printf(_TREXC("no match!\n")); - } - trex_free(x); - } - else { - trex_printf(_TREXC("compilation error [%s]!\n"),error?error:_TREXC("undefined")); - } - return 0; -} diff --git a/drivers/trex/trex.c b/drivers/trex/trex.c deleted file mode 100644 index b3668c3a117..00000000000 --- a/drivers/trex/trex.c +++ /dev/null @@ -1,643 +0,0 @@ - /* see copyright notice in trex.h */ -#include -#include -#include -#include -#include "trex.h" - -#ifdef _UINCODE -#define scisprint iswprint -#define scstrlen wcslen -#define scprintf wprintf -#define _SC(x) L##c -#else -#define scisprint isprint -#define scstrlen strlen -#define scprintf printf -#define _SC(x) (x) -#endif - -#ifdef _DEBUG -#include - -static const TRexChar *g_nnames[] = -{ - _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"), - _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"), - _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"), - _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB") -}; - -#endif -#define OP_GREEDY (MAX_CHAR+1) // * + ? {n} -#define OP_OR (MAX_CHAR+2) -#define OP_EXPR (MAX_CHAR+3) //parentesis () -#define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:) -#define OP_DOT (MAX_CHAR+5) -#define OP_CLASS (MAX_CHAR+6) -#define OP_CCLASS (MAX_CHAR+7) -#define OP_NCLASS (MAX_CHAR+8) //negates class the [^ -#define OP_RANGE (MAX_CHAR+9) -#define OP_CHAR (MAX_CHAR+10) -#define OP_EOL (MAX_CHAR+11) -#define OP_BOL (MAX_CHAR+12) -#define OP_WB (MAX_CHAR+13) - -#define TREX_SYMBOL_ANY_CHAR ('.') -#define TREX_SYMBOL_GREEDY_ONE_OR_MORE ('+') -#define TREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*') -#define TREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?') -#define TREX_SYMBOL_BRANCH ('|') -#define TREX_SYMBOL_END_OF_STRING ('$') -#define TREX_SYMBOL_BEGINNING_OF_STRING ('^') -#define TREX_SYMBOL_ESCAPE_CHAR ('\\') - - -typedef int TRexNodeType; - -typedef struct tagTRexNode{ - TRexNodeType type; - int left; - int right; - int next; -}TRexNode; - -struct TRex{ - const TRexChar *_eol; - const TRexChar *_bol; - const TRexChar *_p; - int _first; - int _op; - TRexNode *_nodes; - int _nallocated; - int _nsize; - int _nsubexpr; - TRexMatch *_matches; - int _currsubexp; - void *_jmpbuf; - const TRexChar **_error; -}; - -static int trex_list(TRex *exp); - -static int trex_newnode(TRex *exp, TRexNodeType type) -{ - TRexNode n; - int newid; - n.type = type; - n.next = n.right = n.left = -1; - if(type == OP_EXPR) - n.right = exp->_nsubexpr++; - if(exp->_nallocated < (exp->_nsize + 1)) { - //int oldsize = exp->_nallocated; - exp->_nallocated *= 2; - exp->_nodes = (TRexNode *)realloc(exp->_nodes, exp->_nallocated * sizeof(TRexNode)); - } - exp->_nodes[exp->_nsize++] = n; - newid = exp->_nsize - 1; - return (int)newid; -} - -static void trex_error(TRex *exp,const TRexChar *error) -{ - if(exp->_error) *exp->_error = error; - longjmp(*((jmp_buf*)exp->_jmpbuf),-1); -} - -static void trex_expect(TRex *exp, int n){ - if((*exp->_p) != n) - trex_error(exp, _SC("expected paren")); - exp->_p++; -} - -static TRexChar trex_escapechar(TRex *exp) -{ - if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR){ - exp->_p++; - switch(*exp->_p) { - case 'v': exp->_p++; return '\v'; - case 'n': exp->_p++; return '\n'; - case 't': exp->_p++; return '\t'; - case 'r': exp->_p++; return '\r'; - case 'f': exp->_p++; return '\f'; - default: return (*exp->_p++); - } - } else if(!scisprint(*exp->_p)) trex_error(exp,_SC("letter expected")); - return (*exp->_p++); -} - -static int trex_charclass(TRex *exp,int classid) -{ - int n = trex_newnode(exp,OP_CCLASS); - exp->_nodes[n].left = classid; - return n; -} - -static int trex_charnode(TRex *exp,TRexBool isclass) -{ - TRexChar t; - if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR) { - exp->_p++; - switch(*exp->_p) { - case 'n': exp->_p++; return trex_newnode(exp,'\n'); - case 't': exp->_p++; return trex_newnode(exp,'\t'); - case 'r': exp->_p++; return trex_newnode(exp,'\r'); - case 'f': exp->_p++; return trex_newnode(exp,'\f'); - case 'v': exp->_p++; return trex_newnode(exp,'\v'); - case 'a': case 'A': case 'w': case 'W': case 's': case 'S': - case 'd': case 'D': case 'x': case 'X': case 'c': case 'C': - case 'p': case 'P': case 'l': case 'u': - { - t = *exp->_p; exp->_p++; - return trex_charclass(exp,t); - } - case 'b': - case 'B': - if(!isclass) { - int node = trex_newnode(exp,OP_WB); - exp->_nodes[node].left = *exp->_p; - exp->_p++; - return node; - } //else default - default: - t = *exp->_p; exp->_p++; - return trex_newnode(exp,t); - } - } - else if(!scisprint(*exp->_p)) { - - trex_error(exp,_SC("letter expected")); - } - t = *exp->_p; exp->_p++; - return trex_newnode(exp,t); -} -static int trex_class(TRex *exp) -{ - int ret = -1; - int first = -1,chain; - if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING){ - ret = trex_newnode(exp,OP_NCLASS); - exp->_p++; - }else ret = trex_newnode(exp,OP_CLASS); - - if(*exp->_p == ']') trex_error(exp,_SC("empty class")); - chain = ret; - while(*exp->_p != ']' && exp->_p != exp->_eol) { - if(*exp->_p == '-' && first != -1){ - int r,t; - if(*exp->_p++ == ']') trex_error(exp,_SC("unfinished range")); - r = trex_newnode(exp,OP_RANGE); - if(first>*exp->_p) trex_error(exp,_SC("invalid range")); - if(exp->_nodes[first].type == OP_CCLASS) trex_error(exp,_SC("cannot use character classes in ranges")); - exp->_nodes[r].left = exp->_nodes[first].type; - t = trex_escapechar(exp); - exp->_nodes[r].right = t; - exp->_nodes[chain].next = r; - chain = r; - first = -1; - } - else{ - if(first!=-1){ - int c = first; - exp->_nodes[chain].next = c; - chain = c; - first = trex_charnode(exp,TRex_True); - } - else{ - first = trex_charnode(exp,TRex_True); - } - } - } - if(first!=-1){ - int c = first; - exp->_nodes[chain].next = c; - chain = c; - first = -1; - } - /* hack? */ - exp->_nodes[ret].left = exp->_nodes[ret].next; - exp->_nodes[ret].next = -1; - return ret; -} - -static int trex_parsenumber(TRex *exp) -{ - int ret = *exp->_p-'0'; - int positions = 10; - exp->_p++; - while(isdigit(*exp->_p)) { - ret = ret*10+(*exp->_p++-'0'); - if(positions==1000000000) trex_error(exp,_SC("overflow in numeric constant")); - positions *= 10; - }; - return ret; -} - -static int trex_element(TRex *exp) -{ - int ret = -1; - switch(*exp->_p) - { - case '(': { - int expr,newn; - exp->_p++; - - - if(*exp->_p =='?') { - exp->_p++; - trex_expect(exp,':'); - expr = trex_newnode(exp,OP_NOCAPEXPR); - } - else - expr = trex_newnode(exp,OP_EXPR); - newn = trex_list(exp); - exp->_nodes[expr].left = newn; - ret = expr; - trex_expect(exp,')'); - } - break; - case '[': - exp->_p++; - ret = trex_class(exp); - trex_expect(exp,']'); - break; - case TREX_SYMBOL_END_OF_STRING: exp->_p++; ret = trex_newnode(exp,OP_EOL);break; - case TREX_SYMBOL_ANY_CHAR: exp->_p++; ret = trex_newnode(exp,OP_DOT);break; - default: - ret = trex_charnode(exp,TRex_False); - break; - } - - { - int op; - TRexBool isgreedy = TRex_False; - unsigned short p0 = 0, p1 = 0; - switch(*exp->_p){ - case TREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break; - case TREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break; - case TREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = TRex_True; break; - case '{': - exp->_p++; - if(!isdigit(*exp->_p)) trex_error(exp,_SC("number expected")); - p0 = (unsigned short)trex_parsenumber(exp); - /*******************************/ - switch(*exp->_p) { - case '}': - p1 = p0; exp->_p++; - break; - case ',': - exp->_p++; - p1 = 0xFFFF; - if(isdigit(*exp->_p)){ - p1 = (unsigned short)trex_parsenumber(exp); - } - trex_expect(exp,'}'); - break; - default: - trex_error(exp,_SC(", or } expected")); - } - /*******************************/ - isgreedy = TRex_True; - break; - - } - if(isgreedy) { - int nnode = trex_newnode(exp,OP_GREEDY); - op = OP_GREEDY; - exp->_nodes[nnode].left = ret; - exp->_nodes[nnode].right = ((p0)<<16)|p1; - ret = nnode; - } - } - if((*exp->_p != TREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != TREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != TREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) { - int nnode = trex_element(exp); - exp->_nodes[ret].next = nnode; - } - - return ret; -} - -static int trex_list(TRex *exp) -{ - int ret=-1,e; - if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING) { - exp->_p++; - ret = trex_newnode(exp,OP_BOL); - } - e = trex_element(exp); - if(ret != -1) { - exp->_nodes[ret].next = e; - } - else ret = e; - - if(*exp->_p == TREX_SYMBOL_BRANCH) { - int temp,tright; - exp->_p++; - temp = trex_newnode(exp,OP_OR); - exp->_nodes[temp].left = ret; - tright = trex_list(exp); - exp->_nodes[temp].right = tright; - ret = temp; - } - return ret; -} - -static TRexBool trex_matchcclass(int cclass,TRexChar c) -{ - switch(cclass) { - case 'a': return isalpha(c)?TRex_True:TRex_False; - case 'A': return !isalpha(c)?TRex_True:TRex_False; - case 'w': return (isalnum(c) || c == '_')?TRex_True:TRex_False; - case 'W': return (!isalnum(c) && c != '_')?TRex_True:TRex_False; - case 's': return isspace(c)?TRex_True:TRex_False; - case 'S': return !isspace(c)?TRex_True:TRex_False; - case 'd': return isdigit(c)?TRex_True:TRex_False; - case 'D': return !isdigit(c)?TRex_True:TRex_False; - case 'x': return isxdigit(c)?TRex_True:TRex_False; - case 'X': return !isxdigit(c)?TRex_True:TRex_False; - case 'c': return iscntrl(c)?TRex_True:TRex_False; - case 'C': return !iscntrl(c)?TRex_True:TRex_False; - case 'p': return ispunct(c)?TRex_True:TRex_False; - case 'P': return !ispunct(c)?TRex_True:TRex_False; - case 'l': return islower(c)?TRex_True:TRex_False; - case 'u': return isupper(c)?TRex_True:TRex_False; - } - return TRex_False; /*cannot happen*/ -} - -static TRexBool trex_matchclass(TRex* exp,TRexNode *node,TRexChar c) -{ - do { - switch(node->type) { - case OP_RANGE: - if(c >= node->left && c <= node->right) return TRex_True; - break; - case OP_CCLASS: - if(trex_matchcclass(node->left,c)) return TRex_True; - break; - default: - if(c == node->type)return TRex_True; - } - } while((node->next != -1) && (node = &exp->_nodes[node->next])); - return TRex_False; -} - -static const TRexChar *trex_matchnode(TRex* exp,TRexNode *node,const TRexChar *str,TRexNode *next) -{ - - TRexNodeType type = node->type; - switch(type) { - case OP_GREEDY: { - //TRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL; - TRexNode *greedystop = NULL; - int p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0; - const TRexChar *s=str, *good = str; - - if(node->next != -1) { - greedystop = &exp->_nodes[node->next]; - } - else { - greedystop = next; - } - - while((nmaches == 0xFFFF || nmaches < p1)) { - - const TRexChar *stop; - if(!(s = trex_matchnode(exp,&exp->_nodes[node->left],s,greedystop))) - break; - nmaches++; - good=s; - if(greedystop) { - //checks that 0 matches satisfy the expression(if so skips) - //if not would always stop(for instance if is a '?') - if(greedystop->type != OP_GREEDY || - (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0)) - { - TRexNode *gnext = NULL; - if(greedystop->next != -1) { - gnext = &exp->_nodes[greedystop->next]; - }else if(next && next->next != -1){ - gnext = &exp->_nodes[next->next]; - } - stop = trex_matchnode(exp,greedystop,s,gnext); - if(stop) { - //if satisfied stop it - if(p0 == p1 && p0 == nmaches) break; - else if(nmaches >= p0 && p1 == 0xFFFF) break; - else if(nmaches >= p0 && nmaches <= p1) break; - } - } - } - - if(s >= exp->_eol) - break; - } - if(p0 == p1 && p0 == nmaches) return good; - else if(nmaches >= p0 && p1 == 0xFFFF) return good; - else if(nmaches >= p0 && nmaches <= p1) return good; - return NULL; - } - case OP_OR: { - const TRexChar *asd = str; - TRexNode *temp=&exp->_nodes[node->left]; - while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) { - if(temp->next != -1) - temp = &exp->_nodes[temp->next]; - else - return asd; - } - asd = str; - temp = &exp->_nodes[node->right]; - while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) { - if(temp->next != -1) - temp = &exp->_nodes[temp->next]; - else - return asd; - } - return NULL; - break; - } - case OP_EXPR: - case OP_NOCAPEXPR:{ - TRexNode *n = &exp->_nodes[node->left]; - const TRexChar *cur = str; - int capture = -1; - if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) { - capture = exp->_currsubexp; - exp->_matches[capture].begin = cur; - exp->_currsubexp++; - } - - do { - TRexNode *subnext = NULL; - if(n->next != -1) { - subnext = &exp->_nodes[n->next]; - }else { - subnext = next; - } - if(!(cur = trex_matchnode(exp,n,cur,subnext))) { - if(capture != -1){ - exp->_matches[capture].begin = 0; - exp->_matches[capture].len = 0; - } - return NULL; - } - } while((n->next != -1) && (n = &exp->_nodes[n->next])); - - if(capture != -1) - exp->_matches[capture].len = cur - exp->_matches[capture].begin; - return cur; - } - case OP_WB: - if((str == exp->_bol && !isspace(*str)) - || (str == exp->_eol && !isspace(*(str-1))) - || (!isspace(*str) && isspace(*(str+1))) - || (isspace(*str) && !isspace(*(str+1))) ) { - return (node->left == 'b')?str:NULL; - } - return (node->left == 'b')?NULL:str; - case OP_BOL: - if(str == exp->_bol) return str; - return NULL; - case OP_EOL: - if(str == exp->_eol) return str; - return NULL; - case OP_DOT:{ - *str++; - } - return str; - case OP_NCLASS: - case OP_CLASS: - if(trex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?TRex_True:TRex_False):(type == OP_NCLASS?TRex_True:TRex_False)) { - *str++; - return str; - } - return NULL; - case OP_CCLASS: - if(trex_matchcclass(node->left,*str)) { - *str++; - return str; - } - return NULL; - default: /* char */ - if(*str != node->type) return NULL; - *str++; - return str; - } - return NULL; -} - -/* public api */ -TRex *trex_compile(const TRexChar *pattern,const TRexChar **error) -{ - TRex *exp = (TRex *)malloc(sizeof(TRex)); - exp->_eol = exp->_bol = NULL; - exp->_p = pattern; - exp->_nallocated = (int)scstrlen(pattern) * sizeof(TRexChar); - exp->_nodes = (TRexNode *)malloc(exp->_nallocated * sizeof(TRexNode)); - exp->_nsize = 0; - exp->_matches = 0; - exp->_nsubexpr = 0; - exp->_first = trex_newnode(exp,OP_EXPR); - exp->_error = error; - exp->_jmpbuf = malloc(sizeof(jmp_buf)); - if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) { - int res = trex_list(exp); - exp->_nodes[exp->_first].left = res; - if(*exp->_p!='\0') - trex_error(exp,_SC("unexpected character")); -#ifdef _DEBUG - { - int nsize,i; - TRexNode *t; - nsize = exp->_nsize; - t = &exp->_nodes[0]; - scprintf(_SC("\n")); - for(i = 0;i < nsize; i++) { - if(exp->_nodes[i].type>MAX_CHAR) - scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]); - else - scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type); - scprintf(_SC("left %02d right %02d next %02d\n"),exp->_nodes[i].left,exp->_nodes[i].right,exp->_nodes[i].next); - } - scprintf(_SC("\n")); - } -#endif - exp->_matches = (TRexMatch *) malloc(exp->_nsubexpr * sizeof(TRexMatch)); - memset(exp->_matches,0,exp->_nsubexpr * sizeof(TRexMatch)); - } - else{ - trex_free(exp); - return NULL; - } - return exp; -} - -void trex_free(TRex *exp) -{ - if(exp) { - if(exp->_nodes) free(exp->_nodes); - if(exp->_jmpbuf) free(exp->_jmpbuf); - if(exp->_matches) free(exp->_matches); - free(exp); - } -} - -TRexBool trex_match(TRex* exp,const TRexChar* text) -{ - const TRexChar* res = NULL; - exp->_bol = text; - exp->_eol = text + scstrlen(text); - exp->_currsubexp = 0; - res = trex_matchnode(exp,exp->_nodes,text,NULL); - if(res == NULL || res != exp->_eol) - return TRex_False; - return TRex_True; -} - -TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end) -{ - const TRexChar *cur = NULL; - int node = exp->_first; - if(text_begin >= text_end) return TRex_False; - exp->_bol = text_begin; - exp->_eol = text_end; - do { - cur = text_begin; - while(node != -1) { - exp->_currsubexp = 0; - cur = trex_matchnode(exp,&exp->_nodes[node],cur,NULL); - if(!cur) - break; - node = exp->_nodes[node].next; - } - *text_begin++; - } while(cur == NULL && text_begin != text_end); - - if(cur == NULL) - return TRex_False; - - --text_begin; - - if(out_begin) *out_begin = text_begin; - if(out_end) *out_end = cur; - return TRex_True; -} - -TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end) -{ - return trex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end); -} - -int trex_getsubexpcount(TRex* exp) -{ - return exp->_nsubexpr; -} - -TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp) -{ - if( n<0 || n >= exp->_nsubexpr) return TRex_False; - *subexp = exp->_matches[n]; - return TRex_True; -} - diff --git a/drivers/trex/trex.h b/drivers/trex/trex.h deleted file mode 100644 index 46e2e73fd50..00000000000 --- a/drivers/trex/trex.h +++ /dev/null @@ -1,70 +0,0 @@ -#ifndef _TREX_H_ -#define _TREX_H_ -/*************************************************************** - T-Rex a tiny regular expression library - - Copyright (C) 2003-2006 Alberto Demichelis - - This software is provided 'as-is', without any express - or implied warranty. In no event will the authors be held - liable for any damages arising from the use of this software. - - Permission is granted to anyone to use this software for - any purpose, including commercial applications, and to alter - it and redistribute it freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; - you must not claim that you wrote the original software. - If you use this software in a product, an acknowledgment - in the product documentation would be appreciated but - is not required. - - 2. Altered source versions must be plainly marked as such, - and must not be misrepresented as being the original software. - - 3. This notice may not be removed or altered from any - source distribution. - -****************************************************************/ - -#define _UNICODE - - -#ifdef _UNICODE -#define TRexChar wchar_t -#define MAX_CHAR 0xFFFF -#define _TREXC(c) L##c -#define trex_strlen wcslen -#define trex_printf wprintf -#else -#define TRexChar char -#define MAX_CHAR 0xFF -#define _TREXC(c) (c) -#define trex_strlen strlen -#define trex_printf printf -#endif - -#ifndef TREX_API -#define TREX_API extern -#endif - -#define TRex_True 1 -#define TRex_False 0 - -typedef unsigned int TRexBool; -typedef struct TRex TRex; - -typedef struct { - const TRexChar *begin; - int len; -} TRexMatch; - -TREX_API TRex *trex_compile(const TRexChar *pattern,const TRexChar **error); -TREX_API void trex_free(TRex *exp); -TREX_API TRexBool trex_match(TRex* exp,const TRexChar* text); -TREX_API TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end); -TREX_API TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end); -TREX_API int trex_getsubexpcount(TRex* exp); -TREX_API TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp); - -#endif From 732bfd0baff3fc17790c345f7e6b02e5c12a5080 Mon Sep 17 00:00:00 2001 From: Lee Zher Huei Date: Fri, 24 Jul 2015 02:02:52 +0100 Subject: [PATCH 05/14] Updated nrex fixes --- drivers/nrex/nrex.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/nrex/nrex.cpp b/drivers/nrex/nrex.cpp index 03e5334ed98..36a5bb4cedc 100644 --- a/drivers/nrex/nrex.cpp +++ b/drivers/nrex/nrex.cpp @@ -181,7 +181,7 @@ struct nrex_node { if (next) { - delete next; + NREX_DELETE(next); } } @@ -227,7 +227,7 @@ struct nrex_node_group : public nrex_node { for (unsigned int i = 0; i < childset.size(); ++i) { - delete childset[i]; + NREX_DELETE(childset[i]); } } @@ -471,7 +471,7 @@ struct nrex_node_quantifier : public nrex_node { if (child) { - delete child; + NREX_DELETE(child); } } @@ -598,7 +598,7 @@ nrex::~nrex() { if (_root) { - delete _root; + NREX_DELETE(_root); } } @@ -612,7 +612,7 @@ void nrex::reset() _capturing = 0; if (_root) { - delete _root; + NREX_DELETE(_root); } _root = NULL; } From d9f1a85948478fe5038ecc2fd1923c451cca84d5 Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Fri, 24 Jul 2015 13:25:04 +0100 Subject: [PATCH 06/14] More nrex fixes --- drivers/nrex/nrex.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/nrex/nrex.cpp b/drivers/nrex/nrex.cpp index 36a5bb4cedc..696d46240e7 100644 --- a/drivers/nrex/nrex.cpp +++ b/drivers/nrex/nrex.cpp @@ -246,7 +246,18 @@ struct nrex_node_group : public nrex_node { return res; } - if ((res >= 0) != negate) + if (negate) + { + if (res < 0) + { + res = pos + 1; + } + else + { + return -1; + } + } + if (res >= 0) { if (capturing >= 0) { @@ -479,7 +490,6 @@ struct nrex_node_quantifier : public nrex_node { nrex_array backtrack; backtrack.push(pos); - s->complete = false; while (backtrack.top() <= s->end) { if (max >= 1 && backtrack.size() > (unsigned int)max) @@ -502,7 +512,6 @@ struct nrex_node_quantifier : public nrex_node return res; } } - s->complete = false; int res = child->test(s, backtrack.top()); if (s->complete) { @@ -516,12 +525,7 @@ struct nrex_node_quantifier : public nrex_node } while (greedy && (unsigned int) min < backtrack.size()) { - s->complete = false; int res = backtrack.top(); - if (s->complete) - { - return res; - } if (next) { res = next->test(s, res); @@ -530,6 +534,10 @@ struct nrex_node_quantifier : public nrex_node { return res; } + if (s->complete) + { + return res; + } backtrack.pop(); } return -1; From 4ca0399ff6a16c7e5e760d57a7675df61f1f8ef0 Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Fri, 24 Jul 2015 13:55:18 +0100 Subject: [PATCH 07/14] Added a live-edit RegEx tester to the demos --- demos/misc/regex/engine.cfg | 4 ++++ demos/misc/regex/regex.gd | 22 ++++++++++++++++++++++ demos/misc/regex/regex.scn | Bin 0 -> 1770 bytes 3 files changed, 26 insertions(+) create mode 100644 demos/misc/regex/engine.cfg create mode 100644 demos/misc/regex/regex.gd create mode 100644 demos/misc/regex/regex.scn diff --git a/demos/misc/regex/engine.cfg b/demos/misc/regex/engine.cfg new file mode 100644 index 00000000000..0a6f4f869cf --- /dev/null +++ b/demos/misc/regex/engine.cfg @@ -0,0 +1,4 @@ +[application] + +name="RegEx" +main_scene="res://regex.scn" diff --git a/demos/misc/regex/regex.gd b/demos/misc/regex/regex.gd new file mode 100644 index 00000000000..a2dc62bf83d --- /dev/null +++ b/demos/misc/regex/regex.gd @@ -0,0 +1,22 @@ +extends VBoxContainer + +var regex = RegEx.new() + +func update_expression(): + regex.compile(get_node("Expression").get_text()) + update_text() + +func update_text(): + var text = get_node("Text").get_text() + regex.match(text) + var list = get_node("List") + for child in list.get_children(): + child.queue_free() + for res in regex.get_capture_list(): + var label = Label.new() + label.set_text(res) + list.add_child(label) + +func _ready(): + get_node("Text").set_text("They asked me \"What's going on \\\"in the manor\\\"?\"") + update_expression() diff --git a/demos/misc/regex/regex.scn b/demos/misc/regex/regex.scn new file mode 100644 index 0000000000000000000000000000000000000000..591b60974aaf5724ac456f43abb3770d56da8b59 GIT binary patch literal 1770 zcmWkvYitzP6+W}G>wWmO#xMNb>jfJ-ky#)VOHPB&?5tUgfV#FJX-Swn-o3k?vOBY# z8Jo4S!kPdMA^d2Y(gH5oghEo35QU0DYXvq0^+yCv8d2IH)Q&4K3N4{1sgi;kI=#}> zy`OX*pU!vBxdVfu2>)Mxfc{c|Pl^CIfE))LDMB6rN{F+hUyf~K@xd5V8F1*gRsiti zNhl+Hna&JtRFAQcDyJEENrhV)9#!Kh7N^ZcMod>SW++!gUj{b`!XT~Ypq%(fkFPrv z(pi#amnP%11$?B&nbKl%iY#<2hd+^01boMG7DyHHP*2=A2rlA=BEsXe!+;?1LY;ko zy=-$>%Zi$6%8JT#C?(a!vKkxF^pLJ3M@$;b``yEuX=-VxB*Dsr78^A}N>bHyCZ)Ae zgF%G_HN0VJnS2%0kSd>{OfV^tl9NV=Z03{++~!C@mF22)jxpU-$W^LMmA_vjf6F7K znJG&W6q8ks-fX6&mNyhlr46v2oa1}rib-c(X<1Jys^yr?`}4Cx5pS}rNu%DhoRv~c zO`0P#3XF)nJe*>3$YrpY&P*DDAq?Se^w1I9oqw9#p{G4Mi4|qs-OEsMjZxpWJd7SjdG*y*i+Nf&M zdT@}(3iZ6I#hK-(2WP$>FL=jfU6EDOfM!x9Jg{|k<7Vu!^>AZ3)?zU%v_&ZoF5^~2=mPv07ogi~sB%WiC|Q=G z9d^+AuukhaT_fvTz9vH)%g`JAk9~VrV-R_6LJJOGK`g&FB((W_Kc>#E?}$hNw^7H!ov^`ez~|4 z7;Uu8a)=8f+aZfP^3}PeSPkQrry3)WB9YeJpwsK&oi_55Rf`i|iK%0XuBmAjd>CGD zS1y3JF+h$wN3^7-E?#Hx&G?ohqZx9I#e?;|f^Cww{#&pcRj_R_^! zY+Ce!Qyxv3QtZXy!-b}(mzUGSO7M@lrq=V0l%~^K{_vr?)did3P5$al=TM6(r&A!4R-q4f7bcLO;$o($sL5OIJNVP~&h~DQmdt=bzaQD)>`alxhLXmV_%%l6;M;brh4Uo? zOJ+ExwhbVOeA%{6oU<9NjpW>Lj?G1%_XpeCd%B-}cIo7nt-!Yx=FZN@HPBX(hrRk=mDj!%atF={u}3z2E;vh9Q=eHU;WJ`vR{ zw}CsuCvm4$yTfurbh+(**4{_(JB5$1+~NoERiWHYx5(}(u+Q?fkERab0qnHLZ7p%G z%it}9f*WhYZxzZ5A?m?-pE)v`9#&-~1p!hGZ6r{%uYY5-)K`y<&eFcMkpWV}A&`3T zpvXTw;rjzA3Eb_A2t&{8xqDDpuxnXK@1S|C`d_}6|2+52bY@Y*)U|7Yb5~vBNZpZ( zH`5(+Jtvf7*MGC8b1>t6X1^z6%;C7i(}BOPhF?NLL(lG` zAAa_uW-Ia4Q~qq|#Ps#>Z*RYmc&T|EyFY$#V@Z8M} d{oBv(5)YI?U+>1qsW9CIFfceE4%olk{{Wo;Kb-&o literal 0 HcmV?d00001 From 87c1e16834a21ac1e42321811cb0efcda4b91b9a Mon Sep 17 00:00:00 2001 From: Zher Huei Lee Date: Fri, 24 Jul 2015 14:09:39 +0100 Subject: [PATCH 08/14] 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 7b512a5d993..93b1835b785 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;(1>#LW9O%IAu+Kr-!P-Ik0W*U`ucL&$L$m|8yOM*sV~~TngZ%^HXvcdF;Z6+> z?v587nq9gb%z+9W)jbb8WIGtU7e8PxcBpq+=TQGZy!rt~@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}gE>&4qq^sQhinI9_xuO!#SZmOs~qYdh*v*gXK**1oX)B! Date: Fri, 24 Jul 2015 16:15:04 +0100 Subject: [PATCH 09/14] Fixed incorrect failsafe return values --- drivers/nrex/regex.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/nrex/regex.cpp b/drivers/nrex/regex.cpp index 708e68cc265..0a813c34907 100644 --- a/drivers/nrex/regex.cpp +++ b/drivers/nrex/regex.cpp @@ -82,9 +82,9 @@ Error RegEx::compile(const String& p_pattern) { 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 ); + ERR_FAIL_COND_V( !exp.valid(), -1 ); + ERR_FAIL_COND_V( p_text.length() < p_start, -1 ); + ERR_FAIL_COND_V( p_text.length() < p_end, -1 ); bool res = exp.match(p_text.c_str(), &captures[0], p_start, p_end); From d50ebbb441cd6e1d2227d26cc168e76c7bff2b62 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Fri, 24 Jul 2015 14:18:02 -0300 Subject: [PATCH 10/14] -added icons for root node types on tabs --- scene/gui/tabs.cpp | 2 +- tools/editor/editor_data.cpp | 8 ++++++++ tools/editor/editor_data.h | 1 + tools/editor/editor_node.cpp | 16 +++++++++++++++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/scene/gui/tabs.cpp b/scene/gui/tabs.cpp index 40a6e20c374..9c59547cbab 100644 --- a/scene/gui/tabs.cpp +++ b/scene/gui/tabs.cpp @@ -142,7 +142,7 @@ void Tabs::_notification(int p_what) { Ref icon; if (tabs[i].icon.is_valid()) { - Ref icon = tabs[i].icon; + icon = tabs[i].icon; if (icon.is_valid()) { lsize+=icon->get_width(); if (s!="") diff --git a/tools/editor/editor_data.cpp b/tools/editor/editor_data.cpp index c4808d0cadd..87983d387c4 100644 --- a/tools/editor/editor_data.cpp +++ b/tools/editor/editor_data.cpp @@ -507,6 +507,14 @@ uint64_t EditorData::get_scene_version(int p_idx) const{ return edited_scene[p_idx].version; } +String EditorData::get_scene_type(int p_idx) const { + + ERR_FAIL_INDEX_V(p_idx,edited_scene.size(),String()); + if (!edited_scene[p_idx].root) + return ""; + return edited_scene[p_idx].root->get_type(); + +} String EditorData::get_scene_title(int p_idx) const { ERR_FAIL_INDEX_V(p_idx,edited_scene.size(),String()); if (!edited_scene[p_idx].root) diff --git a/tools/editor/editor_data.h b/tools/editor/editor_data.h index e3fdd52d015..52e86639f90 100644 --- a/tools/editor/editor_data.h +++ b/tools/editor/editor_data.h @@ -177,6 +177,7 @@ public: int get_edited_scene_count() const; String get_scene_title(int p_idx) const; String get_scene_path(int p_idx) const; + String get_scene_type(int p_idx) const; void set_edited_scene_version(uint64_t version); uint64_t get_edited_scene_version() const; uint64_t get_scene_version(int p_idx) const; diff --git a/tools/editor/editor_node.cpp b/tools/editor/editor_node.cpp index 7b44f2cad91..ae2eac29cc6 100644 --- a/tools/editor/editor_node.cpp +++ b/tools/editor/editor_node.cpp @@ -110,9 +110,23 @@ void EditorNode::_update_scene_tabs() { scene_tabs->clear_tabs(); for(int i=0;i icon; + if (type!=String()) { + + if (!gui_base->has_icon(type,"EditorIcons")) { + type="Node"; + } + + icon=gui_base->get_icon(type,"EditorIcons"); + + } + int current = editor_data.get_edited_scene(); bool unsaved = (i==current)?saved_version!=editor_data.get_undo_redo().get_version():editor_data.get_scene_version(i)!=0; - scene_tabs->add_tab(editor_data.get_scene_title(i)+(unsaved?"(*)":"")); + scene_tabs->add_tab(editor_data.get_scene_title(i)+(unsaved?"(*)":""),icon); + } scene_tabs->set_current_tab(editor_data.get_edited_scene()); From 07c99e11f5939699fefa10fab0b898ddc9246f85 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sun, 26 Jul 2015 10:44:10 -0300 Subject: [PATCH 11/14] QOL, script buttons in scene tabs --- scene/gui/tabs.cpp | 113 +++++++++++++++++- scene/gui/tabs.h | 7 ++ .../resources/default_theme/default_theme.cpp | 4 +- tools/editor/editor_data.cpp | 17 +++ tools/editor/editor_data.h | 1 + tools/editor/editor_node.cpp | 18 ++- tools/editor/editor_node.h | 1 + tools/editor/project_export.cpp | 2 +- 8 files changed, 158 insertions(+), 5 deletions(-) diff --git a/scene/gui/tabs.cpp b/scene/gui/tabs.cpp index 9c59547cbab..a849d3ae728 100644 --- a/scene/gui/tabs.cpp +++ b/scene/gui/tabs.cpp @@ -56,7 +56,14 @@ Size2 Tabs::get_minimum_size() const { else ms.width+=tab_bg->get_minimum_size().width; + if (tabs[i].right_button.is_valid()) { + Ref rb=tabs[i].right_button; + Size2 bms = rb->get_size()+get_stylebox("button")->get_minimum_size(); + bms.width+=get_constant("hseparation"); + ms.width+=bms.width; + ms.height=MAX(bms.height+tab_bg->get_minimum_size().height,ms.height); + } } return ms; @@ -66,6 +73,39 @@ Size2 Tabs::get_minimum_size() const { void Tabs::_input_event(const InputEvent& p_event) { + if (p_event.type==InputEvent::MOUSE_MOTION) { + + Point2 pos( p_event.mouse_motion.x, p_event.mouse_motion.y ); + + int hover=-1; + for(int i=0;i=tabs[i].ofs_cache && pos.x style = get_stylebox("button"); + Ref rb=tabs[i].right_button; + + lsize+=get_constant("hseparation"); + lsize+=style->get_margin(MARGIN_LEFT); + lsize+=rb->get_width(); + lsize+=style->get_margin(MARGIN_RIGHT); + + } Ref sb; int va; @@ -184,7 +243,37 @@ void Tabs::_notification(int p_what) { font->draw(ci, Point2i( w, sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-font->get_height())/2+font->get_ascent() ), s, col ); - w+=slen+sb->get_margin(MARGIN_RIGHT); + w+=slen; + + if (tabs[i].right_button.is_valid()) { + Ref style = get_stylebox("button"); + Ref rb=tabs[i].right_button; + + w+=get_constant("hseparation"); + + Rect2 rb_rect; + rb_rect.size=style->get_minimum_size()+rb->get_size(); + rb_rect.pos.x=w; + rb_rect.pos.y=sb->get_margin(MARGIN_TOP)+((sb_rect.size.y-sb_ms.y)-(rb_rect.size.y))/2; + + if (rb_hover==i) { + if (rb_pressing) + get_stylebox("button_pressed")->draw(ci,rb_rect); + else + style->draw(ci,rb_rect); + } + + w+=style->get_margin(MARGIN_LEFT); + + rb->draw(ci,Point2i( w,rb_rect.pos.y+style->get_margin(MARGIN_TOP) )); + w+=rb->get_width(); + w+=style->get_margin(MARGIN_RIGHT); + tabs[i].rb_rect=rb_rect; + + + } + + w+=sb->get_margin(MARGIN_RIGHT); tabs[i].size_cache=w-tabs[i].ofs_cache; @@ -252,6 +341,23 @@ Ref Tabs::get_tab_icon(int p_tab) const{ } + + +void Tabs::set_tab_right_button(int p_tab,const Ref& p_right_button){ + + ERR_FAIL_INDEX(p_tab,tabs.size()); + tabs[p_tab].right_button=p_right_button; + update(); + minimum_size_changed(); + +} +Ref Tabs::get_tab_right_button(int p_tab) const{ + + ERR_FAIL_INDEX_V(p_tab,tabs.size(),Ref()); + return tabs[p_tab].right_button; + +} + void Tabs::add_tab(const String& p_str,const Ref& p_icon) { Tab t; @@ -316,6 +422,7 @@ void Tabs::_bind_methods() { ObjectTypeDB::bind_method(_MD("get_tab_align"),&Tabs::get_tab_align); ADD_SIGNAL(MethodInfo("tab_changed",PropertyInfo(Variant::INT,"tab"))); + ADD_SIGNAL(MethodInfo("right_button_pressed",PropertyInfo(Variant::INT,"tab"))); ADD_PROPERTY( PropertyInfo(Variant::INT, "current_tab", PROPERTY_HINT_RANGE,"-1,4096,1",PROPERTY_USAGE_EDITOR), _SCS("set_current_tab"), _SCS("get_current_tab") ); @@ -328,5 +435,7 @@ Tabs::Tabs() { current=0; tab_align=ALIGN_CENTER; + rb_hover=-1; + rb_pressing=false; } diff --git a/scene/gui/tabs.h b/scene/gui/tabs.h index 8d4d0123f89..5cb0d9e9166 100644 --- a/scene/gui/tabs.h +++ b/scene/gui/tabs.h @@ -51,6 +51,8 @@ private: Ref icon; int ofs_cache; int size_cache; + Ref right_button; + Rect2 rb_rect; }; Vector tabs; @@ -58,6 +60,8 @@ private: Control *_get_tab(int idx) const; int _get_top_margin() const; TabAlign tab_align; + int rb_hover; + bool rb_pressing; protected: @@ -75,6 +79,9 @@ public: void set_tab_icon(int p_tab,const Ref& p_icon); Ref get_tab_icon(int p_tab) const; + void set_tab_right_button(int p_tab,const Ref& p_right_button); + Ref get_tab_right_button(int p_tab) const; + void set_tab_align(TabAlign p_align); TabAlign get_tab_align() const; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 9a9048e3e5f..cd0e67f04d9 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -250,7 +250,6 @@ void make_default_theme() { t->set_stylebox("hover","ToolButton", make_stylebox( button_normal_png,4,4,4,4) ); t->set_stylebox("disabled","ToolButton", make_empty_stylebox(4,4,4,4) ); t->set_stylebox("focus","ToolButton", focus ); - t->set_font("font","ToolButton", default_font ); t->set_color("font_color","ToolButton", control_font_color ); @@ -676,6 +675,9 @@ void make_default_theme() { t->set_stylebox("tab_fg","Tabs", make_stylebox( tab_current_png,4,4,4,4,16,4,16,4) ); t->set_stylebox("tab_bg","Tabs", make_stylebox( tab_behind_png,4,4,4,4,16,6,16,4) ); t->set_stylebox("panel","Tabs", make_stylebox( tab_container_bg_png,4,4,4,4) ); + t->set_stylebox("button_pressed","Tabs", make_stylebox( button_pressed_png,4,4,4,4) ); + t->set_stylebox("button","Tabs", make_stylebox( button_normal_png,4,4,4,4) ); + t->set_font("font","Tabs", default_font ); diff --git a/tools/editor/editor_data.cpp b/tools/editor/editor_data.cpp index 87983d387c4..f729a6c8696 100644 --- a/tools/editor/editor_data.cpp +++ b/tools/editor/editor_data.cpp @@ -515,6 +515,23 @@ String EditorData::get_scene_type(int p_idx) const { return edited_scene[p_idx].root->get_type(); } + +Ref