2002-10-22 00:09:32 +00:00
|
|
|
//
|
|
|
|
// file: repattrn.cpp
|
|
|
|
//
|
|
|
|
/*
|
2003-03-21 00:40:25 +00:00
|
|
|
***************************************************************************
|
2004-05-19 20:42:44 +00:00
|
|
|
* Copyright (C) 2002-2004 International Business Machines Corporation *
|
2003-03-21 00:40:25 +00:00
|
|
|
* and others. All rights reserved. *
|
|
|
|
***************************************************************************
|
2002-10-22 00:09:32 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2002-11-07 02:34:46 +00:00
|
|
|
|
|
|
|
#if !UCONFIG_NO_REGULAR_EXPRESSIONS
|
|
|
|
|
2002-10-22 00:09:32 +00:00
|
|
|
#include "unicode/regex.h"
|
2003-08-22 23:26:53 +00:00
|
|
|
#include "unicode/uclean.h"
|
2002-10-22 00:09:32 +00:00
|
|
|
#include "uassert.h"
|
|
|
|
#include "uvector.h"
|
2003-01-08 23:38:23 +00:00
|
|
|
#include "uvectr32.h"
|
2002-10-22 00:09:32 +00:00
|
|
|
#include "regexcmp.h"
|
|
|
|
#include "regeximp.h"
|
2003-05-02 21:33:17 +00:00
|
|
|
#include "regexst.h"
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
2002-10-23 01:14:17 +00:00
|
|
|
// RegexPattern Default Constructor
|
2002-10-22 00:09:32 +00:00
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
RegexPattern::RegexPattern() {
|
2003-08-22 23:26:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
u_init(&status);
|
2003-05-02 21:33:17 +00:00
|
|
|
// Init all of this instances data.
|
2002-10-23 01:14:17 +00:00
|
|
|
init();
|
2003-05-02 21:33:17 +00:00
|
|
|
|
|
|
|
// Lazy init of all shared global sets.
|
2003-05-28 01:42:29 +00:00
|
|
|
RegexStaticSets::initGlobals(&fDeferredStatus);
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copy Constructor Note: This is a rather inefficient implementation,
|
|
|
|
// but it probably doesn't matter.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
RegexPattern::RegexPattern(const RegexPattern &other) : UObject(other) {
|
|
|
|
init();
|
|
|
|
*this = other;
|
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Assignmenet Operator
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
RegexPattern &RegexPattern::operator = (const RegexPattern &other) {
|
|
|
|
if (this == &other) {
|
|
|
|
// Source and destination are the same. Don't do anything.
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean out any previous contents of object being assigned to.
|
|
|
|
zap();
|
|
|
|
|
|
|
|
// Give target object a default initialization
|
|
|
|
init();
|
|
|
|
|
|
|
|
// Copy simple fields
|
|
|
|
fPattern = other.fPattern;
|
|
|
|
fFlags = other.fFlags;
|
|
|
|
fLiteralText = other.fLiteralText;
|
2003-03-21 00:40:25 +00:00
|
|
|
fDeferredStatus = other.fDeferredStatus;
|
2003-02-26 05:16:49 +00:00
|
|
|
fMinMatchLen = other.fMinMatchLen;
|
2004-01-08 00:40:59 +00:00
|
|
|
fFrameSize = other.fFrameSize;
|
|
|
|
fDataSize = other.fDataSize;
|
2002-10-24 22:16:07 +00:00
|
|
|
fMaxCaptureDigits = other.fMaxCaptureDigits;
|
2003-03-13 01:56:01 +00:00
|
|
|
fStaticSets = other.fStaticSets;
|
2004-01-08 00:40:59 +00:00
|
|
|
fStaticSets8 = other.fStaticSets8;
|
2003-03-13 01:56:01 +00:00
|
|
|
|
2003-03-06 01:57:52 +00:00
|
|
|
fStartType = other.fStartType;
|
2003-03-13 01:56:01 +00:00
|
|
|
fInitialStringIdx = other.fInitialStringIdx;
|
|
|
|
fInitialStringLen = other.fInitialStringLen;
|
2003-03-28 02:31:17 +00:00
|
|
|
*fInitialChars = *other.fInitialChars;
|
2003-03-13 01:56:01 +00:00
|
|
|
fInitialChar = other.fInitialChar;
|
2004-01-08 00:40:59 +00:00
|
|
|
*fInitialChars8 = *other.fInitialChars8;
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
// Copy the pattern. It's just values, nothing deep to copy.
|
2003-03-21 00:40:25 +00:00
|
|
|
fCompiledPat->assign(*other.fCompiledPat, fDeferredStatus);
|
|
|
|
fGroupMap->assign(*other.fGroupMap, fDeferredStatus);
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
// Copy the Unicode Sets.
|
|
|
|
// Could be made more efficient if the sets were reference counted and shared,
|
|
|
|
// but I doubt that pattern copying will be particularly common.
|
2002-11-14 18:27:00 +00:00
|
|
|
// Note: init() already added an empty element zero to fSets
|
2003-01-16 01:12:04 +00:00
|
|
|
int32_t i;
|
2003-03-28 02:31:17 +00:00
|
|
|
int32_t numSets = other.fSets->size();
|
|
|
|
fSets8 = new Regex8BitSet[numSets];
|
|
|
|
for (i=1; i<numSets; i++) {
|
2003-03-21 00:40:25 +00:00
|
|
|
if (U_FAILURE(fDeferredStatus)) {
|
|
|
|
return *this;
|
|
|
|
}
|
2002-10-23 01:14:17 +00:00
|
|
|
UnicodeSet *sourceSet = (UnicodeSet *)other.fSets->elementAt(i);
|
|
|
|
UnicodeSet *newSet = new UnicodeSet(*sourceSet);
|
|
|
|
if (newSet == NULL) {
|
2003-03-21 00:40:25 +00:00
|
|
|
fDeferredStatus = U_MEMORY_ALLOCATION_ERROR;
|
2002-10-23 01:14:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-03-21 00:40:25 +00:00
|
|
|
fSets->addElement(newSet, fDeferredStatus);
|
2003-03-28 02:31:17 +00:00
|
|
|
fSets8[i] = other.fSets8[i];
|
2002-10-22 00:09:32 +00:00
|
|
|
}
|
2003-03-27 01:25:20 +00:00
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// init Shared initialization for use by constructors.
|
|
|
|
// Bring an uninitialized RegexPattern up to a default state.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
void RegexPattern::init() {
|
2004-01-08 00:40:59 +00:00
|
|
|
fPattern.remove();
|
2002-10-23 01:14:17 +00:00
|
|
|
fFlags = 0;
|
2004-01-08 00:40:59 +00:00
|
|
|
fCompiledPat = 0;
|
|
|
|
fLiteralText.remove();
|
|
|
|
fSets = NULL;
|
|
|
|
fSets8 = NULL;
|
2003-03-21 00:40:25 +00:00
|
|
|
fDeferredStatus = U_ZERO_ERROR;
|
2003-02-26 05:16:49 +00:00
|
|
|
fMinMatchLen = 0;
|
2003-01-16 01:12:04 +00:00
|
|
|
fFrameSize = 0;
|
2003-01-17 01:43:54 +00:00
|
|
|
fDataSize = 0;
|
2004-01-08 00:40:59 +00:00
|
|
|
fGroupMap = NULL;
|
|
|
|
fMaxCaptureDigits = 1;
|
|
|
|
fStaticSets = NULL;
|
|
|
|
fStaticSets8 = NULL;
|
2003-03-06 01:57:52 +00:00
|
|
|
fStartType = START_NO_INFO;
|
2003-03-13 01:56:01 +00:00
|
|
|
fInitialStringIdx = 0;
|
|
|
|
fInitialStringLen = 0;
|
|
|
|
fInitialChars = NULL;
|
|
|
|
fInitialChar = 0;
|
2004-01-08 00:40:59 +00:00
|
|
|
fInitialChars8 = NULL;
|
2002-10-23 01:14:17 +00:00
|
|
|
|
2003-03-21 00:40:25 +00:00
|
|
|
fCompiledPat = new UVector32(fDeferredStatus);
|
|
|
|
fGroupMap = new UVector32(fDeferredStatus);
|
|
|
|
fSets = new UVector(fDeferredStatus);
|
|
|
|
fInitialChars = new UnicodeSet;
|
2003-03-27 01:25:20 +00:00
|
|
|
fInitialChars8 = new Regex8BitSet;
|
2003-03-21 00:40:25 +00:00
|
|
|
if (U_FAILURE(fDeferredStatus)) {
|
|
|
|
return;
|
|
|
|
}
|
2003-03-27 01:25:20 +00:00
|
|
|
if (fCompiledPat == NULL || fGroupMap == NULL || fSets == NULL ||
|
|
|
|
fInitialChars == NULL || fInitialChars8 == NULL) {
|
2003-03-21 00:40:25 +00:00
|
|
|
fDeferredStatus = U_MEMORY_ALLOCATION_ERROR;
|
2002-10-23 01:14:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slot zero of the vector of sets is reserved. Fill it here.
|
2003-03-21 00:40:25 +00:00
|
|
|
fSets->addElement((int32_t)0, fDeferredStatus);
|
2002-10-23 01:14:17 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// zap Delete everything owned by this RegexPattern.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
void RegexPattern::zap() {
|
2002-10-22 00:09:32 +00:00
|
|
|
delete fCompiledPat;
|
2002-10-23 01:14:17 +00:00
|
|
|
fCompiledPat = NULL;
|
2002-10-22 00:09:32 +00:00
|
|
|
int i;
|
2002-11-14 18:27:00 +00:00
|
|
|
for (i=1; i<fSets->size(); i++) {
|
2002-10-22 00:09:32 +00:00
|
|
|
UnicodeSet *s;
|
|
|
|
s = (UnicodeSet *)fSets->elementAt(i);
|
|
|
|
if (s != NULL) {
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete fSets;
|
|
|
|
fSets = NULL;
|
2004-01-08 00:40:59 +00:00
|
|
|
delete[] fSets8;
|
|
|
|
fSets8 = NULL;
|
2003-01-16 01:12:04 +00:00
|
|
|
delete fGroupMap;
|
|
|
|
fGroupMap = NULL;
|
2003-03-13 01:56:01 +00:00
|
|
|
delete fInitialChars;
|
|
|
|
fInitialChars = NULL;
|
2003-03-27 01:25:20 +00:00
|
|
|
delete fInitialChars8;
|
|
|
|
fInitialChars8 = NULL;
|
2002-10-23 01:14:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Destructor
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
RegexPattern::~RegexPattern() {
|
|
|
|
zap();
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Clone
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
2002-10-22 00:09:32 +00:00
|
|
|
RegexPattern *RegexPattern::clone() const {
|
|
|
|
RegexPattern *copy = new RegexPattern(*this);
|
|
|
|
return copy;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// operator == (comparison) Consider to patterns to be == if the
|
|
|
|
// pattern strings and the flags are the same.
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
UBool RegexPattern::operator ==(const RegexPattern &other) const {
|
|
|
|
UBool r = this->fFlags == other.fFlags &&
|
|
|
|
this->fPattern == other.fPattern &&
|
2003-03-21 00:40:25 +00:00
|
|
|
this->fDeferredStatus == other.fDeferredStatus;
|
2002-10-23 01:14:17 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2002-10-22 00:09:32 +00:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// compile
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
2004-08-24 17:38:33 +00:00
|
|
|
RegexPattern * U_EXPORT2
|
|
|
|
RegexPattern::compile(const UnicodeString ®ex,
|
|
|
|
uint32_t flags,
|
|
|
|
UParseError &pe,
|
|
|
|
UErrorCode &status)
|
|
|
|
{
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-11-14 18:27:00 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-10-22 00:09:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-02-07 02:04:14 +00:00
|
|
|
|
|
|
|
const uint32_t allFlags = UREGEX_CANON_EQ | UREGEX_CASE_INSENSITIVE | UREGEX_COMMENTS |
|
2004-03-22 22:16:21 +00:00
|
|
|
UREGEX_DOTALL | UREGEX_MULTILINE | UREGEX_UWORD;
|
2003-02-07 02:04:14 +00:00
|
|
|
|
|
|
|
if ((flags & ~allFlags) != 0) {
|
|
|
|
status = U_REGEX_INVALID_FLAG;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-02-13 01:10:22 +00:00
|
|
|
if ((flags & UREGEX_CANON_EQ) != 0) {
|
2002-11-14 18:27:00 +00:00
|
|
|
status = U_REGEX_UNIMPLEMENTED;
|
2002-11-11 18:49:49 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-10-22 00:09:32 +00:00
|
|
|
RegexPattern *This = new RegexPattern;
|
|
|
|
if (This == NULL) {
|
2002-11-14 18:27:00 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-21 00:40:25 +00:00
|
|
|
if (U_FAILURE(This->fDeferredStatus)) {
|
|
|
|
status = This->fDeferredStatus;
|
2002-10-22 00:09:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-23 01:14:17 +00:00
|
|
|
This->fFlags = flags;
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2003-02-11 01:17:51 +00:00
|
|
|
RegexCompile compiler(This, status);
|
|
|
|
compiler.compile(regex, pe, status);
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
return This;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
//
|
|
|
|
// compile with default flags.
|
|
|
|
//
|
2004-08-24 17:38:33 +00:00
|
|
|
RegexPattern * U_EXPORT2
|
|
|
|
RegexPattern::compile(const UnicodeString ®ex,
|
|
|
|
UParseError &pe,
|
|
|
|
UErrorCode &err)
|
2002-10-23 01:14:17 +00:00
|
|
|
{
|
|
|
|
return compile(regex, 0, pe, err);
|
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-24 05:23:07 +00:00
|
|
|
//
|
|
|
|
// compile with no UParseErr parameter.
|
|
|
|
//
|
2004-08-24 17:38:33 +00:00
|
|
|
RegexPattern * U_EXPORT2
|
|
|
|
RegexPattern::compile( const UnicodeString ®ex,
|
2003-03-24 05:23:07 +00:00
|
|
|
uint32_t flags,
|
|
|
|
UErrorCode &err)
|
|
|
|
{
|
|
|
|
UParseError pe;
|
|
|
|
return compile(regex, flags, pe, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-10-22 00:09:32 +00:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// flags
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
2002-11-19 19:31:03 +00:00
|
|
|
uint32_t RegexPattern::flags() const {
|
2002-10-22 00:09:32 +00:00
|
|
|
return fFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// matcher(UnicodeString, err)
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
RegexMatcher *RegexPattern::matcher(const UnicodeString &input,
|
2003-03-21 00:40:25 +00:00
|
|
|
UErrorCode &status) const {
|
2003-03-24 05:23:07 +00:00
|
|
|
RegexMatcher *retMatcher = matcher(status);
|
|
|
|
if (retMatcher != NULL) {
|
|
|
|
retMatcher->reset(input);
|
|
|
|
}
|
|
|
|
return retMatcher;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2003-03-24 05:23:07 +00:00
|
|
|
|
2004-11-05 18:18:36 +00:00
|
|
|
RegexMatcher *RegexPattern::matcher(const UChar * /*input*/,
|
2004-10-22 16:32:12 +00:00
|
|
|
UErrorCode &status) const
|
|
|
|
{
|
2004-11-05 18:18:36 +00:00
|
|
|
/* This should never get called. The API with UnicodeString should be called instead. */
|
2004-10-22 16:32:12 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
status = U_UNSUPPORTED_ERROR;
|
|
|
|
}
|
2004-11-05 18:18:36 +00:00
|
|
|
return NULL;
|
2004-10-22 16:32:12 +00:00
|
|
|
}
|
2003-03-24 05:23:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// matcher(status)
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
RegexMatcher *RegexPattern::matcher(UErrorCode &status) const {
|
2002-10-22 00:09:32 +00:00
|
|
|
RegexMatcher *retMatcher = NULL;
|
|
|
|
|
2003-03-21 00:40:25 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-11-14 18:27:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2003-03-21 00:40:25 +00:00
|
|
|
if (U_FAILURE(fDeferredStatus)) {
|
|
|
|
status = fDeferredStatus;
|
2002-11-14 18:27:00 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
retMatcher = new RegexMatcher(this);
|
|
|
|
if (retMatcher == NULL) {
|
2003-03-21 00:40:25 +00:00
|
|
|
status = U_MEMORY_ALLOCATION_ERROR;
|
2002-10-22 00:09:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return retMatcher;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2002-10-23 01:14:17 +00:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// matches Convenience function to test for a match, starting
|
|
|
|
// with a pattern string and a data string.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
2004-08-24 17:38:33 +00:00
|
|
|
UBool U_EXPORT2 RegexPattern::matches(const UnicodeString ®ex,
|
2002-10-23 01:14:17 +00:00
|
|
|
const UnicodeString &input,
|
|
|
|
UParseError &pe,
|
|
|
|
UErrorCode &status) {
|
|
|
|
|
2002-11-14 18:27:00 +00:00
|
|
|
if (U_FAILURE(status)) {return FALSE;}
|
|
|
|
|
|
|
|
UBool retVal;
|
2002-10-23 01:14:17 +00:00
|
|
|
RegexPattern *pat = NULL;
|
|
|
|
RegexMatcher *matcher = NULL;
|
|
|
|
|
2002-11-14 18:27:00 +00:00
|
|
|
pat = RegexPattern::compile(regex, 0, pe, status);
|
2002-10-23 01:14:17 +00:00
|
|
|
matcher = pat->matcher(input, status);
|
2002-11-14 18:27:00 +00:00
|
|
|
retVal = matcher->matches(status);
|
2002-10-23 01:14:17 +00:00
|
|
|
|
|
|
|
delete matcher;
|
|
|
|
delete pat;
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// pattern
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
UnicodeString RegexPattern::pattern() const {
|
|
|
|
return fPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// split
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
2002-10-23 01:14:17 +00:00
|
|
|
int32_t RegexPattern::split(const UnicodeString &input,
|
2002-10-22 00:09:32 +00:00
|
|
|
UnicodeString dest[],
|
2002-10-23 16:38:10 +00:00
|
|
|
int32_t destCapacity,
|
|
|
|
UErrorCode &status) const
|
2002-10-22 00:09:32 +00:00
|
|
|
{
|
2002-10-23 16:38:10 +00:00
|
|
|
if (U_FAILURE(status)) {
|
2002-10-22 00:09:32 +00:00
|
|
|
return 0;
|
|
|
|
};
|
2002-10-23 16:38:10 +00:00
|
|
|
|
2003-03-24 05:23:07 +00:00
|
|
|
RegexMatcher m(this);
|
|
|
|
int32_t r = m.split(input, dest, destCapacity, status);
|
|
|
|
return r;
|
2002-10-22 00:09:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// dump Output the compiled form of the pattern.
|
|
|
|
// Debugging function only.
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------
|
2003-04-18 21:43:25 +00:00
|
|
|
#if defined(REGEX_DEBUG)
|
2003-12-02 19:14:26 +00:00
|
|
|
void RegexPattern::dumpOp(int32_t index) const {
|
2003-04-18 21:43:25 +00:00
|
|
|
static const char * const opNames[] = {URX_OPCODE_NAMES};
|
2002-11-19 19:31:03 +00:00
|
|
|
int32_t op = fCompiledPat->elementAti(index);
|
|
|
|
int32_t val = URX_VAL(op);
|
|
|
|
int32_t type = URX_TYPE(op);
|
|
|
|
int32_t pinnedType = type;
|
|
|
|
if (pinnedType >= sizeof(opNames)/sizeof(char *)) {
|
|
|
|
pinnedType = 0;
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%4d %08x %-15s ", index, op, opNames[pinnedType]));
|
2002-11-19 19:31:03 +00:00
|
|
|
switch (type) {
|
|
|
|
case URX_NOP:
|
|
|
|
case URX_DOTANY:
|
2003-03-26 01:17:16 +00:00
|
|
|
case URX_DOTANY_ALL:
|
|
|
|
case URX_DOTANY_PL:
|
|
|
|
case URX_DOTANY_ALL_PL:
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_FAIL:
|
2003-02-12 01:28:01 +00:00
|
|
|
case URX_CARET:
|
|
|
|
case URX_DOLLAR:
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_BACKSLASH_G:
|
|
|
|
case URX_BACKSLASH_X:
|
|
|
|
case URX_END:
|
2003-02-12 01:28:01 +00:00
|
|
|
case URX_DOLLAR_M:
|
|
|
|
case URX_CARET_M:
|
2002-11-19 19:31:03 +00:00
|
|
|
// Types with no operand field of interest.
|
|
|
|
break;
|
|
|
|
|
2003-01-16 01:12:04 +00:00
|
|
|
case URX_RESERVED_OP:
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_START_CAPTURE:
|
|
|
|
case URX_END_CAPTURE:
|
|
|
|
case URX_STATE_SAVE:
|
|
|
|
case URX_JMP:
|
2003-03-18 01:51:36 +00:00
|
|
|
case URX_JMP_SAV:
|
2003-03-26 01:17:16 +00:00
|
|
|
case URX_JMP_SAV_X:
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_BACKSLASH_B:
|
2003-11-08 02:01:42 +00:00
|
|
|
case URX_BACKSLASH_BU:
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_BACKSLASH_D:
|
|
|
|
case URX_BACKSLASH_Z:
|
|
|
|
case URX_STRING_LEN:
|
2003-01-16 01:12:04 +00:00
|
|
|
case URX_CTR_INIT:
|
|
|
|
case URX_CTR_INIT_NG:
|
|
|
|
case URX_CTR_LOOP:
|
|
|
|
case URX_CTR_LOOP_NG:
|
|
|
|
case URX_RELOC_OPRND:
|
2003-01-17 01:43:54 +00:00
|
|
|
case URX_STO_SP:
|
|
|
|
case URX_LD_SP:
|
2003-01-25 18:57:42 +00:00
|
|
|
case URX_BACKREF:
|
|
|
|
case URX_STO_INP_LOC:
|
|
|
|
case URX_JMPX:
|
2003-02-07 02:04:14 +00:00
|
|
|
case URX_LA_START:
|
|
|
|
case URX_LA_END:
|
2003-02-12 01:28:01 +00:00
|
|
|
case URX_BACKREF_I:
|
2003-03-02 19:11:09 +00:00
|
|
|
case URX_LB_START:
|
|
|
|
case URX_LB_CONT:
|
|
|
|
case URX_LB_END:
|
|
|
|
case URX_LBN_CONT:
|
|
|
|
case URX_LBN_END:
|
2003-03-28 02:31:17 +00:00
|
|
|
case URX_LOOP_C:
|
2003-04-02 23:10:16 +00:00
|
|
|
case URX_LOOP_DOT_I:
|
2002-11-19 19:31:03 +00:00
|
|
|
// types with an integer operand field.
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%d", val));
|
2002-11-19 19:31:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case URX_ONECHAR:
|
2003-02-07 02:04:14 +00:00
|
|
|
case URX_ONECHAR_I:
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", val<256?val:'?'));
|
2002-11-19 19:31:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case URX_STRING:
|
2003-02-07 02:04:14 +00:00
|
|
|
case URX_STRING_I:
|
2002-11-19 19:31:03 +00:00
|
|
|
{
|
|
|
|
int32_t lengthOp = fCompiledPat->elementAti(index+1);
|
|
|
|
U_ASSERT(URX_TYPE(lengthOp) == URX_STRING_LEN);
|
|
|
|
int32_t length = URX_VAL(lengthOp);
|
|
|
|
int32_t i;
|
|
|
|
for (i=val; i<val+length; i++) {
|
|
|
|
UChar c = fLiteralText[i];
|
|
|
|
if (c < 32 || c >= 256) {c = '.';}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", c));
|
2002-11-19 19:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-11-19 19:31:03 +00:00
|
|
|
case URX_SETREF:
|
2003-03-28 02:31:17 +00:00
|
|
|
case URX_LOOP_SR_I:
|
2002-11-19 19:31:03 +00:00
|
|
|
{
|
|
|
|
UnicodeString s;
|
|
|
|
UnicodeSet *set = (UnicodeSet *)fSets->elementAt(val);
|
|
|
|
set->toPattern(s, TRUE);
|
|
|
|
for (int32_t i=0; i<s.length(); i++) {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", s.charAt(i)));
|
2002-11-19 19:31:03 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-20 23:30:20 +00:00
|
|
|
break;
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2002-11-20 23:30:20 +00:00
|
|
|
case URX_STATIC_SETREF:
|
2003-03-27 01:25:20 +00:00
|
|
|
case URX_STAT_SETREF_N:
|
2002-11-20 23:30:20 +00:00
|
|
|
{
|
|
|
|
UnicodeString s;
|
|
|
|
if (val & URX_NEG_SET) {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("NOT "));
|
2002-11-20 23:30:20 +00:00
|
|
|
val &= ~URX_NEG_SET;
|
|
|
|
}
|
|
|
|
UnicodeSet *set = fStaticSets[val];
|
|
|
|
set->toPattern(s, TRUE);
|
|
|
|
for (int32_t i=0; i<s.length(); i++) {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", s.charAt(i)));
|
2002-11-20 23:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2002-11-19 19:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
default:
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("??????"));
|
2002-11-19 19:31:03 +00:00
|
|
|
break;
|
2002-10-22 00:09:32 +00:00
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("\n"));
|
2002-11-19 19:31:03 +00:00
|
|
|
}
|
2003-12-02 19:14:26 +00:00
|
|
|
#endif
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
|
2003-04-18 21:43:25 +00:00
|
|
|
#if defined(REGEX_DEBUG)
|
2003-12-06 01:49:56 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
RegexPatternDump(const RegexPattern *This) {
|
2002-11-19 19:31:03 +00:00
|
|
|
int index;
|
|
|
|
int i;
|
|
|
|
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("Original Pattern: "));
|
|
|
|
for (i=0; i<This->fPattern.length(); i++) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", This->fPattern.charAt(i)));
|
2002-11-19 19:31:03 +00:00
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("\n"));
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" Min Match Length: %d\n", This->fMinMatchLen));
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" Match Start Type: %s\n", START_OF_MATCH_STR(This->fStartType)));
|
|
|
|
if (This->fStartType == START_STRING) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" Initial match sting: \""));
|
|
|
|
for (i=This->fInitialStringIdx; i<This->fInitialStringIdx+This->fInitialStringLen; i++) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c", This->fLiteralText[i])); // TODO: non-printables, surrogates.
|
2003-03-13 01:56:01 +00:00
|
|
|
}
|
|
|
|
|
2003-12-06 01:49:56 +00:00
|
|
|
} else if (This->fStartType == START_SET) {
|
|
|
|
int32_t numSetChars = This->fInitialChars->size();
|
2003-03-13 01:56:01 +00:00
|
|
|
if (numSetChars > 20) {
|
|
|
|
numSetChars = 20;
|
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" Match First Chars : "));
|
2003-03-13 01:56:01 +00:00
|
|
|
for (i=0; i<numSetChars; i++) {
|
2003-12-06 01:49:56 +00:00
|
|
|
UChar32 c = This->fInitialChars->charAt(i);
|
2003-03-13 01:56:01 +00:00
|
|
|
if (0x20<c && c <0x7e) {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c ", c));
|
2003-03-13 01:56:01 +00:00
|
|
|
} else {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%#x ", c));
|
2003-03-13 01:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
if (numSetChars < This->fInitialChars->size()) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" ..."));
|
2003-03-13 01:56:01 +00:00
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("\n"));
|
2003-03-13 01:56:01 +00:00
|
|
|
|
2003-12-06 01:49:56 +00:00
|
|
|
} else if (This->fStartType == START_CHAR) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF((" First char of Match : "));
|
|
|
|
if (0x20 < This->fInitialChar && This->fInitialChar<0x7e) {
|
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%c\n", This->fInitialChar));
|
2003-03-13 01:56:01 +00:00
|
|
|
} else {
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("%#x\n", This->fInitialChar));
|
2003-03-13 01:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("\nIndex Binary Type Operand\n" \
|
|
|
|
"-------------------------------------------\n"));
|
|
|
|
for (index = 0; index<This->fCompiledPat->size(); index++) {
|
|
|
|
This->dumpOp(index);
|
2002-10-22 00:09:32 +00:00
|
|
|
}
|
2003-12-06 01:49:56 +00:00
|
|
|
REGEX_DUMP_DEBUG_PRINTF(("\n\n"));
|
2002-10-22 00:09:32 +00:00
|
|
|
};
|
2003-12-06 01:49:56 +00:00
|
|
|
#endif
|
2002-10-22 00:09:32 +00:00
|
|
|
|
2003-03-13 01:56:01 +00:00
|
|
|
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RegexPattern)
|
2002-10-22 00:09:32 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
2002-11-07 02:34:46 +00:00
|
|
|
#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS
|