mirror of
https://github.com/godotengine/godot.git
synced 2026-03-10 21:06:13 +00:00
Regex library Nrex initial port
This commit is contained in:
@@ -31,7 +31,7 @@
|
||||
//#include "math_funcs.h"
|
||||
#include <stdio.h>
|
||||
#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<String> 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<String>::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<regexp.get_capture_count(); i++)
|
||||
{
|
||||
printf("%ls\n", regexp.get_capture(i).c_str());
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
struct test_27_data {
|
||||
|
||||
@@ -29,7 +29,7 @@ if (env["openssl"]=="builtin"):
|
||||
|
||||
SConscript("rtaudio/SCsub");
|
||||
SConscript("nedmalloc/SCsub");
|
||||
SConscript("trex/SCsub");
|
||||
SConscript("nrex/SCsub");
|
||||
SConscript("chibi/SCsub");
|
||||
if (env["vorbis"]=="yes" or env["speex"]=="yes" or env["theora"]=="yes"):
|
||||
SConscript("ogg/SCsub");
|
||||
|
||||
64
drivers/nrex/README.md
Normal file
64
drivers/nrex/README.md
Normal file
@@ -0,0 +1,64 @@
|
||||
# NREX: Node RegEx
|
||||
|
||||
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 `.`
|
||||
* Shorthand caracter classes `\w\W\s\S\d\D`
|
||||
* User-defined character classes such as `[A-Za-z]`
|
||||
* Simple quantifiers `?`, `*` and `+`
|
||||
* Range quantifiers `{0,1}`
|
||||
* Lazy (non-greedy) quantifiers `*?`
|
||||
* Begining `^` and end `$` anchors
|
||||
* Alternation `|`
|
||||
* Backreferences `\1` to `\99`
|
||||
|
||||
To do list:
|
||||
* Unicode `\uFFFF` code points
|
||||
|
||||
## 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.
|
||||
@@ -2,8 +2,7 @@
|
||||
Import('env')
|
||||
|
||||
sources = [
|
||||
|
||||
'trex.c',
|
||||
'nrex.cpp',
|
||||
'regex.cpp',
|
||||
]
|
||||
env.add_source_files(env.drivers_sources, sources)
|
||||
902
drivers/nrex/nrex.cpp
Normal file
902
drivers/nrex/nrex.cpp
Normal file
File diff suppressed because it is too large
Load Diff
144
drivers/nrex/nrex.hpp
Normal file
144
drivers/nrex/nrex.hpp
Normal file
@@ -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 <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
|
||||
12
drivers/nrex/nrex_config.h
Normal file
12
drivers/nrex/nrex_config.h
Normal file
@@ -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)
|
||||
112
drivers/nrex/regex.cpp
Normal file
112
drivers/nrex/regex.cpp
Normal file
@@ -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<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 {
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
Error RegEx::compile(const String& p_pattern) {
|
||||
|
||||
clear();
|
||||
|
||||
exp.compile(p_pattern.c_str());
|
||||
|
||||
ERR_FAIL_COND_V( !exp.valid(), FAILED );
|
||||
|
||||
captures.resize(exp.capture_size());
|
||||
|
||||
return OK;
|
||||
|
||||
};
|
||||
|
||||
bool RegEx::match(const String& p_text, int p_start, int p_end) const {
|
||||
|
||||
|
||||
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 );
|
||||
|
||||
bool res = exp.match(p_text.c_str(), &captures[0], p_start, p_end);
|
||||
|
||||
if (res) {
|
||||
text = p_text;
|
||||
return true;
|
||||
}
|
||||
text.clear();
|
||||
return false;
|
||||
|
||||
};
|
||||
|
||||
RegEx::RegEx(const String& p_pattern) {
|
||||
|
||||
compile(p_pattern);
|
||||
|
||||
};
|
||||
|
||||
RegEx::RegEx() {
|
||||
|
||||
};
|
||||
|
||||
RegEx::~RegEx() {
|
||||
|
||||
clear();
|
||||
|
||||
};
|
||||
@@ -13,34 +13,31 @@
|
||||
#define REGEX_H
|
||||
|
||||
#include "ustring.h"
|
||||
#include "list.h"
|
||||
#include "vector.h"
|
||||
#include "core/reference.h"
|
||||
struct TRex;
|
||||
#include "nrex.hpp"
|
||||
|
||||
class RegEx : public Reference {
|
||||
|
||||
OBJ_TYPE(RegEx, Reference);
|
||||
|
||||
mutable String text;
|
||||
TRex *exp;
|
||||
mutable Vector<nrex_result> 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<String>* 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<String>* 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);
|
||||
@@ -48,7 +48,7 @@
|
||||
#endif
|
||||
|
||||
|
||||
#include "drivers/trex/regex.h"
|
||||
#include "drivers/nrex/regex.h"
|
||||
|
||||
#ifdef MUSEPACK_ENABLED
|
||||
#include "mpc/audio_stream_mpc.h"
|
||||
|
||||
@@ -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_
|
||||
@@ -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
|
||||
@@ -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.
|
||||
|
||||
@@ -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<String>* 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; i<count; i++) {
|
||||
|
||||
int start, len;
|
||||
get_capture_limits(i, start, len);
|
||||
p_captures->push_back(p_text.substr(start, len));
|
||||
};
|
||||
};
|
||||
} else {
|
||||
|
||||
p_rstart = -1;
|
||||
};
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
|
||||
bool RegEx::match(const String& p_text, List<String>* 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<count; i++) {
|
||||
|
||||
String c = get_capture(i);
|
||||
ret.push_back(c);
|
||||
};
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
bool RegEx::is_valid() const {
|
||||
|
||||
return exp != NULL;
|
||||
};
|
||||
|
||||
void RegEx::clear() {
|
||||
|
||||
if (exp) {
|
||||
|
||||
trex_free(exp);
|
||||
exp = NULL;
|
||||
};
|
||||
};
|
||||
|
||||
RegEx::RegEx(const String& p_pattern) {
|
||||
|
||||
exp = NULL;
|
||||
compile(p_pattern);
|
||||
};
|
||||
|
||||
RegEx::RegEx() {
|
||||
|
||||
exp = NULL;
|
||||
};
|
||||
|
||||
RegEx::~RegEx() {
|
||||
|
||||
clear();
|
||||
};
|
||||
@@ -1,41 +0,0 @@
|
||||
#include "trex.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -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
|
||||
Reference in New Issue
Block a user