Merge pull request #2642 from leezh/nrex-update

Updated nrex with new features
This commit is contained in:
Juan Linietsky 2015-11-18 19:13:57 -03:00
commit 474822b6db
7 changed files with 696 additions and 201 deletions

View File

@ -2,21 +2,23 @@ extends VBoxContainer
var regex = RegEx.new() var regex = RegEx.new()
func update_expression(): func update_expression(text):
regex.compile(get_node("Expression").get_text()) regex.compile(text)
update_text() update_text()
func update_text(): func update_text():
var text = get_node("Text").get_text() var text = get_node("Text").get_text()
regex.find(text)
var list = get_node("List") var list = get_node("List")
for child in list.get_children(): for child in list.get_children():
child.queue_free() child.queue_free()
for res in regex.get_captures(): if regex.is_valid():
var label = Label.new() regex.find(text)
label.set_text(res) for res in regex.get_captures():
list.add_child(label) var label = Label.new()
label.set_text(res)
list.add_child(label)
func _ready(): func _ready():
get_node("Text").set_text("They asked me \"What's going on \\\"in the manor\\\"?\"") get_node("Text").set_text("They asked me \"What's going on \\\"in the manor\\\"?\"")
update_expression() update_expression(get_node("Expression").get_text())

Binary file not shown.

View File

@ -18,47 +18,42 @@ More details about its use is documented in `nrex.hpp`
Currently supported features: Currently supported features:
* Capturing `()` and non-capturing `(?:)` groups * Capturing `()` and non-capturing `(?:)` groups
* Any character `.` * Any character `.` (includes newlines)
* Shorthand caracter classes `\w\W\s\S\d\D` * Shorthand caracter classes `\w\W\s\S\d\D`
* User-defined character classes such as `[A-Za-z]` * POSIX character classes such as `[[:alnum:]]`
* Bracket expressions such as `[A-Za-z]`
* Simple quantifiers `?`, `*` and `+` * Simple quantifiers `?`, `*` and `+`
* Range quantifiers `{0,1}` * Range quantifiers `{0,1}`
* Lazy (non-greedy) quantifiers `*?` * Lazy (non-greedy) quantifiers `*?`
* Begining `^` and end `$` anchors * Begining `^` and end `$` anchors
* Word boundaries `\b`
* Alternation `|` * Alternation `|`
* Backreferences `\1` to `\99` * ASCII `\xFF` code points
To do list:
* Unicode `\uFFFF` code points * Unicode `\uFFFF` code points
* Positive `(?=)` and negative `(?!)` lookahead
* Positive `(?<=)` and negative `(?<!)` lookbehind (fixed length and no alternations)
* Backreferences `\1` to `\9` (with option to expand to `\99`)
## License ## License
Copyright (c) 2015, Zher Huei Lee Copyright (c) 2015, Zher Huei Lee
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without This software is provided 'as-is', without any express or implied
modification, are permitted provided that the following conditions are warranty. In no event will the authors be held liable for any damages
met: arising from the use of this software.
1. Redistributions of source code must retain the above copyright Permission is granted to anyone to use this software for any purpose,
notice, this list of conditions and the following disclaimer. including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
2. Redistributions in binary form must reproduce the above copyright 1. The origin of this software must not be misrepresented; you must not
notice, this list of conditions and the following disclaimer in the claim that you wrote the original software. If you use this software
documentation and/or other materials provided with the distribution. in a product, an acknowledgment in the product documentation would
be appreciated but is not required.
3. Neither the name of the copyright holder nor the names of its 2. Altered source versions must be plainly marked as such, and must not
contributors may be used to endorse or promote products derived from be misrepresented as being the original software.
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 3. This notice may not be removed or altered from any source
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED distribution.
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

File diff suppressed because it is too large Load Diff

View File

@ -79,7 +79,8 @@ class nrex
* This is used to provide the array size of the captures needed for * 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 * nrex::match() to work. The size is actually the number of capture
* groups + one for the matching of the entire pattern. The result is * groups + one for the matching of the entire pattern. The result is
* always capped at 100. * always capped at 10 or 100, depending on the extend option given in
* nrex::compile() (default 10).
* *
* \return The number of captures * \return The number of captures
*/ */
@ -95,10 +96,13 @@ class nrex
* runtime error nrex_compile_error if it encounters a problem when * runtime error nrex_compile_error if it encounters a problem when
* parsing the pattern. * parsing the pattern.
* *
* \param The regex pattern * \param pattern The regex pattern
* \param extended If true, raises the limit on number of capture
* groups and back-references to 99. Otherwise limited
* to 9. Defaults to false.
* \return True if the pattern was succesfully compiled * \return True if the pattern was succesfully compiled
*/ */
bool compile(const nrex_char* pattern); bool compile(const nrex_char* pattern, bool extended = false);
/*! /*!
* \brief Uses the pattern to search through the provided string * \brief Uses the pattern to search through the provided string

View File

@ -15,7 +15,7 @@
void RegEx::_bind_methods() { void RegEx::_bind_methods() {
ObjectTypeDB::bind_method(_MD("compile","pattern"),&RegEx::compile); ObjectTypeDB::bind_method(_MD("compile","pattern", "expanded"),&RegEx::compile, DEFVAL(true));
ObjectTypeDB::bind_method(_MD("find","text","start","end"),&RegEx::find, 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("clear"),&RegEx::clear);
ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid); ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid);
@ -55,6 +55,8 @@ bool RegEx::is_valid() const {
int RegEx::get_capture_count() const { int RegEx::get_capture_count() const {
ERR_FAIL_COND_V( !exp.valid(), 0 );
return exp.capture_size(); return exp.capture_size();
} }
@ -66,11 +68,11 @@ String RegEx::get_capture(int capture) const {
} }
Error RegEx::compile(const String& p_pattern) { Error RegEx::compile(const String& p_pattern, bool expanded) {
clear(); clear();
exp.compile(p_pattern.c_str()); exp.compile(p_pattern.c_str(), expanded);
ERR_FAIL_COND_V( !exp.valid(), FAILED ); ERR_FAIL_COND_V( !exp.valid(), FAILED );

View File

@ -36,7 +36,7 @@ public:
bool is_valid() const; bool is_valid() const;
int get_capture_count() const; int get_capture_count() const;
String get_capture(int capture) const; String get_capture(int capture) const;
Error compile(const String& p_pattern); Error compile(const String& p_pattern, bool expanded = false);
int find(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();