Quellcode durchsuchen

Merge pull request #2642 from leezh/nrex-update

Updated nrex with new features
Juan Linietsky vor 9 Jahren
Ursprung
Commit
474822b6db

+ 10 - 8
demos/misc/regex/regex.gd

@@ -2,21 +2,23 @@ extends VBoxContainer
 
 var regex = RegEx.new()
 
-func update_expression():
-	regex.compile(get_node("Expression").get_text())
+func update_expression(text):
+	regex.compile(text)
 	update_text()
 
 func update_text():
 	var text = get_node("Text").get_text()
-	regex.find(text)
 	var list = get_node("List")
 	for child in list.get_children():
 		child.queue_free()
-	for res in regex.get_captures():
-		var label = Label.new()
-		label.set_text(res)
-		list.add_child(label)
+	if regex.is_valid():
+		regex.find(text)
+		for res in regex.get_captures():
+			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()
+	update_expression(get_node("Expression").get_text())
+

BIN
demos/misc/regex/regex.scn


+ 26 - 31
drivers/nrex/README.md

@@ -18,47 +18,42 @@ More details about its use is documented in `nrex.hpp`
 
 Currently supported features:
  * Capturing `()` and non-capturing `(?:)` groups
- * Any character `.`
+ * Any character `.` (includes newlines)
  * 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 `+`
  * Range quantifiers `{0,1}`
  * Lazy (non-greedy) quantifiers `*?`
  * Begining `^` and end `$` anchors
+ * Word boundaries `\b`
  * Alternation `|`
- * Backreferences `\1` to `\99`
-
-To do list:
+ * ASCII `\xFF` 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
 
 Copyright (c) 2015, Zher Huei Lee
 All rights reserved.
 
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are
-met:
-
- 1. Redistributions of source code must retain the above copyright
-    notice, this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer in the
-    documentation and/or other materials provided with the distribution.
-
- 3. Neither the name of the copyright holder nor the names of its
-    contributors may be used to endorse or promote products derived from
-    this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
-IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
-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.
+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.

Datei-Diff unterdrückt, da er zu groß ist
+ 618 - 140
drivers/nrex/nrex.cpp


+ 7 - 3
drivers/nrex/nrex.hpp

@@ -79,7 +79,8 @@ class nrex
          * 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.
+         * always capped at 10 or 100, depending on the extend option given in
+         * nrex::compile() (default 10).
          *
          * \return The number of captures
          */
@@ -95,10 +96,13 @@ class nrex
          * runtime error nrex_compile_error if it encounters a problem when
          * 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
          */
-        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

+ 6 - 4
drivers/nrex/regex.cpp

@@ -15,7 +15,7 @@
 
 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("clear"),&RegEx::clear);
 	ObjectTypeDB::bind_method(_MD("is_valid"),&RegEx::is_valid);
@@ -54,7 +54,9 @@ bool RegEx::is_valid() const {
 };
 
 int RegEx::get_capture_count() const {
-	
+
+	ERR_FAIL_COND_V( !exp.valid(), 0 );
+
 	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();
 
-	exp.compile(p_pattern.c_str());
+	exp.compile(p_pattern.c_str(), expanded);
 
 	ERR_FAIL_COND_V( !exp.valid(), FAILED );
 

+ 1 - 1
drivers/nrex/regex.h

@@ -36,7 +36,7 @@ public:
 	bool is_valid() const;
 	int get_capture_count() 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;
 
 	RegEx();

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.