2004-03-22 22:16:21 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2011-01-27 01:12:12 +00:00
|
|
|
* Copyright (C) 2004-2011, International Business Machines
|
2004-03-22 22:16:21 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: regex.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_REGULAR_EXPRESSIONS
|
|
|
|
|
|
|
|
#include "unicode/regex.h"
|
|
|
|
#include "unicode/uregex.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/uchar.h"
|
2004-06-13 01:11:26 +00:00
|
|
|
#include "unicode/uobject.h"
|
2011-07-27 05:53:56 +00:00
|
|
|
#include "unicode/utf16.h"
|
2004-03-22 22:16:21 +00:00
|
|
|
#include "umutex.h"
|
|
|
|
#include "uassert.h"
|
2004-06-13 01:11:26 +00:00
|
|
|
#include "cmemory.h"
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
#include "regextxt.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
2006-11-28 07:34:13 +00:00
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
#define REMAINING_CAPACITY(idx,len) ((((len)-(idx))>0)?((len)-(idx)):0)
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
struct RegularExpression: public UMemory {
|
2004-06-13 01:11:26 +00:00
|
|
|
public:
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression();
|
|
|
|
~RegularExpression();
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t fMagic;
|
|
|
|
RegexPattern *fPat;
|
|
|
|
int32_t *fPatRefCount;
|
|
|
|
UChar *fPatString;
|
|
|
|
int32_t fPatStringLen;
|
|
|
|
RegexMatcher *fMatcher;
|
2004-03-24 01:57:51 +00:00
|
|
|
const UChar *fText; // Text from setText()
|
|
|
|
int32_t fTextLength; // Length provided by user with setText(), which
|
|
|
|
// may be -1.
|
2010-02-03 02:59:35 +00:00
|
|
|
UBool fOwnsText;
|
2004-03-22 22:16:21 +00:00
|
|
|
};
|
|
|
|
|
2004-04-09 18:59:44 +00:00
|
|
|
static const int32_t REXP_MAGIC = 0x72657870; // "rexp" in ASCII
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression::RegularExpression() {
|
2004-03-22 22:16:21 +00:00
|
|
|
fMagic = REXP_MAGIC;
|
|
|
|
fPat = NULL;
|
|
|
|
fPatRefCount = NULL;
|
|
|
|
fPatString = NULL;
|
|
|
|
fPatStringLen = 0;
|
|
|
|
fMatcher = NULL;
|
|
|
|
fText = NULL;
|
|
|
|
fTextLength = 0;
|
2010-02-03 02:59:35 +00:00
|
|
|
fOwnsText = FALSE;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression::~RegularExpression() {
|
2004-06-13 01:11:26 +00:00
|
|
|
delete fMatcher;
|
|
|
|
fMatcher = NULL;
|
|
|
|
if (fPatRefCount!=NULL && umtx_atomic_dec(fPatRefCount)==0) {
|
|
|
|
delete fPat;
|
|
|
|
uprv_free(fPatString);
|
|
|
|
uprv_free(fPatRefCount);
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
if (fOwnsText && fText!=NULL) {
|
|
|
|
uprv_free((void *)fText);
|
|
|
|
}
|
2004-06-13 01:11:26 +00:00
|
|
|
fMagic = 0;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
U_NAMESPACE_USE
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// validateRE Do boilerplate style checks on API function parameters.
|
|
|
|
// Return TRUE if they look OK.
|
|
|
|
//----------------------------------------------------------------------------------------
|
2011-03-31 18:33:28 +00:00
|
|
|
static UBool validateRE(const RegularExpression *re, UBool requiresText, UErrorCode *status) {
|
2004-03-22 22:16:21 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (re == NULL || re->fMagic != REXP_MAGIC) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
// !!! Not sure how to update this with the new UText backing, which is stored in re->fMatcher anyway
|
|
|
|
if (requiresText && re->fText == NULL && !re->fOwnsText) {
|
2004-03-26 01:23:01 +00:00
|
|
|
*status = U_REGEX_INVALID_STATE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_open
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
U_CAPI URegularExpression * U_EXPORT2
|
|
|
|
uregex_open( const UChar *pattern,
|
|
|
|
int32_t patternLength,
|
|
|
|
uint32_t flags,
|
|
|
|
UParseError *pe,
|
|
|
|
UErrorCode *status) {
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pattern == NULL || patternLength < -1 || patternLength == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
int32_t actualPatLen = patternLength;
|
|
|
|
if (actualPatLen == -1) {
|
|
|
|
actualPatLen = u_strlen(pattern);
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *re = new RegularExpression;
|
2004-06-13 01:11:26 +00:00
|
|
|
int32_t *refC = (int32_t *)uprv_malloc(sizeof(int32_t));
|
|
|
|
UChar *patBuf = (UChar *)uprv_malloc(sizeof(UChar)*(actualPatLen+1));
|
2004-03-22 22:16:21 +00:00
|
|
|
if (re == NULL || refC == NULL || patBuf == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete re;
|
2004-06-13 01:11:26 +00:00
|
|
|
uprv_free(refC);
|
|
|
|
uprv_free(patBuf);
|
2004-03-22 22:16:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
re->fPatRefCount = refC;
|
2004-06-13 01:11:26 +00:00
|
|
|
*re->fPatRefCount = 1;
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Make a copy of the pattern string, so we can return it later if asked.
|
2010-02-03 02:59:35 +00:00
|
|
|
// For compiling the pattern, we will use a UText wrapper around
|
|
|
|
// this local copy, to avoid making even more copies.
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
re->fPatString = patBuf;
|
|
|
|
re->fPatStringLen = patternLength;
|
|
|
|
u_memcpy(patBuf, pattern, actualPatLen);
|
|
|
|
patBuf[actualPatLen] = 0;
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
UText patText = UTEXT_INITIALIZER;
|
|
|
|
utext_openUChars(&patText, patBuf, patternLength, status);
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Compile the pattern
|
|
|
|
//
|
|
|
|
if (pe != NULL) {
|
2010-02-03 02:59:35 +00:00
|
|
|
re->fPat = RegexPattern::compile(&patText, flags, *pe, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
} else {
|
2010-02-03 02:59:35 +00:00
|
|
|
re->fPat = RegexPattern::compile(&patText, flags, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
utext_close(&patText);
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
goto ErrorExit;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create the matcher object
|
|
|
|
//
|
|
|
|
re->fMatcher = re->fPat->matcher(*status);
|
2004-06-13 01:11:26 +00:00
|
|
|
if (U_SUCCESS(*status)) {
|
2009-10-01 22:52:39 +00:00
|
|
|
return (URegularExpression*)re;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorExit:
|
|
|
|
delete re;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_openUText
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
U_CAPI URegularExpression * U_EXPORT2
|
|
|
|
uregex_openUText(UText *pattern,
|
|
|
|
uint32_t flags,
|
|
|
|
UParseError *pe,
|
|
|
|
UErrorCode *status) {
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (pattern == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t patternNativeLength = utext_nativeLength(pattern);
|
|
|
|
|
|
|
|
if (patternNativeLength == 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegularExpression *re = new RegularExpression;
|
|
|
|
|
|
|
|
UErrorCode lengthStatus = U_ZERO_ERROR;
|
|
|
|
int32_t pattern16Length = utext_extract(pattern, 0, patternNativeLength, NULL, 0, &lengthStatus);
|
|
|
|
|
|
|
|
int32_t *refC = (int32_t *)uprv_malloc(sizeof(int32_t));
|
|
|
|
UChar *patBuf = (UChar *)uprv_malloc(sizeof(UChar)*(pattern16Length+1));
|
|
|
|
if (re == NULL || refC == NULL || patBuf == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
delete re;
|
|
|
|
uprv_free(refC);
|
|
|
|
uprv_free(patBuf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
re->fPatRefCount = refC;
|
|
|
|
*re->fPatRefCount = 1;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Make a copy of the pattern string, so we can return it later if asked.
|
|
|
|
// For compiling the pattern, we will use a read-only UText wrapper
|
|
|
|
// around this local copy, to avoid making even more copies.
|
|
|
|
//
|
|
|
|
re->fPatString = patBuf;
|
|
|
|
re->fPatStringLen = pattern16Length;
|
|
|
|
utext_extract(pattern, 0, patternNativeLength, patBuf, pattern16Length+1, status);
|
|
|
|
|
|
|
|
UText patText = UTEXT_INITIALIZER;
|
|
|
|
utext_openUChars(&patText, patBuf, pattern16Length, status);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Compile the pattern
|
|
|
|
//
|
|
|
|
if (pe != NULL) {
|
|
|
|
re->fPat = RegexPattern::compile(&patText, flags, *pe, *status);
|
|
|
|
} else {
|
|
|
|
re->fPat = RegexPattern::compile(&patText, flags, *status);
|
|
|
|
}
|
|
|
|
utext_close(&patText);
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
goto ErrorExit;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create the matcher object
|
|
|
|
//
|
|
|
|
re->fMatcher = re->fPat->matcher(*status);
|
|
|
|
if (U_SUCCESS(*status)) {
|
|
|
|
return (URegularExpression*)re;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorExit:
|
|
|
|
delete re;
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_close
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_close(URegularExpression *re2) {
|
|
|
|
RegularExpression *re = (RegularExpression*)re2;
|
2004-03-22 22:16:21 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(re, FALSE, &status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete re;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_clone
|
|
|
|
//
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
U_CAPI URegularExpression * U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_clone(const URegularExpression *source2, UErrorCode *status) {
|
|
|
|
RegularExpression *source = (RegularExpression*)source2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(source, FALSE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *clone = new RegularExpression;
|
2004-03-22 22:16:21 +00:00
|
|
|
if (clone == NULL) {
|
|
|
|
*status = U_MEMORY_ALLOCATION_ERROR;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
clone->fMatcher = source->fPat->matcher(*status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
delete clone;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-03-24 01:57:51 +00:00
|
|
|
clone->fPat = source->fPat;
|
|
|
|
clone->fPatRefCount = source->fPatRefCount;
|
|
|
|
clone->fPatString = source->fPatString;
|
|
|
|
clone->fPatStringLen = source->fPatStringLen;
|
2004-03-22 22:16:21 +00:00
|
|
|
umtx_atomic_inc(source->fPatRefCount);
|
2004-03-24 01:57:51 +00:00
|
|
|
// Note: fText is not cloned.
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
return (URegularExpression*)clone;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_pattern
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI const UChar * U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_pattern(const URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
int32_t *patLength,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (patLength != NULL) {
|
|
|
|
*patLength = regexp->fPatStringLen;
|
|
|
|
}
|
|
|
|
return regexp->fPatString;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_patternUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_patternUText(const URegularExpression *regexp2,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2010-09-18 03:07:17 +00:00
|
|
|
return regexp->fPat->patternText(*status);
|
2010-02-03 02:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_flags
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_flags(const URegularExpression *regexp2, UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2004-03-24 01:57:51 +00:00
|
|
|
return 0;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
int32_t flags = regexp->fPat->flags();
|
|
|
|
return flags;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_setText
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_setText(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
const UChar *text,
|
|
|
|
int32_t textLength,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (text == NULL || textLength < -1) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
if (regexp->fOwnsText && regexp->fText != NULL) {
|
|
|
|
uprv_free((void *)regexp->fText);
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
regexp->fText = text;
|
|
|
|
regexp->fTextLength = textLength;
|
2010-02-03 02:59:35 +00:00
|
|
|
regexp->fOwnsText = FALSE;
|
|
|
|
|
|
|
|
UText input = UTEXT_INITIALIZER;
|
|
|
|
utext_openUChars(&input, text, textLength, status);
|
|
|
|
regexp->fMatcher->reset(&input);
|
|
|
|
utext_close(&input); // reset() made a shallow clone, so we don't need this copy
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_setUText(URegularExpression *regexp2,
|
|
|
|
UText *text,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (text == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (regexp->fOwnsText && regexp->fText != NULL) {
|
|
|
|
uprv_free((void *)regexp->fText);
|
|
|
|
}
|
|
|
|
|
|
|
|
regexp->fText = NULL; // only fill it in on request
|
|
|
|
regexp->fTextLength = -1;
|
|
|
|
regexp->fOwnsText = TRUE;
|
|
|
|
regexp->fMatcher->reset(text);
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_getText
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI const UChar * U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_getText(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t *textLength,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
if (regexp->fText == NULL) {
|
|
|
|
// need to fill in the text
|
|
|
|
UText *inputText = regexp->fMatcher->inputText();
|
|
|
|
int64_t inputNativeLength = utext_nativeLength(inputText);
|
|
|
|
if (UTEXT_FULL_TEXT_IN_CHUNK(inputText, inputNativeLength)) {
|
|
|
|
regexp->fText = inputText->chunkContents;
|
2010-02-27 07:43:07 +00:00
|
|
|
regexp->fTextLength = (int32_t)inputNativeLength;
|
2010-02-03 02:59:35 +00:00
|
|
|
regexp->fOwnsText = FALSE; // because the UText owns it
|
|
|
|
} else {
|
|
|
|
UErrorCode lengthStatus = U_ZERO_ERROR;
|
|
|
|
regexp->fTextLength = utext_extract(inputText, 0, inputNativeLength, NULL, 0, &lengthStatus); // buffer overflow error
|
|
|
|
UChar *inputChars = (UChar *)uprv_malloc(sizeof(UChar)*(regexp->fTextLength+1));
|
|
|
|
|
|
|
|
utext_extract(inputText, 0, inputNativeLength, inputChars, regexp->fTextLength+1, status);
|
|
|
|
regexp->fText = inputChars;
|
|
|
|
regexp->fOwnsText = TRUE; // should already be set but just in case
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
if (textLength != NULL) {
|
|
|
|
*textLength = regexp->fTextLength;
|
|
|
|
}
|
|
|
|
return regexp->fText;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_getUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_getUText(URegularExpression *regexp2,
|
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
return dest;
|
|
|
|
}
|
2010-09-18 03:07:17 +00:00
|
|
|
return regexp->fMatcher->getInput(dest, *status);
|
2010-02-03 02:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-27 01:12:12 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_refreshUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_refreshUText(URegularExpression *regexp2,
|
|
|
|
UText *text,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2011-01-27 01:12:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->refreshInputText(text, *status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_matches
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_matches(URegularExpression *regexp2,
|
2010-09-18 03:07:17 +00:00
|
|
|
int32_t startIndex,
|
|
|
|
UErrorCode *status) {
|
|
|
|
return uregex_matches64( regexp2, (int64_t)startIndex, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
uregex_matches64(URegularExpression *regexp2,
|
|
|
|
int64_t startIndex,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2007-12-11 21:30:10 +00:00
|
|
|
UBool result = FALSE;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (startIndex == -1) {
|
|
|
|
result = regexp->fMatcher->matches(*status);
|
|
|
|
} else {
|
|
|
|
result = regexp->fMatcher->matches(startIndex, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_lookingAt
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_lookingAt(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t startIndex,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return uregex_lookingAt64( regexp2, (int64_t)startIndex, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
uregex_lookingAt64(URegularExpression *regexp2,
|
|
|
|
int64_t startIndex,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2007-12-11 21:30:10 +00:00
|
|
|
UBool result = FALSE;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (startIndex == -1) {
|
|
|
|
result = regexp->fMatcher->lookingAt(*status);
|
|
|
|
} else {
|
|
|
|
result = regexp->fMatcher->lookingAt(startIndex, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_find
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_find(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t startIndex,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return uregex_find64( regexp2, (int64_t)startIndex, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI UBool U_EXPORT2
|
|
|
|
uregex_find64(URegularExpression *regexp2,
|
|
|
|
int64_t startIndex,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2007-12-11 21:30:10 +00:00
|
|
|
UBool result = FALSE;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (startIndex == -1) {
|
|
|
|
regexp->fMatcher->resetPreserveRegion();
|
|
|
|
result = regexp->fMatcher->find();
|
|
|
|
} else {
|
|
|
|
result = regexp->fMatcher->find(startIndex, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2010-09-18 03:07:17 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_findNext
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_findNext(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
UBool result = regexp->fMatcher->find();
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_groupCount
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_groupCount(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result = regexp->fMatcher->groupCount();
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_group
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_group(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t groupNum,
|
|
|
|
UChar *dest,
|
|
|
|
int32_t destCapacity,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (destCapacity < 0 || (destCapacity > 0 && dest == NULL)) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
if (destCapacity == 0 || regexp->fText != NULL) {
|
|
|
|
// If preflighting or if we already have the text as UChars,
|
2010-09-18 03:07:17 +00:00
|
|
|
// this is a little cheaper than going through uregex_groupUTextDeep()
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Pick up the range of characters from the matcher
|
|
|
|
//
|
|
|
|
int32_t startIx = regexp->fMatcher->start(groupNum, *status);
|
|
|
|
int32_t endIx = regexp->fMatcher->end (groupNum, *status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//
|
|
|
|
// Trim length based on buffer capacity
|
|
|
|
//
|
|
|
|
int32_t fullLength = endIx - startIx;
|
|
|
|
int32_t copyLength = fullLength;
|
|
|
|
if (copyLength < destCapacity) {
|
|
|
|
dest[copyLength] = 0;
|
|
|
|
} else if (copyLength == destCapacity) {
|
|
|
|
*status = U_STRING_NOT_TERMINATED_WARNING;
|
|
|
|
} else {
|
|
|
|
copyLength = destCapacity;
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copy capture group to user's buffer
|
|
|
|
//
|
|
|
|
if (copyLength > 0) {
|
|
|
|
u_memcpy(dest, ®exp->fText[startIx], copyLength);
|
|
|
|
}
|
|
|
|
return fullLength;
|
|
|
|
} else {
|
2010-09-18 03:07:17 +00:00
|
|
|
UText *groupText = uregex_groupUTextDeep(regexp2, groupNum, NULL, status);
|
2010-02-03 02:59:35 +00:00
|
|
|
int32_t result = utext_extract(groupText, 0, utext_nativeLength(groupText), dest, destCapacity, status);
|
|
|
|
utext_close(groupText);
|
|
|
|
return result;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_groupUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_groupUText(URegularExpression *regexp2,
|
2010-09-18 03:07:17 +00:00
|
|
|
int32_t groupNum,
|
|
|
|
UText *dest,
|
|
|
|
int64_t *groupLength,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-09-18 03:07:17 +00:00
|
|
|
UErrorCode emptyTextStatus = U_ZERO_ERROR;
|
|
|
|
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
|
|
|
|
}
|
|
|
|
|
|
|
|
return regexp->fMatcher->group(groupNum, dest, *groupLength, *status);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_groupUTextDeep
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_groupUTextDeep(URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
int32_t groupNum,
|
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
UErrorCode emptyTextStatus = U_ZERO_ERROR;
|
|
|
|
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
if (regexp->fText != NULL) {
|
|
|
|
//
|
|
|
|
// Pick up the range of characters from the matcher
|
|
|
|
// and use our already-extracted characters
|
|
|
|
//
|
|
|
|
int32_t startIx = regexp->fMatcher->start(groupNum, *status);
|
|
|
|
int32_t endIx = regexp->fMatcher->end (groupNum, *status);
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
UErrorCode emptyTextStatus = U_ZERO_ERROR;
|
|
|
|
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dest) {
|
|
|
|
utext_replace(dest, 0, utext_nativeLength(dest), ®exp->fText[startIx], endIx - startIx, status);
|
|
|
|
} else {
|
|
|
|
UText groupText = UTEXT_INITIALIZER;
|
|
|
|
utext_openUChars(&groupText, ®exp->fText[startIx], endIx - startIx, status);
|
|
|
|
dest = utext_clone(NULL, &groupText, TRUE, FALSE, status);
|
|
|
|
utext_close(&groupText);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
} else {
|
|
|
|
return regexp->fMatcher->group(groupNum, dest, *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_start
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_start(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t groupNum,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return (int32_t)uregex_start64( regexp2, groupNum, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int64_t U_EXPORT2
|
|
|
|
uregex_start64(URegularExpression *regexp2,
|
|
|
|
int32_t groupNum,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result = regexp->fMatcher->start(groupNum, *status);
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_end
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_end(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t groupNum,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return (int32_t)uregex_end64( regexp2, groupNum, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int64_t U_EXPORT2
|
|
|
|
uregex_end64(URegularExpression *regexp2,
|
|
|
|
int32_t groupNum,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int32_t result = regexp->fMatcher->end(groupNum, *status);
|
|
|
|
return result;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_reset
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_reset(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t index,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
uregex_reset64( regexp2, (int64_t)index, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_reset64(URegularExpression *regexp2,
|
|
|
|
int64_t index,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->reset(index, *status);
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2007-12-11 21:30:10 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setRegion
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_setRegion(URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
int32_t regionStart,
|
|
|
|
int32_t regionLimit,
|
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
uregex_setRegion64( regexp2, (int64_t)regionStart, (int64_t)regionLimit, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_setRegion64(URegularExpression *regexp2,
|
|
|
|
int64_t regionStart,
|
|
|
|
int64_t regionLimit,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->region(regionStart, regionLimit, *status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-18 03:07:17 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setRegionAndStart
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_DRAFT void U_EXPORT2
|
|
|
|
uregex_setRegionAndStart(URegularExpression *regexp2,
|
|
|
|
int64_t regionStart,
|
|
|
|
int64_t regionLimit,
|
|
|
|
int64_t startIndex,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->region(regionStart, regionLimit, startIndex, *status);
|
|
|
|
}
|
|
|
|
|
2007-12-11 21:30:10 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_regionStart
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_regionStart(const URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return (int32_t)uregex_regionStart64(regexp2, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int64_t U_EXPORT2
|
|
|
|
uregex_regionStart64(const URegularExpression *regexp2,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->regionStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_regionEnd
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_regionEnd(const URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
UErrorCode *status) {
|
2010-09-18 03:07:17 +00:00
|
|
|
return (int32_t)uregex_regionEnd64(regexp2, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int64_t U_EXPORT2
|
|
|
|
uregex_regionEnd64(const URegularExpression *regexp2,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->regionEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_hasTransparentBounds
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_hasTransparentBounds(const URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->hasTransparentBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_useTransparentBounds
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_useTransparentBounds(URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
UBool b,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->useTransparentBounds(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_hasAnchoringBounds
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_hasAnchoringBounds(const URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->hasAnchoringBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_useAnchoringBounds
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_useAnchoringBounds(URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
UBool b,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
regexp->fMatcher->useAnchoringBounds(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_hitEnd
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_hitEnd(const URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->hitEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_requireEnd
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UBool U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_requireEnd(const URegularExpression *regexp2,
|
2007-12-11 21:30:10 +00:00
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2007-12-11 21:30:10 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return regexp->fMatcher->requireEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 03:08:44 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setTimeLimit
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_setTimeLimit(URegularExpression *regexp2,
|
2008-02-20 03:08:44 +00:00
|
|
|
int32_t limit,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2008-02-20 03:08:44 +00:00
|
|
|
regexp->fMatcher->setTimeLimit(limit, *status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_getTimeLimit
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_getTimeLimit(const URegularExpression *regexp2,
|
2008-02-20 03:08:44 +00:00
|
|
|
UErrorCode *status) {
|
|
|
|
int32_t retVal = 0;
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2008-02-20 03:08:44 +00:00
|
|
|
retVal = regexp->fMatcher->getTimeLimit();
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setStackLimit
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_setStackLimit(URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
int32_t limit,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2008-02-20 03:08:44 +00:00
|
|
|
regexp->fMatcher->setStackLimit(limit, *status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_getStackLimit
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_getStackLimit(const URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
UErrorCode *status) {
|
2008-02-20 03:08:44 +00:00
|
|
|
int32_t retVal = 0;
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2008-02-20 03:08:44 +00:00
|
|
|
retVal = regexp->fMatcher->getStackLimit();
|
|
|
|
}
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setMatchCallback
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_setMatchCallback(URegularExpression *regexp2,
|
2008-02-26 08:40:20 +00:00
|
|
|
URegexMatchCallback *callback,
|
2008-02-20 03:08:44 +00:00
|
|
|
const void *context,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2010-02-03 02:59:35 +00:00
|
|
|
regexp->fMatcher->setMatchCallback(callback, context, *status);
|
2008-02-20 03:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_getMatchCallback
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_getMatchCallback(const URegularExpression *regexp2,
|
2008-02-26 08:40:20 +00:00
|
|
|
URegexMatchCallback **callback,
|
2008-02-20 03:08:44 +00:00
|
|
|
const void **context,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2008-02-20 03:08:44 +00:00
|
|
|
regexp->fMatcher->getMatchCallback(*callback, *context, *status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 20:19:10 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_setMatchProgressCallback
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_setFindProgressCallback(URegularExpression *regexp2,
|
|
|
|
URegexFindProgressCallback *callback,
|
|
|
|
const void *context,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2010-06-15 20:19:10 +00:00
|
|
|
regexp->fMatcher->setFindProgressCallback(callback, context, *status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_getMatchCallback
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_getFindProgressCallback(const URegularExpression *regexp2,
|
|
|
|
URegexFindProgressCallback **callback,
|
|
|
|
const void **context,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, FALSE, status)) {
|
2010-06-15 20:19:10 +00:00
|
|
|
regexp->fMatcher->getFindProgressCallback(*callback, *context, *status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_replaceAll
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_replaceAll(URegularExpression *regexp2,
|
2005-06-21 16:54:39 +00:00
|
|
|
const UChar *replacementText,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t replacementLength,
|
|
|
|
UChar *destBuf,
|
|
|
|
int32_t destCapacity,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (replacementText == NULL || replacementLength < -1 ||
|
2010-07-12 18:03:29 +00:00
|
|
|
(destBuf == NULL && destCapacity > 0) ||
|
2004-03-22 22:16:21 +00:00
|
|
|
destCapacity < 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t len = 0;
|
2009-01-08 06:55:22 +00:00
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_reset(regexp2, 0, status);
|
2009-01-08 06:55:22 +00:00
|
|
|
|
|
|
|
// Note: Seperate error code variables for findNext() and appendReplacement()
|
|
|
|
// are used so that destination buffer overflow errors
|
|
|
|
// in appendReplacement won't stop findNext() from working.
|
|
|
|
// appendReplacement() and appendTail() special case incoming buffer
|
|
|
|
// overflow errors, continuing to return the correct length.
|
|
|
|
UErrorCode findStatus = *status;
|
2009-10-01 22:52:39 +00:00
|
|
|
while (uregex_findNext(regexp2, &findStatus)) {
|
|
|
|
len += uregex_appendReplacement(regexp2, replacementText, replacementLength,
|
2004-03-22 22:16:21 +00:00
|
|
|
&destBuf, &destCapacity, status);
|
|
|
|
}
|
2009-10-01 22:52:39 +00:00
|
|
|
len += uregex_appendTail(regexp2, &destBuf, &destCapacity, status);
|
2009-01-08 06:55:22 +00:00
|
|
|
|
|
|
|
if (U_FAILURE(findStatus)) {
|
|
|
|
// If anything went wrong with the findNext(), make that error trump
|
|
|
|
// whatever may have happened with the append() operations.
|
|
|
|
// Errors in findNext() are not expected.
|
|
|
|
*status = findStatus;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
return len;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_replaceAllUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_replaceAllUText(URegularExpression *regexp2,
|
|
|
|
UText *replacementText,
|
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (replacementText == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = regexp->fMatcher->replaceAll(replacementText, dest, *status);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_replaceFirst
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_replaceFirst(URegularExpression *regexp2,
|
2005-06-21 16:54:39 +00:00
|
|
|
const UChar *replacementText,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t replacementLength,
|
|
|
|
UChar *destBuf,
|
|
|
|
int32_t destCapacity,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (replacementText == NULL || replacementLength < -1 ||
|
2010-07-12 18:03:29 +00:00
|
|
|
(destBuf == NULL && destCapacity > 0) ||
|
2004-03-22 22:16:21 +00:00
|
|
|
destCapacity < 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t len = 0;
|
2004-03-24 01:57:51 +00:00
|
|
|
UBool findSucceeded;
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_reset(regexp2, 0, status);
|
|
|
|
findSucceeded = uregex_find(regexp2, 0, status);
|
2004-03-24 01:57:51 +00:00
|
|
|
if (findSucceeded) {
|
2009-10-01 22:52:39 +00:00
|
|
|
len = uregex_appendReplacement(regexp2, replacementText, replacementLength,
|
2004-03-22 22:16:21 +00:00
|
|
|
&destBuf, &destCapacity, status);
|
|
|
|
}
|
2009-10-01 22:52:39 +00:00
|
|
|
len += uregex_appendTail(regexp2, &destBuf, &destCapacity, status);
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
return len;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// uregex_replaceFirstUText
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_replaceFirstUText(URegularExpression *regexp2,
|
|
|
|
UText *replacementText,
|
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (replacementText == NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = regexp->fMatcher->replaceFirst(replacementText, dest, *status);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_appendReplacement
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// Dummy class, because these functions need to be friends of class RegexMatcher,
|
|
|
|
// and stand-alone C functions don't work as friends
|
|
|
|
//
|
|
|
|
class RegexCImpl {
|
|
|
|
public:
|
2009-10-01 22:52:39 +00:00
|
|
|
inline static int32_t appendReplacement(RegularExpression *regexp,
|
2005-06-21 16:54:39 +00:00
|
|
|
const UChar *replacementText,
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t replacementLength,
|
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status);
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
inline static int32_t appendTail(RegularExpression *regexp,
|
2010-02-03 02:59:35 +00:00
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status);
|
|
|
|
|
|
|
|
inline static int32_t split(RegularExpression *regexp,
|
|
|
|
UChar *destBuf,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *requiredCapacity,
|
|
|
|
UChar *destFields[],
|
|
|
|
int32_t destFieldsCapacity,
|
|
|
|
UErrorCode *status);
|
2004-03-22 22:16:21 +00:00
|
|
|
};
|
2009-10-01 22:52:39 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
static const UChar BACKSLASH = 0x5c;
|
|
|
|
static const UChar DOLLARSIGN = 0x24;
|
|
|
|
|
2004-03-26 20:20:21 +00:00
|
|
|
//
|
|
|
|
// Move a character to an output buffer, with bounds checking on the index.
|
|
|
|
// Index advances even if capacity is exceeded, for preflight size computations.
|
|
|
|
// This little sequence is used a LOT.
|
|
|
|
//
|
|
|
|
static inline void appendToBuf(UChar c, int32_t *idx, UChar *buf, int32_t bufCapacity) {
|
|
|
|
if (*idx < bufCapacity) {
|
|
|
|
buf[*idx] = c;
|
|
|
|
}
|
|
|
|
(*idx)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// appendReplacement, the actual implementation.
|
|
|
|
//
|
2009-10-01 22:52:39 +00:00
|
|
|
int32_t RegexCImpl::appendReplacement(RegularExpression *regexp,
|
2010-02-03 02:59:35 +00:00
|
|
|
const UChar *replacementText,
|
|
|
|
int32_t replacementLength,
|
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status) {
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
// If we come in with a buffer overflow error, don't suppress the operation.
|
|
|
|
// A series of appendReplacements, appendTail need to correctly preflight
|
|
|
|
// the buffer size when an overflow happens somewhere in the middle.
|
|
|
|
UBool pendingBufferOverflow = FALSE;
|
2009-03-26 23:04:40 +00:00
|
|
|
if (*status == U_BUFFER_OVERFLOW_ERROR && destCapacity != NULL && *destCapacity == 0) {
|
2004-03-22 22:16:21 +00:00
|
|
|
pendingBufferOverflow = TRUE;
|
|
|
|
*status = U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Validate all paramters
|
|
|
|
//
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (replacementText == NULL || replacementLength < -1 ||
|
|
|
|
destCapacity == NULL || destBuf == NULL ||
|
2010-07-12 18:03:29 +00:00
|
|
|
(*destBuf == NULL && *destCapacity > 0) ||
|
2004-03-22 22:16:21 +00:00
|
|
|
*destCapacity < 0) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegexMatcher *m = regexp->fMatcher;
|
|
|
|
if (m->fMatch == FALSE) {
|
|
|
|
*status = U_REGEX_INVALID_STATE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:20:21 +00:00
|
|
|
UChar *dest = *destBuf;
|
|
|
|
int32_t capacity = *destCapacity;
|
|
|
|
int32_t destIdx = 0;
|
|
|
|
int32_t i;
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
// If it wasn't supplied by the caller, get the length of the replacement text.
|
|
|
|
// TODO: slightly smarter logic in the copy loop could watch for the NUL on
|
|
|
|
// the fly and avoid this step.
|
|
|
|
if (replacementLength == -1) {
|
|
|
|
replacementLength = u_strlen(replacementText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy input string from the end of previous match to start of current match
|
2010-02-03 02:59:35 +00:00
|
|
|
if (regexp->fText != NULL) {
|
|
|
|
int32_t matchStart;
|
|
|
|
int32_t lastMatchEnd;
|
|
|
|
if (UTEXT_USES_U16(m->fInputText)) {
|
2010-02-27 07:43:07 +00:00
|
|
|
lastMatchEnd = (int32_t)m->fLastMatchEnd;
|
|
|
|
matchStart = (int32_t)m->fMatchStart;
|
2010-02-03 02:59:35 +00:00
|
|
|
} else {
|
|
|
|
// !!!: Would like a better way to do this!
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
lastMatchEnd = utext_extract(m->fInputText, 0, m->fLastMatchEnd, NULL, 0, &status);
|
|
|
|
status = U_ZERO_ERROR;
|
|
|
|
matchStart = lastMatchEnd + utext_extract(m->fInputText, m->fLastMatchEnd, m->fMatchStart, NULL, 0, &status);
|
|
|
|
}
|
|
|
|
for (i=lastMatchEnd; i<matchStart; i++) {
|
|
|
|
appendToBuf(regexp->fText[i], &destIdx, dest, capacity);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UErrorCode possibleOverflowError = U_ZERO_ERROR; // ignore
|
|
|
|
destIdx += utext_extract(m->fInputText, m->fLastMatchEnd, m->fMatchStart,
|
|
|
|
&dest[destIdx], REMAINING_CAPACITY(destIdx, capacity), &possibleOverflowError);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// scan the replacement text, looking for substitutions ($n) and \escapes.
|
|
|
|
int32_t replIdx = 0;
|
|
|
|
while (replIdx < replacementLength) {
|
|
|
|
UChar c = replacementText[replIdx];
|
|
|
|
replIdx++;
|
|
|
|
if (c != DOLLARSIGN && c != BACKSLASH) {
|
|
|
|
// Common case, no substitution, no escaping,
|
|
|
|
// just copy the char to the dest buf.
|
2004-03-26 20:20:21 +00:00
|
|
|
appendToBuf(c, &destIdx, dest, capacity);
|
2004-03-22 22:16:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == BACKSLASH) {
|
|
|
|
// Backslash Escape. Copy the following char out without further checks.
|
|
|
|
// Note: Surrogate pairs don't need any special handling
|
|
|
|
// The second half wont be a '$' or a '\', and
|
|
|
|
// will move to the dest normally on the next
|
|
|
|
// loop iteration.
|
|
|
|
if (replIdx >= replacementLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c = replacementText[replIdx];
|
|
|
|
|
|
|
|
if (c==0x55/*U*/ || c==0x75/*u*/) {
|
|
|
|
// We have a \udddd or \Udddddddd escape sequence.
|
|
|
|
UChar32 escapedChar =
|
2010-02-03 02:59:35 +00:00
|
|
|
u_unescapeAt(uregex_ucstr_unescape_charAt,
|
2004-03-22 22:16:21 +00:00
|
|
|
&replIdx, // Index is updated by unescapeAt
|
2004-03-26 20:20:21 +00:00
|
|
|
replacementLength, // Length of replacement text
|
2005-06-21 16:54:39 +00:00
|
|
|
(void *)replacementText);
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
if (escapedChar != (UChar32)0xFFFFFFFF) {
|
2004-03-26 01:23:01 +00:00
|
|
|
if (escapedChar <= 0xffff) {
|
2004-03-26 20:20:21 +00:00
|
|
|
appendToBuf((UChar)escapedChar, &destIdx, dest, capacity);
|
2004-03-26 01:23:01 +00:00
|
|
|
} else {
|
2004-03-26 20:20:21 +00:00
|
|
|
appendToBuf(U16_LEAD(escapedChar), &destIdx, dest, capacity);
|
|
|
|
appendToBuf(U16_TRAIL(escapedChar), &destIdx, dest, capacity);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
2004-03-26 20:20:21 +00:00
|
|
|
continue;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
// Note: if the \u escape was invalid, just fall through and
|
|
|
|
// treat it as a plain \<anything> escape.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Plain backslash escape. Just put out the escaped character.
|
2004-03-26 20:20:21 +00:00
|
|
|
appendToBuf(c, &destIdx, dest, capacity);
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
replIdx++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We've got a $. Pick up a capture group number if one follows.
|
|
|
|
// Consume at most the number of digits necessary for the largest capture
|
|
|
|
// number that is valid for this pattern.
|
|
|
|
|
|
|
|
int32_t numDigits = 0;
|
|
|
|
int32_t groupNum = 0;
|
|
|
|
UChar32 digitC;
|
|
|
|
for (;;) {
|
|
|
|
if (replIdx >= replacementLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
U16_GET(replacementText, 0, replIdx, replacementLength, digitC);
|
|
|
|
if (u_isdigit(digitC) == FALSE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
U16_FWD_1(replacementText, replIdx, replacementLength);
|
|
|
|
groupNum=groupNum*10 + u_charDigitValue(digitC);
|
|
|
|
numDigits++;
|
|
|
|
if (numDigits >= m->fPattern->fMaxCaptureDigits) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (numDigits == 0) {
|
|
|
|
// The $ didn't introduce a group number at all.
|
|
|
|
// Treat it as just part of the substitution text.
|
2004-03-26 20:20:21 +00:00
|
|
|
appendToBuf(DOLLARSIGN, &destIdx, dest, capacity);
|
2004-03-22 22:16:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, append the capture group data to the destination.
|
2010-02-03 02:59:35 +00:00
|
|
|
destIdx += uregex_group((URegularExpression*)regexp, groupNum, &dest[destIdx], REMAINING_CAPACITY(destIdx, capacity), status);
|
2004-03-22 22:16:21 +00:00
|
|
|
if (*status == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
// Ignore buffer overflow when extracting the group. We need to
|
|
|
|
// continue on to get full size of the untruncated result. We will
|
|
|
|
// raise our own buffer overflow error at the end.
|
|
|
|
*status = U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
// Can fail if group number is out of range.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Nul Terminate the dest buffer if possible.
|
|
|
|
// Set the appropriate buffer overflow or not terminated error, if needed.
|
|
|
|
//
|
2004-03-26 20:20:21 +00:00
|
|
|
if (destIdx < capacity) {
|
|
|
|
dest[destIdx] = 0;
|
|
|
|
} else if (destIdx == *destCapacity) {
|
2004-03-22 22:16:21 +00:00
|
|
|
*status = U_STRING_NOT_TERMINATED_WARNING;
|
|
|
|
} else {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Return an updated dest buffer and capacity to the caller.
|
|
|
|
//
|
2004-03-26 20:20:21 +00:00
|
|
|
if (destIdx > 0 && *destCapacity > 0) {
|
|
|
|
if (destIdx < capacity) {
|
|
|
|
*destBuf += destIdx;
|
|
|
|
*destCapacity -= destIdx;
|
2004-03-22 22:16:21 +00:00
|
|
|
} else {
|
2004-03-26 20:20:21 +00:00
|
|
|
*destBuf += capacity;
|
|
|
|
*destCapacity = 0;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we came in with a buffer overflow, make sure we go out with one also.
|
|
|
|
// (A zero length match right at the end of the previous match could
|
|
|
|
// make this function succeed even though a previous call had overflowed the buf)
|
|
|
|
if (pendingBufferOverflow && U_SUCCESS(*status)) {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-03-26 20:20:21 +00:00
|
|
|
return destIdx;
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2010-02-03 02:59:35 +00:00
|
|
|
// appendReplacement the actual API function,
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_appendReplacement(URegularExpression *regexp2,
|
2010-02-03 02:59:35 +00:00
|
|
|
const UChar *replacementText,
|
|
|
|
int32_t replacementLength,
|
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status) {
|
|
|
|
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2004-03-22 22:16:21 +00:00
|
|
|
return RegexCImpl::appendReplacement(
|
|
|
|
regexp, replacementText, replacementLength,destBuf, destCapacity, status);
|
|
|
|
}
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//
|
|
|
|
// uregex_appendReplacementUText...can just use the normal C++ method
|
|
|
|
//
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
uregex_appendReplacementUText(URegularExpression *regexp2,
|
|
|
|
UText *replText,
|
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
|
|
|
regexp->fMatcher->appendReplacement(dest, replText, *status);
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_appendTail
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2009-10-01 22:52:39 +00:00
|
|
|
int32_t RegexCImpl::appendTail(RegularExpression *regexp,
|
2010-02-03 02:59:35 +00:00
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status)
|
2008-02-14 04:39:06 +00:00
|
|
|
{
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
// If we come in with a buffer overflow error, don't suppress the operation.
|
|
|
|
// A series of appendReplacements, appendTail need to correctly preflight
|
|
|
|
// the buffer size when an overflow happens somewhere in the middle.
|
|
|
|
UBool pendingBufferOverflow = FALSE;
|
2009-01-08 06:55:22 +00:00
|
|
|
if (*status == U_BUFFER_OVERFLOW_ERROR && destCapacity != NULL && *destCapacity == 0) {
|
2004-03-22 22:16:21 +00:00
|
|
|
pendingBufferOverflow = TRUE;
|
|
|
|
*status = U_ZERO_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2004-03-22 22:16:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2009-01-08 06:55:22 +00:00
|
|
|
|
|
|
|
if (destCapacity == NULL || destBuf == NULL ||
|
2010-07-12 18:03:29 +00:00
|
|
|
(*destBuf == NULL && *destCapacity > 0) ||
|
2009-01-08 06:55:22 +00:00
|
|
|
*destCapacity < 0)
|
|
|
|
{
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
RegexMatcher *m = regexp->fMatcher;
|
2004-03-24 01:57:51 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
int32_t destIdx = 0;
|
|
|
|
int32_t destCap = *destCapacity;
|
|
|
|
UChar *dest = *destBuf;
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
if (regexp->fText != NULL) {
|
|
|
|
int32_t srcIdx;
|
|
|
|
int64_t nativeIdx = (m->fMatch ? m->fMatchEnd : m->fLastMatchEnd);
|
|
|
|
if (nativeIdx == -1) {
|
|
|
|
srcIdx = 0;
|
|
|
|
} else if (UTEXT_USES_U16(m->fInputText)) {
|
2010-02-27 07:43:07 +00:00
|
|
|
srcIdx = (int32_t)nativeIdx;
|
2004-03-22 22:16:21 +00:00
|
|
|
} else {
|
2010-02-03 02:59:35 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
srcIdx = utext_extract(m->fInputText, 0, nativeIdx, NULL, 0, &status);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (srcIdx == regexp->fTextLength) {
|
2004-03-22 22:16:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
UChar c = regexp->fText[srcIdx];
|
|
|
|
if (c == 0 && regexp->fTextLength == -1) {
|
|
|
|
regexp->fTextLength = srcIdx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (destIdx < destCap) {
|
|
|
|
dest[destIdx] = c;
|
|
|
|
} else {
|
|
|
|
// We've overflowed the dest buffer.
|
|
|
|
// If the total input string length is known, we can
|
|
|
|
// compute the total buffer size needed without scanning through the string.
|
|
|
|
if (regexp->fTextLength > 0) {
|
|
|
|
destIdx += (regexp->fTextLength - srcIdx);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
srcIdx++;
|
|
|
|
destIdx++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int64_t srcIdx;
|
|
|
|
if (m->fMatch) {
|
|
|
|
// The most recent call to find() succeeded.
|
|
|
|
srcIdx = m->fMatchEnd;
|
|
|
|
} else {
|
|
|
|
// The last call to find() on this matcher failed().
|
|
|
|
// Look back to the end of the last find() that succeeded for src index.
|
|
|
|
srcIdx = m->fLastMatchEnd;
|
|
|
|
if (srcIdx == -1) {
|
|
|
|
// There has been no successful match with this matcher.
|
|
|
|
// We want to copy the whole string.
|
|
|
|
srcIdx = 0;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
destIdx = utext_extract(m->fInputText, srcIdx, m->fInputLength, dest, destCap, status);
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// NUL terminate the output string, if possible, otherwise issue the
|
|
|
|
// appropriate error or warning.
|
|
|
|
//
|
|
|
|
if (destIdx < destCap) {
|
|
|
|
dest[destIdx] = 0;
|
|
|
|
} else if (destIdx == destCap) {
|
|
|
|
*status = U_STRING_NOT_TERMINATED_WARNING;
|
|
|
|
} else {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Update the user's buffer ptr and capacity vars to reflect the
|
|
|
|
// amount used.
|
|
|
|
//
|
|
|
|
if (destIdx < destCap) {
|
|
|
|
*destBuf += destIdx;
|
|
|
|
*destCapacity -= destIdx;
|
|
|
|
} else {
|
|
|
|
*destBuf += destCap;
|
|
|
|
*destCapacity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingBufferOverflow && U_SUCCESS(*status)) {
|
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return destIdx;
|
2004-12-30 07:25:51 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//
|
|
|
|
// appendTail the actual API function
|
|
|
|
//
|
2004-03-22 22:16:21 +00:00
|
|
|
U_CAPI int32_t U_EXPORT2
|
2009-10-01 22:52:39 +00:00
|
|
|
uregex_appendTail(URegularExpression *regexp2,
|
2004-03-22 22:16:21 +00:00
|
|
|
UChar **destBuf,
|
|
|
|
int32_t *destCapacity,
|
|
|
|
UErrorCode *status) {
|
2009-10-01 22:52:39 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2004-03-22 22:16:21 +00:00
|
|
|
return RegexCImpl::appendTail(regexp, destBuf, destCapacity, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//
|
|
|
|
// uregex_appendTailUText...can just use the normal C++ method
|
|
|
|
//
|
|
|
|
U_CAPI UText * U_EXPORT2
|
|
|
|
uregex_appendTailUText(URegularExpression *regexp2,
|
2010-09-18 03:07:17 +00:00
|
|
|
UText *dest,
|
|
|
|
UErrorCode *status) {
|
2010-02-03 02:59:35 +00:00
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2010-09-18 03:07:17 +00:00
|
|
|
return regexp->fMatcher->appendTail(dest, *status);
|
2010-02-03 02:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// copyString Internal utility to copy a string to an output buffer,
|
|
|
|
// while managing buffer overflow and preflight size
|
|
|
|
// computation. NUL termination is added to destination,
|
|
|
|
// and the NUL is counted in the output size.
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2010-02-09 20:17:52 +00:00
|
|
|
#if 0
|
2004-03-22 22:16:21 +00:00
|
|
|
static void copyString(UChar *destBuffer, // Destination buffer.
|
|
|
|
int32_t destCapacity, // Total capacity of dest buffer
|
|
|
|
int32_t *destIndex, // Index into dest buffer. Updated on return.
|
|
|
|
// Update not clipped to destCapacity.
|
|
|
|
const UChar *srcPtr, // Pointer to source string
|
|
|
|
int32_t srcLen) // Source string len.
|
|
|
|
{
|
|
|
|
int32_t si;
|
|
|
|
int32_t di = *destIndex;
|
|
|
|
UChar c;
|
|
|
|
|
2004-03-26 01:23:01 +00:00
|
|
|
for (si=0; si<srcLen; si++) {
|
2004-03-22 22:16:21 +00:00
|
|
|
c = srcPtr[si];
|
|
|
|
if (di < destCapacity) {
|
|
|
|
destBuffer[di] = c;
|
|
|
|
di++;
|
|
|
|
} else {
|
|
|
|
di += srcLen - si;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-08-31 00:52:22 +00:00
|
|
|
if (di<destCapacity) {
|
|
|
|
destBuffer[di] = 0;
|
|
|
|
}
|
|
|
|
di++;
|
2004-03-22 22:16:21 +00:00
|
|
|
*destIndex = di;
|
|
|
|
}
|
2010-02-09 20:17:52 +00:00
|
|
|
#endif
|
2004-03-22 22:16:21 +00:00
|
|
|
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// uregex_split
|
|
|
|
//
|
2004-12-30 07:25:51 +00:00
|
|
|
//------------------------------------------------------------------------------
|
2010-02-03 02:59:35 +00:00
|
|
|
int32_t RegexCImpl::split(RegularExpression *regexp,
|
|
|
|
UChar *destBuf,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *requiredCapacity,
|
|
|
|
UChar *destFields[],
|
|
|
|
int32_t destFieldsCapacity,
|
|
|
|
UErrorCode *status) {
|
2004-03-22 22:16:21 +00:00
|
|
|
//
|
|
|
|
// Reset for the input text
|
|
|
|
//
|
|
|
|
regexp->fMatcher->reset();
|
2010-02-03 02:59:35 +00:00
|
|
|
UText *inputText = regexp->fMatcher->fInputText;
|
|
|
|
int64_t nextOutputStringStart = 0;
|
|
|
|
int64_t inputLen = regexp->fMatcher->fInputLength;
|
2004-03-22 22:16:21 +00:00
|
|
|
if (inputLen == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Loop through the input text, searching for the delimiter pattern
|
|
|
|
//
|
|
|
|
int32_t i; // Index of the field being processed.
|
|
|
|
int32_t destIdx = 0; // Next available position in destBuf;
|
|
|
|
int32_t numCaptureGroups = regexp->fMatcher->groupCount();
|
2010-02-03 02:59:35 +00:00
|
|
|
UErrorCode tStatus = U_ZERO_ERROR; // Want to ignore any buffer overflow errors so that the strings are still counted
|
2004-03-22 22:16:21 +00:00
|
|
|
for (i=0; ; i++) {
|
|
|
|
if (i>=destFieldsCapacity-1) {
|
2010-02-03 02:59:35 +00:00
|
|
|
// There are one or zero output strings left.
|
2004-03-22 22:16:21 +00:00
|
|
|
// Fill the last output string with whatever is left from the input, then exit the loop.
|
|
|
|
// ( i will be == destFieldsCapacity if we filled the output array while processing
|
|
|
|
// capture groups of the delimiter expression, in which case we will discard the
|
|
|
|
// last capture group saved in favor of the unprocessed remainder of the
|
|
|
|
// input string.)
|
2010-02-03 02:59:35 +00:00
|
|
|
if (inputLen > nextOutputStringStart) {
|
|
|
|
if (i != destFieldsCapacity-1) {
|
|
|
|
// No fields are left. Recycle the last one for holding the trailing part of
|
|
|
|
// the input string.
|
|
|
|
i = destFieldsCapacity-1;
|
|
|
|
destIdx = (int32_t)(destFields[i] - destFields[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
destFields[i] = &destBuf[destIdx];
|
|
|
|
destIdx += 1 + utext_extract(inputText, nextOutputStringStart, inputLen,
|
|
|
|
&destBuf[destIdx], REMAINING_CAPACITY(destIdx, destCapacity), status);
|
2004-03-26 01:23:01 +00:00
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-03-26 01:23:01 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
if (regexp->fMatcher->find()) {
|
|
|
|
// We found another delimiter. Move everything from where we started looking
|
|
|
|
// up until the start of the delimiter into the next output string.
|
|
|
|
destFields[i] = &destBuf[destIdx];
|
2010-02-03 02:59:35 +00:00
|
|
|
|
|
|
|
destIdx += 1 + utext_extract(inputText, nextOutputStringStart, regexp->fMatcher->fMatchStart,
|
|
|
|
&destBuf[destIdx], REMAINING_CAPACITY(destIdx, destCapacity), &tStatus);
|
|
|
|
if (tStatus == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
tStatus = U_ZERO_ERROR;
|
|
|
|
} else {
|
|
|
|
*status = tStatus;
|
|
|
|
}
|
|
|
|
nextOutputStringStart = regexp->fMatcher->fMatchEnd;
|
2004-03-26 01:23:01 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
// If the delimiter pattern has capturing parentheses, the captured
|
|
|
|
// text goes out into the next n destination strings.
|
|
|
|
int32_t groupNum;
|
|
|
|
for (groupNum=1; groupNum<=numCaptureGroups; groupNum++) {
|
|
|
|
// If we've run out of output string slots, bail out.
|
|
|
|
if (i==destFieldsCapacity-1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
2004-03-26 01:23:01 +00:00
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
// Set up to extract the capture group contents into the dest buffer.
|
|
|
|
destFields[i] = &destBuf[destIdx];
|
2010-02-03 02:59:35 +00:00
|
|
|
tStatus = U_ZERO_ERROR;
|
2011-04-15 00:48:39 +00:00
|
|
|
int32_t t = uregex_group((URegularExpression*)regexp,
|
|
|
|
groupNum,
|
|
|
|
destFields[i],
|
|
|
|
REMAINING_CAPACITY(destIdx, destCapacity),
|
|
|
|
&tStatus);
|
2004-03-22 22:16:21 +00:00
|
|
|
destIdx += t + 1; // Record the space used in the output string buffer.
|
|
|
|
// +1 for the NUL that terminates the string.
|
2010-02-03 02:59:35 +00:00
|
|
|
if (tStatus == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
tStatus = U_ZERO_ERROR;
|
|
|
|
} else {
|
|
|
|
*status = tStatus;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nextOutputStringStart == inputLen) {
|
2011-04-15 00:48:39 +00:00
|
|
|
// The delimiter was at the end of the string.
|
|
|
|
// Output an empty string, and then we are done.
|
|
|
|
if (destIdx < destCapacity) {
|
|
|
|
destBuf[destIdx] = 0;
|
|
|
|
}
|
|
|
|
if (i < destFieldsCapacity-1) {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
if (destIdx < destCapacity) {
|
|
|
|
destFields[i] = destBuf + destIdx;
|
|
|
|
}
|
|
|
|
++destIdx;
|
2004-03-22 22:16:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We ran off the end of the input while looking for the next delimiter.
|
|
|
|
// All the remaining text goes into the current output string.
|
|
|
|
destFields[i] = &destBuf[destIdx];
|
2010-02-03 02:59:35 +00:00
|
|
|
destIdx += 1 + utext_extract(inputText, nextOutputStringStart, inputLen,
|
|
|
|
&destBuf[destIdx], REMAINING_CAPACITY(destIdx, destCapacity), status);
|
2004-03-22 22:16:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-03-24 18:34:36 +00:00
|
|
|
|
|
|
|
// Zero out any unused portion of the destFields array
|
|
|
|
int j;
|
|
|
|
for (j=i+1; j<destFieldsCapacity; j++) {
|
|
|
|
destFields[j] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requiredCapacity != NULL) {
|
|
|
|
*requiredCapacity = destIdx;
|
|
|
|
}
|
2005-08-02 21:51:56 +00:00
|
|
|
if (destIdx > destCapacity) {
|
2004-03-26 01:23:01 +00:00
|
|
|
*status = U_BUFFER_OVERFLOW_ERROR;
|
|
|
|
}
|
2004-03-22 22:16:21 +00:00
|
|
|
return i+1;
|
|
|
|
}
|
|
|
|
|
2010-02-03 02:59:35 +00:00
|
|
|
//
|
|
|
|
// uregex_split The actual API function
|
|
|
|
//
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uregex_split(URegularExpression *regexp2,
|
|
|
|
UChar *destBuf,
|
|
|
|
int32_t destCapacity,
|
|
|
|
int32_t *requiredCapacity,
|
|
|
|
UChar *destFields[],
|
|
|
|
int32_t destFieldsCapacity,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
2011-03-31 18:33:28 +00:00
|
|
|
if (validateRE(regexp, TRUE, status) == FALSE) {
|
2010-02-03 02:59:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-07-12 18:03:29 +00:00
|
|
|
if ((destBuf == NULL && destCapacity > 0) ||
|
2010-02-03 02:59:35 +00:00
|
|
|
destCapacity < 0 ||
|
|
|
|
destFields == NULL ||
|
|
|
|
destFieldsCapacity < 1 ) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RegexCImpl::split(regexp, destBuf, destCapacity, requiredCapacity, destFields, destFieldsCapacity, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// uregex_splitUText...can just use the normal C++ method
|
|
|
|
//
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
|
|
|
uregex_splitUText(URegularExpression *regexp2,
|
|
|
|
UText *destFields[],
|
|
|
|
int32_t destFieldsCapacity,
|
|
|
|
UErrorCode *status) {
|
|
|
|
RegularExpression *regexp = (RegularExpression*)regexp2;
|
|
|
|
return regexp->fMatcher->split(regexp->fMatcher->inputText(), destFields, destFieldsCapacity, *status);
|
|
|
|
}
|
|
|
|
|
2004-03-22 22:16:21 +00:00
|
|
|
|
|
|
|
#endif // !UCONFIG_NO_REGULAR_EXPRESSIONS
|
2004-12-30 07:25:51 +00:00
|
|
|
|