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

U_INTERNAL URegularExpression* U_EXPORT2 uregex_openUText ( UText pattern,
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.

The contents of the pattern UText will be extracted and saved. Ownership of the UText struct itself remains with the caller. This is to match the behavior of uregex_open().

Parameters:
pattern The Regular Expression pattern to be compiled.
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.

For internal use only.

ICU 4.4 technology preview

Definition at line 184 of file uregex.cpp.

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

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


Generated by  Doxygen 1.6.0   Back to index