RegEx re-implemented as a module
Re-wrote nrex as a module using godot-specific parts and new features: * Added string substitutions. * Named groups are now supported. * Removed use of mutable variables in RegEx. RegExMatch is returned instead.
This commit is contained in:
parent
470ead74db
commit
439d439321
|
@ -31,7 +31,6 @@
|
|||
//#include "math_funcs.h"
|
||||
#include <stdio.h>
|
||||
#include "os/os.h"
|
||||
#include "drivers/nrex/regex.h"
|
||||
#include "core/io/ip_address.h"
|
||||
|
||||
#include "test_string.h"
|
||||
|
@ -462,18 +461,8 @@ bool test_25() {
|
|||
|
||||
bool test_26() {
|
||||
|
||||
OS::get_singleton()->print("\n\nTest 26: RegEx\n");
|
||||
RegEx regexp("(.*):(.*)");
|
||||
|
||||
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; i<regexp.get_capture_count(); i++)
|
||||
{
|
||||
printf("%ls\n", regexp.get_capture(i).c_str());
|
||||
}
|
||||
return (res>=0);
|
||||
//TODO: Do replacement RegEx test
|
||||
return true;
|
||||
};
|
||||
|
||||
struct test_27_data {
|
||||
|
|
|
@ -32514,6 +32514,7 @@
|
|||
would be read as [code]"(?:\\.|[^"])*"[/code]
|
||||
Currently supported features:
|
||||
* Capturing [code]()[/code] and non-capturing [code](?:)[/code] groups
|
||||
* Named capturing groups [code](?P<name>)[/code]
|
||||
* Any character [code].[/code]
|
||||
* Shorthand character classes [code]\w \W \s \S \d \D[/code]
|
||||
* User-defined character classes such as [code][A-Za-z][/code]
|
||||
|
@ -32522,7 +32523,7 @@
|
|||
* Lazy (non-greedy) quantifiers [code]*?[/code]
|
||||
* Beginning [code]^[/code] and end [code]$[/code] anchors
|
||||
* Alternation [code]|[/code]
|
||||
* Backreferences [code]\1[/code] and [code]\g{1}[/code]
|
||||
* Backreferences [code]\1[/code], [code]\g{1}[/code], and [code]\g<name>[/code]
|
||||
* POSIX character classes [code][[:alnum:]][/code]
|
||||
* Lookahead [code](?=)[/code], [code](?!)[/code] and lookbehind [code](?<=)[/code], [code](?<!)[/code]
|
||||
* ASCII [code]\xFF[/code] and Unicode [code]\uFFFF[/code] code points (in a style similar to Python)
|
||||
|
@ -32531,7 +32532,7 @@
|
|||
<methods>
|
||||
<method name="clear">
|
||||
<description>
|
||||
This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object, and forgets all captures made by the last [method find].
|
||||
This method resets the state of the object, as it was freshly created. Namely, it unassigns the regular expression of this object.
|
||||
</description>
|
||||
</method>
|
||||
<method name="compile">
|
||||
|
@ -32539,15 +32540,41 @@
|
|||
</return>
|
||||
<argument index="0" name="pattern" type="String">
|
||||
</argument>
|
||||
<argument index="1" name="capture" type="int" default="9">
|
||||
</argument>
|
||||
<description>
|
||||
Compiles and assign the regular expression pattern to use. The limit on the number of capturing groups can be specified or made unlimited if negative.
|
||||
Compiles and assign the regular expression pattern to use.
|
||||
</description>
|
||||
</method>
|
||||
<method name="find" qualifiers="const">
|
||||
<method name="get_group_count" qualifiers="const">
|
||||
<return type="int">
|
||||
</return>
|
||||
<description>
|
||||
Returns the number of numeric capturing groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_names" qualifiers="const">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<description>
|
||||
Returns an array of names of named capturing groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_pattern" qualifiers="const">
|
||||
<return type="String">
|
||||
</return>
|
||||
<description>
|
||||
Returns the expression used to compile the code.
|
||||
</description>
|
||||
</method>
|
||||
<method name="is_valid" qualifiers="const">
|
||||
<return type="bool">
|
||||
</return>
|
||||
<description>
|
||||
Returns whether this object has a valid regular expression assigned.
|
||||
</description>
|
||||
</method>
|
||||
<method name="search" qualifiers="const">
|
||||
<return type="Object">
|
||||
</return>
|
||||
<argument index="0" name="text" type="String">
|
||||
</argument>
|
||||
<argument index="1" name="start" type="int" default="0">
|
||||
|
@ -32555,45 +32582,96 @@
|
|||
<argument index="2" name="end" type="int" default="-1">
|
||||
</argument>
|
||||
<description>
|
||||
This method tries to find the pattern within the string, and returns the position where it was found. It also stores any capturing group (see [method get_capture]) for further retrieval.
|
||||
Searches the text for the compiled pattern. Returns a [RegExMatch] container of the first matching reult if found, otherwise null. The starting point of the serch could be specified without moving the string start anchor.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_capture" qualifiers="const">
|
||||
<method name="sub" qualifiers="const">
|
||||
<return type="String">
|
||||
</return>
|
||||
<argument index="0" name="capture" type="int">
|
||||
<argument index="0" name="text" type="String">
|
||||
</argument>
|
||||
<argument index="1" name="template" type="String">
|
||||
</argument>
|
||||
<argument index="2" name="start" type="int" default="0">
|
||||
</argument>
|
||||
<argument index="3" name="end" type="int" default="-1">
|
||||
</argument>
|
||||
<description>
|
||||
Returns a captured group. A captured group is the part of a string that matches a part of the pattern delimited by parentheses (unless they are non-capturing parentheses [i](?:)[/i]).
|
||||
Searches the specified text for the compiled pattern and returns the text with the result replaced. Escapes and backreferences such as [code]\1[/code] and [code]\g<name>[/code] are automatically expanded and resolved. If no change was found the unmodified text is returned instead.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_capture_count" qualifiers="const">
|
||||
</methods>
|
||||
<constants>
|
||||
</constants>
|
||||
</class>
|
||||
<class name="RegExMatch" inherits="Reference" category="Core">
|
||||
<brief_description>
|
||||
</brief_description>
|
||||
<description>
|
||||
</description>
|
||||
<methods>
|
||||
<method name="expand" qualifiers="const">
|
||||
<return type="String">
|
||||
</return>
|
||||
<argument index="0" name="template" type="String">
|
||||
</argument>
|
||||
<description>
|
||||
Using results from the search, returns the specified string with escapes and backreferences such as [code]\1[/code] and [code]\g<name>[/code] expanded and resolved
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_end" qualifiers="const">
|
||||
<return type="int">
|
||||
</return>
|
||||
<argument index="0" name="name" type="Variant" default="0">
|
||||
</argument>
|
||||
<description>
|
||||
Returns the end position of the match in the string. An interger can be specified for numeric groups or a string for named groups. Returns -1 if that group wasn't found or doesn't exist. Defaults to 0 (whole pattern).
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_group_array" qualifiers="const">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<description>
|
||||
Returns an array of the results of the numeric groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_group_count" qualifiers="const">
|
||||
<return type="int">
|
||||
</return>
|
||||
<description>
|
||||
Returns the number of capturing groups. A captured group is the part of a string that matches a part of the pattern delimited by parentheses (unless they are non-capturing parentheses [i](?:)[/i]).
|
||||
Returns the number of numeric capturing groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_capture_start" qualifiers="const">
|
||||
<method name="get_name_dict" qualifiers="const">
|
||||
<return type="Dictionary">
|
||||
</return>
|
||||
<description>
|
||||
Returns a dictionary containing the named capturing groups and their results.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_names" qualifiers="const">
|
||||
<return type="Array">
|
||||
</return>
|
||||
<description>
|
||||
Returns an array of names of named capturing groups.
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_start" qualifiers="const">
|
||||
<return type="int">
|
||||
</return>
|
||||
<argument index="0" name="capture" type="int">
|
||||
<argument index="0" name="name" type="Variant" default="0">
|
||||
</argument>
|
||||
<description>
|
||||
Returns the starting position of the match in the string. An interger can be specified for numeric groups or a string for named groups. Returns -1 if that group wasn't found or doesn't exist. Defaults to 0 (whole pattern).
|
||||
</description>
|
||||
</method>
|
||||
<method name="get_captures" qualifiers="const">
|
||||
<return type="StringArray">
|
||||
<method name="get_string" qualifiers="const">
|
||||
<return type="String">
|
||||
</return>
|
||||
<argument index="0" name="name" type="Variant" default="0">
|
||||
</argument>
|
||||
<description>
|
||||
Return a list of all the captures made by the regular expression.
|
||||
</description>
|
||||
</method>
|
||||
<method name="is_valid" qualifiers="const">
|
||||
<return type="bool">
|
||||
</return>
|
||||
<description>
|
||||
Returns whether this object has a valid regular expression assigned.
|
||||
Returns the result of the match in the string. An interger can be specified for numeric groups or a string for named groups. Returns -1 if that group wasn't found or doesn't exist. Defaults to 0 (whole pattern).
|
||||
</description>
|
||||
</method>
|
||||
</methods>
|
||||
|
|
|
@ -25,7 +25,6 @@ SConscript('gl_context/SCsub');
|
|||
|
||||
# Core dependencies
|
||||
SConscript("png/SCsub");
|
||||
SConscript("nrex/SCsub");
|
||||
|
||||
# Tools override
|
||||
# FIXME: Should likely be integrated in the tools/ codebase
|
||||
|
|
|
@ -1,75 +0,0 @@
|
|||
# NREX: Node RegEx
|
||||
|
||||
[![Build Status](https://travis-ci.org/leezh/nrex.svg?branch=master)](https://travis-ci.org/leezh/nrex)
|
||||
|
||||
** Version 0.2 **
|
||||
|
||||
Small node-based regular expression library. It only does text pattern
|
||||
matchhing, not replacement. To use add the files `nrex.hpp`, `nrex.cpp`
|
||||
and `nrex_config.h` to your project and follow the example:
|
||||
|
||||
nrex regex;
|
||||
regex.compile("^(fo+)bar$");
|
||||
|
||||
nrex_result captures[regex.capture_size()];
|
||||
if (regex.match("foobar", captures))
|
||||
{
|
||||
std::cout << captures[0].start << std::endl;
|
||||
std::cout << captures[0].length << std::endl;
|
||||
}
|
||||
|
||||
More details about its use is documented in `nrex.hpp`
|
||||
|
||||
Currently supported features:
|
||||
* Capturing `()` and non-capturing `(?:)` groups
|
||||
* Any character `.` (includes newlines)
|
||||
* Shorthand caracter classes `\w\W\s\S\d\D`
|
||||
* 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 `|`
|
||||
* ASCII `\xFF` code points
|
||||
* Unicode `\uFFFF` code points
|
||||
* Positive `(?=)` and negative `(?!)` lookahead
|
||||
* Positive `(?<=)` and negative `(?<!)` lookbehind (fixed length and no alternations)
|
||||
* Backreferences `\1` and `\g{1}` (limited by default to 9 - can be unlimited)
|
||||
|
||||
## License
|
||||
|
||||
Copyright (c) 2015-2016, 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.
|
||||
|
||||
|
||||
# Changes
|
||||
|
||||
## Version 0.2 (2016-08-04)
|
||||
* Fixed capturing groups matching to invalid results
|
||||
* Fixed parents of recursive quantifiers not expanding properly
|
||||
* Fixed LookAhead sometimes adding to result
|
||||
* More verbose unit testing
|
||||
|
||||
## Version 0.1 (2015-12-04)
|
||||
* Initial release
|
File diff suppressed because it is too large
Load Diff
|
@ -1,176 +0,0 @@
|
|||
// NREX: Node RegEx
|
||||
// Version 0.2
|
||||
//
|
||||
// Copyright (c) 2015-2016, 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:
|
||||
unsigned int _capturing;
|
||||
unsigned int _lookahead_depth;
|
||||
nrex_node* _root;
|
||||
public:
|
||||
|
||||
/*!
|
||||
* \brief Initialises an empty regex container
|
||||
*/
|
||||
nrex();
|
||||
|
||||
/*!
|
||||
* \brief Initialises and compiles the regex pattern
|
||||
*
|
||||
* This calls nrex::compile() with the same arguments. To check whether
|
||||
* the compilation was successfull, use nrex::valid().
|
||||
*
|
||||
* 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 pattern The regex pattern
|
||||
* \param captures The maximum number of capture groups to allow. Any
|
||||
* extra would be converted to non-capturing groups.
|
||||
* If negative, no limit would be imposed. Defaults
|
||||
* to 9.
|
||||
*
|
||||
* \see nrex::compile()
|
||||
*/
|
||||
nrex(const nrex_char* pattern, int captures = 9);
|
||||
|
||||
~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. This can be
|
||||
* capped using the extra argument given in nrex::compile()
|
||||
* (default 10).
|
||||
*
|
||||
* \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 pattern The regex pattern
|
||||
* \param captures The maximum number of capture groups to allow. Any
|
||||
* extra would be converted to non-capturing groups.
|
||||
* If negative, no limit would be imposed. Defaults
|
||||
* to 9.
|
||||
* \return True if the pattern was succesfully compiled
|
||||
*/
|
||||
bool compile(const nrex_char* pattern, int captures = 9);
|
||||
|
||||
/*!
|
||||
* \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 <stdexcept>
|
||||
|
||||
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
|
|
@ -1,12 +0,0 @@
|
|||
// 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)
|
|
@ -1,142 +0,0 @@
|
|||
/*************************************************************************/
|
||||
/* regex.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* http://www.godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
#include "regex.h"
|
||||
#include "nrex.hpp"
|
||||
#include "core/os/memory.h"
|
||||
|
||||
void RegEx::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("compile","pattern", "capture"),&RegEx::compile, DEFVAL(9));
|
||||
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_start","capture"),&RegEx::get_capture_start);
|
||||
ObjectTypeDB::bind_method(_MD("get_captures"),&RegEx::_bind_get_captures);
|
||||
|
||||
};
|
||||
|
||||
StringArray RegEx::_bind_get_captures() const {
|
||||
|
||||
StringArray ret;
|
||||
int count = get_capture_count();
|
||||
for (int i=0; i<count; i++) {
|
||||
|
||||
String c = get_capture(i);
|
||||
ret.push_back(c);
|
||||
};
|
||||
|
||||
return ret;
|
||||
|
||||
};
|
||||
|
||||
void RegEx::clear() {
|
||||
|
||||
text.clear();
|
||||
captures.clear();
|
||||
exp.reset();
|
||||
|
||||
};
|
||||
|
||||
bool RegEx::is_valid() const {
|
||||
|
||||
return exp.valid();
|
||||
|
||||
};
|
||||
|
||||
int RegEx::get_capture_count() const {
|
||||
|
||||
ERR_FAIL_COND_V( !exp.valid(), 0 );
|
||||
|
||||
return exp.capture_size();
|
||||
}
|
||||
|
||||
String RegEx::get_capture(int capture) const {
|
||||
|
||||
ERR_FAIL_COND_V( get_capture_count() <= capture, String() );
|
||||
|
||||
return text.substr(captures[capture].start, captures[capture].length);
|
||||
|
||||
}
|
||||
|
||||
int RegEx::get_capture_start(int capture) const {
|
||||
|
||||
ERR_FAIL_COND_V( get_capture_count() <= capture, -1 );
|
||||
|
||||
return captures[capture].start;
|
||||
|
||||
}
|
||||
|
||||
Error RegEx::compile(const String& p_pattern, int capture) {
|
||||
|
||||
clear();
|
||||
|
||||
exp.compile(p_pattern.c_str(), capture);
|
||||
|
||||
ERR_FAIL_COND_V( !exp.valid(), FAILED );
|
||||
|
||||
captures.resize(exp.capture_size());
|
||||
|
||||
return OK;
|
||||
|
||||
};
|
||||
|
||||
int RegEx::find(const String& p_text, int p_start, int p_end) const {
|
||||
|
||||
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);
|
||||
|
||||
if (res) {
|
||||
text = p_text;
|
||||
return captures[0].start;
|
||||
}
|
||||
text.clear();
|
||||
return -1;
|
||||
|
||||
};
|
||||
|
||||
RegEx::RegEx(const String& p_pattern) {
|
||||
|
||||
compile(p_pattern);
|
||||
|
||||
};
|
||||
|
||||
RegEx::RegEx() {
|
||||
|
||||
};
|
||||
|
||||
RegEx::~RegEx() {
|
||||
|
||||
clear();
|
||||
|
||||
};
|
|
@ -40,8 +40,6 @@
|
|||
#include "platform/windows/export/export.h"
|
||||
#endif
|
||||
|
||||
#include "drivers/nrex/regex.h"
|
||||
|
||||
static ImageLoaderPNG *image_loader_png=NULL;
|
||||
static ResourceSaverPNG *resource_saver_png=NULL;
|
||||
|
||||
|
@ -53,8 +51,6 @@ void register_core_driver_types() {
|
|||
|
||||
resource_saver_png = memnew( ResourceSaverPNG );
|
||||
ResourceSaver::add_resource_format_saver(resource_saver_png);
|
||||
|
||||
ObjectTypeDB::register_type<RegEx>();
|
||||
}
|
||||
|
||||
void unregister_core_driver_types() {
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
|
||||
Import('env')
|
||||
|
||||
env.add_source_files(env.drivers_sources, "*.cpp")
|
||||
env.add_source_files(env.modules_sources, "*.cpp")
|
||||
|
||||
Export('env')
|
|
@ -0,0 +1,8 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
def can_build(platform):
|
||||
return True
|
||||
|
||||
def configure(env):
|
||||
pass
|
||||
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,114 @@
|
|||
/*************************************************************************/
|
||||
/* regex.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* http://www.godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
#ifndef REGEX_H
|
||||
#define REGEX_H
|
||||
|
||||
#include "core/vector.h"
|
||||
#include "core/ustring.h"
|
||||
#include "core/dictionary.h"
|
||||
#include "core/reference.h"
|
||||
#include "core/resource.h"
|
||||
|
||||
class RegExNode;
|
||||
|
||||
class RegExMatch : public Reference {
|
||||
|
||||
OBJ_TYPE(RegExMatch, Reference);
|
||||
|
||||
struct Group {
|
||||
Variant name;
|
||||
int start;
|
||||
int length;
|
||||
};
|
||||
|
||||
Vector<Group> captures;
|
||||
String string;
|
||||
|
||||
friend class RegEx;
|
||||
friend class RegExSearch;
|
||||
friend class RegExNodeCapturing;
|
||||
friend class RegExNodeBackReference;
|
||||
|
||||
protected:
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
|
||||
String expand(const String& p_template) const;
|
||||
|
||||
int get_group_count() const;
|
||||
Array get_group_array() const;
|
||||
|
||||
Array get_names() const;
|
||||
Dictionary get_name_dict() const;
|
||||
|
||||
String get_string(const Variant& p_name) const;
|
||||
int get_start(const Variant& p_name) const;
|
||||
int get_end(const Variant& p_name) const;
|
||||
|
||||
RegExMatch();
|
||||
|
||||
};
|
||||
|
||||
class RegEx : public Reference {
|
||||
|
||||
OBJ_TYPE(RegEx, Reference);
|
||||
|
||||
RegExNode* root;
|
||||
Vector<Variant> group_names;
|
||||
String pattern;
|
||||
int lookahead_depth;
|
||||
|
||||
protected:
|
||||
|
||||
static void _bind_methods();
|
||||
|
||||
public:
|
||||
|
||||
void clear();
|
||||
Error compile(const String& p_pattern);
|
||||
|
||||
Ref<RegExMatch> search(const String& p_text, int p_start = 0, int p_end = -1) const;
|
||||
String sub(const String& p_text, const String& p_template, int p_start = 0, int p_end = -1) const;
|
||||
|
||||
bool is_valid() const;
|
||||
String get_pattern() const;
|
||||
int get_group_count() const;
|
||||
Array get_names() const;
|
||||
|
||||
RegEx();
|
||||
RegEx(const String& p_pattern);
|
||||
~RegEx();
|
||||
|
||||
};
|
||||
|
||||
#endif // REGEX_H
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
/*************************************************************************/
|
||||
/* regex.h */
|
||||
/* register_types.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
|
@ -26,40 +26,18 @@
|
|||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
#ifndef REGEX_H
|
||||
#define REGEX_H
|
||||
|
||||
#include "ustring.h"
|
||||
#include "vector.h"
|
||||
#include "core/reference.h"
|
||||
#include "nrex.hpp"
|
||||
#include "register_types.h"
|
||||
#include "object_type_db.h"
|
||||
#include "regex.h"
|
||||
|
||||
class RegEx : public Reference {
|
||||
void register_regex_types() {
|
||||
|
||||
OBJ_TYPE(RegEx, Reference);
|
||||
ObjectTypeDB::register_type<RegExMatch>();
|
||||
ObjectTypeDB::register_type<RegEx>();
|
||||
}
|
||||
|
||||
mutable String text;
|
||||
mutable Vector<nrex_result> captures;
|
||||
nrex exp;
|
||||
void unregister_regex_types() {
|
||||
|
||||
protected:
|
||||
}
|
||||
|
||||
static void _bind_methods();
|
||||
StringArray _bind_get_captures() const;
|
||||
|
||||
public:
|
||||
|
||||
void clear();
|
||||
bool is_valid() const;
|
||||
int get_capture_count() const;
|
||||
int get_capture_start(int capture) const;
|
||||
String get_capture(int capture) const;
|
||||
Error compile(const String& p_pattern, int capture = 9);
|
||||
int find(const String& p_text, int p_start = 0, int p_end = -1) const;
|
||||
|
||||
RegEx();
|
||||
RegEx(const String& p_pattern);
|
||||
~RegEx();
|
||||
};
|
||||
|
||||
#endif // REGEX_H
|
|
@ -0,0 +1,31 @@
|
|||
/*************************************************************************/
|
||||
/* register_types.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* http://www.godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/*************************************************************************/
|
||||
|
||||
void register_regex_types();
|
||||
void unregister_regex_types();
|
Loading…
Reference in New Issue