mirror of
https://foundry.openuru.org/gitblit/r/CWE-ou-minkata.git
synced 2025-07-18 11:19:10 +00:00
Merge pull request #355 from Deledrius/c11_regex
Use std::regex instead of PCRE.
This commit is contained in:
@ -45,7 +45,6 @@ find_package(Ogg REQUIRED) #TODO: Not required if we aren't building the clie
|
|||||||
find_package(Vorbis REQUIRED) #TODO: Not required if we aren't building the client
|
find_package(Vorbis REQUIRED) #TODO: Not required if we aren't building the client
|
||||||
find_package(Speex REQUIRED) #TODO: Not required if we aren't building the client
|
find_package(Speex REQUIRED) #TODO: Not required if we aren't building the client
|
||||||
find_package(CURL REQUIRED)
|
find_package(CURL REQUIRED)
|
||||||
find_package(PCRE REQUIRED)
|
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
find_package(PhysX REQUIRED) #TODO: Not required if we aren't building the client
|
find_package(PhysX REQUIRED) #TODO: Not required if we aren't building the client
|
||||||
@ -64,12 +63,6 @@ if(CURL_IS_STATIC)
|
|||||||
add_definitions(-DCURL_STATICLIB)
|
add_definitions(-DCURL_STATICLIB)
|
||||||
endif(CURL_IS_STATIC)
|
endif(CURL_IS_STATIC)
|
||||||
|
|
||||||
#libpcre isn't smart enough to detect this either
|
|
||||||
option(PCRE_IS_STATIC "Using the static version of libpcre?" ON)
|
|
||||||
if(PCRE_IS_STATIC)
|
|
||||||
add_definitions(-DPCRE_STATIC)
|
|
||||||
endif(PCRE_IS_STATIC)
|
|
||||||
|
|
||||||
option(PLASMA_EXTERNAL_RELEASE "Is this release intended for the general public?" OFF)
|
option(PLASMA_EXTERNAL_RELEASE "Is this release intended for the general public?" OFF)
|
||||||
if(PLASMA_EXTERNAL_RELEASE)
|
if(PLASMA_EXTERNAL_RELEASE)
|
||||||
add_definitions(-DPLASMA_EXTERNAL_RELEASE)
|
add_definitions(-DPLASMA_EXTERNAL_RELEASE)
|
||||||
|
@ -29,7 +29,6 @@ Plasma currently requires the following third-party libraries:
|
|||||||
- libPNG - http://www.libpng.org/
|
- libPNG - http://www.libpng.org/
|
||||||
- speex - http://www.speex.org/downloads/
|
- speex - http://www.speex.org/downloads/
|
||||||
- zlib - http://zlib.net/
|
- zlib - http://zlib.net/
|
||||||
- PCRE - http://www.pcre.org/
|
|
||||||
- libcurl - http://curl.haxx.se/
|
- libcurl - http://curl.haxx.se/
|
||||||
|
|
||||||
The following libraries are optional:
|
The following libraries are optional:
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
include_directories(${PCRE_INCLUDE_DIR})
|
|
||||||
|
|
||||||
add_definitions(-D_LIB)
|
add_definitions(-D_LIB)
|
||||||
|
|
||||||
add_definitions(-DPRODUCT_BRANCH_ID=${PRODUCT_BRANCH_ID})
|
add_definitions(-DPRODUCT_BRANCH_ID=${PRODUCT_BRANCH_ID})
|
||||||
@ -111,7 +109,6 @@ set(CoreLib_HEADERS
|
|||||||
|
|
||||||
use_precompiled_header(_CoreLibPch.h Pch.cpp CoreLib_HEADERS CoreLib_SOURCES)
|
use_precompiled_header(_CoreLibPch.h Pch.cpp CoreLib_HEADERS CoreLib_SOURCES)
|
||||||
add_library(CoreLib STATIC ${CoreLib_SOURCES} ${CoreLib_HEADERS})
|
add_library(CoreLib STATIC ${CoreLib_SOURCES} ${CoreLib_HEADERS})
|
||||||
target_link_libraries(CoreLib ${PCRE_LIBRARY})
|
|
||||||
|
|
||||||
if(UNIX)
|
if(UNIX)
|
||||||
target_link_libraries(CoreLib pthread)
|
target_link_libraries(CoreLib pthread)
|
||||||
|
@ -47,7 +47,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com
|
|||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
|
|
||||||
#include "plString.h"
|
#include "plString.h"
|
||||||
#include <pcre.h>
|
#include <regex>
|
||||||
|
|
||||||
const plString plString::Null;
|
const plString plString::Null;
|
||||||
|
|
||||||
@ -605,66 +605,44 @@ ssize_t plString::Find(const char *str, CaseSensitivity sense) const
|
|||||||
|
|
||||||
bool plString::REMatch(const char *pattern, CaseSensitivity sense) const
|
bool plString::REMatch(const char *pattern, CaseSensitivity sense) const
|
||||||
{
|
{
|
||||||
int opts = PCRE_UTF8;
|
auto opts = std::regex_constants::ECMAScript;
|
||||||
if (sense == kCaseInsensitive)
|
if (sense == kCaseInsensitive)
|
||||||
opts |= PCRE_CASELESS;
|
opts |= std::regex_constants::icase;
|
||||||
|
|
||||||
plString pat_full = plString::Format("(?:%s)\\z", pattern);
|
std::regex re;
|
||||||
const char *errptr;
|
try {
|
||||||
int erroffs;
|
re = std::regex(pattern, opts);
|
||||||
std::unique_ptr<pcre, std::function<void (pcre *)>>
|
if (std::regex_match(c_str(), re))
|
||||||
re(pcre_compile(pat_full.c_str(), opts, &errptr, &erroffs, nullptr), pcre_free);
|
return true;
|
||||||
if (!re.get()) {
|
} catch (const std::regex_error& e) {
|
||||||
hsAssert(0, plString::Format("Invalid Regex pattern: %s (at %d)", errptr, erroffs).c_str());
|
hsAssert(0, plString::Format("Regex match error: %s", e.what()).c_str());
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = pcre_exec(re.get(), nullptr, c_str(), GetSize(), 0,
|
|
||||||
PCRE_ANCHORED, nullptr, 0);
|
|
||||||
if (result >= 0)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
hsAssert(result == PCRE_ERROR_NOMATCH, plString::Format("Regex match error: %d", result).c_str());
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<plString> plString::RESearch(const char *pattern,
|
std::vector<plString> plString::RESearch(const char *pattern,
|
||||||
CaseSensitivity sense) const
|
CaseSensitivity sense) const
|
||||||
{
|
{
|
||||||
int opts = PCRE_UTF8;
|
auto opts = std::regex_constants::ECMAScript;
|
||||||
if (sense == kCaseInsensitive)
|
if (sense == kCaseInsensitive)
|
||||||
opts |= PCRE_CASELESS;
|
opts |= std::regex_constants::icase;
|
||||||
|
|
||||||
const char *errptr;
|
std::vector<plString> substrings;
|
||||||
int erroffs;
|
|
||||||
std::unique_ptr<pcre, std::function<void (pcre *)>>
|
try {
|
||||||
re(pcre_compile(pattern, opts, &errptr, &erroffs, nullptr), pcre_free);
|
std::regex re(pattern, opts);
|
||||||
if (!re.get()) {
|
std::cmatch matches;
|
||||||
hsAssert(0, plString::Format("Invalid Regex pattern: %s (at %d)", errptr, erroffs).c_str());
|
std::regex_search(c_str(), matches, re);
|
||||||
return std::vector<plString>();
|
substrings.resize(matches.size());
|
||||||
|
|
||||||
|
for (size_t i = 0; i < matches.size(); ++i)
|
||||||
|
substrings[i] = matches[i].str().c_str();
|
||||||
|
} catch (const std::regex_error& e) {
|
||||||
|
hsAssert(0, plString::Format("Regex search error: %s", e.what()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
int ncaps = 0;
|
return substrings;
|
||||||
pcre_fullinfo(re.get(), nullptr, PCRE_INFO_CAPTURECOUNT, &ncaps);
|
|
||||||
|
|
||||||
ncaps += 1; // For the whole-pattern capture
|
|
||||||
std::unique_ptr<int> outvec(new int[ncaps * 3]);
|
|
||||||
memset(outvec.get(), -1, sizeof(int) * ncaps * 3);
|
|
||||||
int result = pcre_exec(re.get(), nullptr, c_str(), GetSize(), 0, 0,
|
|
||||||
outvec.get(), ncaps * 3);
|
|
||||||
if (result >= 0) {
|
|
||||||
std::vector<plString> caps;
|
|
||||||
caps.resize(ncaps);
|
|
||||||
for (int i = 0; i < ncaps; ++i) {
|
|
||||||
int start = outvec.get()[i*2], end = outvec.get()[i*2+1];
|
|
||||||
if (start >= 0)
|
|
||||||
caps[i] = Substr(start, end - start);
|
|
||||||
}
|
|
||||||
return caps;
|
|
||||||
}
|
|
||||||
|
|
||||||
hsAssert(result == PCRE_ERROR_NOMATCH, plString::Format("Regex search error: %d", result).c_str());
|
|
||||||
return std::vector<plString>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool in_set(char key, const char *charset)
|
static bool in_set(char key, const char *charset)
|
||||||
|
Reference in New Issue
Block a user