Logo Search packages:      
Sourcecode: icu version File versions  Download package

U_STABLE URegularExpression* U_EXPORT2 uregex_open ( const UChar *  pattern,
int32_t  patternLength,
uint32_t  flags,
UParseError pe,
UErrorCode status 
)

Open (compile) an ICU regular expression. Compiles the regular expression in string form into an internal representation using the specified match mode flags. The resulting regular expression handle can then be used to perform various matching operations.

Parameters:
pattern The Regular Expression pattern to be compiled.
patternLength The length of the pattern, or -1 if the pattern is NUL termintated.
flags Flags that alter the default matching behavior for the regular expression, UREGEX_CASE_INSENSITIVE, for example. For default behavior, set this parameter to zero. See enum URegexpFlag. All desired flags are bitwise-ORed together.
pe Receives the position (line and column nubers) of any syntax error within the source regular expression string. If this information is not wanted, pass NULL for this parameter.
status Receives error detected by this function. ICU 3.0

Definition at line 106 of file uregex.cpp.

References RegexPattern::compile(), NULL, U_FAILURE, U_ILLEGAL_ARGUMENT_ERROR, U_MEMORY_ALLOCATION_ERROR, u_strlen(), U_SUCCESS, and UTEXT_INITIALIZER.

                                            {

    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);
    }

    RegularExpression *re     = new RegularExpression;
    int32_t            *refC   = (int32_t *)uprv_malloc(sizeof(int32_t));
    UChar              *patBuf = (UChar *)uprv_malloc(sizeof(UChar)*(actualPatLen+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 UText wrapper around
    //    this local copy, to avoid making even more copies.
    //
    re->fPatString    = patBuf;
    re->fPatStringLen = patternLength;
    u_memcpy(patBuf, pattern, actualPatLen);
    patBuf[actualPatLen] = 0;
    
    UText patText = UTEXT_INITIALIZER;
    utext_openUChars(&patText, patBuf, patternLength, 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;

}


Generated by  Doxygen 1.6.0   Back to index