// © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * * Copyright (C) 2001-2015, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* * file name: ustrcase.cpp * encoding: UTF-8 * tab size: 8 (not used) * indentation:4 * * created on: 2002feb20 * created by: Markus W. Scherer * * Implementation file for string casing C API functions. * Uses functions from uchar.c for basic functionality that requires access * to the Unicode Character Database (uprops.dat). */ #include "unicode/utypes.h" #include "unicode/brkiter.h" #include "unicode/casemap.h" #include "unicode/edits.h" #include "unicode/stringoptions.h" #include "unicode/ustring.h" #include "unicode/ucasemap.h" #include "unicode/ubrk.h" #include "unicode/utf.h" #include "unicode/utf16.h" #include "cmemory.h" #include "ucase.h" #include "ucasemap_imp.h" #include "ustr_imp.h" #include "uassert.h" /** * Code point for COMBINING ACUTE ACCENT * @internal */ #define ACUTE u'\u0301' U_NAMESPACE_BEGIN namespace { int32_t checkOverflowAndEditsError(int32_t destIndex, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { if (U_SUCCESS(errorCode)) { if (destIndex > destCapacity) { errorCode = U_BUFFER_OVERFLOW_ERROR; } else if (edits != nullptr) { edits->copyErrorTo(errorCode); } } return destIndex; } /* Appends a full case mapping result, see UCASE_MAX_STRING_LENGTH. */ inline int32_t appendResult(char16_t *dest, int32_t destIndex, int32_t destCapacity, int32_t result, const char16_t *s, int32_t cpLength, uint32_t options, icu::Edits *edits) { UChar32 c; int32_t length; /* decode the result */ if(result<0) { /* (not) original code point */ if(edits!=nullptr) { edits->addUnchanged(cpLength); } if(options & U_OMIT_UNCHANGED_TEXT) { return destIndex; } c=~result; if(destIndexaddReplace(cpLength, 1); } return destIndex; } else { c=result; length=U16_LENGTH(c); } if(edits!=nullptr) { edits->addReplace(cpLength, length); } } if(length>(INT32_MAX-destIndex)) { return -1; // integer overflow } if(destIndex=0) { /* code point */ UBool isError=false; U16_APPEND(dest, destIndex, destCapacity, c, isError); if(isError) { /* overflow, nothing written */ destIndex+=length; } } else { /* string */ if((destIndex+length)<=destCapacity) { while(length>0) { dest[destIndex++]=*s++; --length; } } else { /* overflow */ destIndex+=length; } } } else { /* preflight */ destIndex+=length; } return destIndex; } inline int32_t appendUChar(char16_t *dest, int32_t destIndex, int32_t destCapacity, char16_t c) { if(destIndexaddUnchanged(length); } if(options & U_OMIT_UNCHANGED_TEXT) { return destIndex; } if(length>(INT32_MAX-destIndex)) { return -1; // integer overflow } if((destIndex+length)<=destCapacity) { u_memcpy(dest+destIndex, s, length); } return destIndex + length; } inline int32_t appendUnchanged(char16_t *dest, int32_t destIndex, int32_t destCapacity, const char16_t *s, int32_t length, uint32_t options, icu::Edits *edits) { if (length <= 0) { return destIndex; } return appendNonEmptyUnchanged(dest, destIndex, destCapacity, s, length, options, edits); } UChar32 U_CALLCONV utf16_caseContextIterator(void *context, int8_t dir) { UCaseContext *csc=(UCaseContext *)context; UChar32 c; if(dir<0) { /* reset for backward iteration */ csc->index=csc->cpStart; csc->dir=dir; } else if(dir>0) { /* reset for forward iteration */ csc->index=csc->cpLimit; csc->dir=dir; } else { /* continue current iteration direction */ dir=csc->dir; } if(dir<0) { if(csc->startindex) { U16_PREV((const char16_t *)csc->p, csc->start, csc->index, c); return c; } } else { if(csc->indexlimit) { U16_NEXT((const char16_t *)csc->p, csc->index, csc->limit, c); return c; } } return U_SENTINEL; } /** * caseLocale >= 0: Lowercases [srcStart..srcLimit[ but takes context [0..srcLength[ into account. * caseLocale < 0: Case-folds [srcStart..srcLimit[. */ int32_t toLower(int32_t caseLocale, uint32_t options, char16_t *dest, int32_t destCapacity, const char16_t *src, UCaseContext *csc, int32_t srcStart, int32_t srcLimit, icu::Edits *edits, UErrorCode &errorCode) { const int8_t *latinToLower; if (caseLocale == UCASE_LOC_ROOT || (caseLocale >= 0 ? !(caseLocale == UCASE_LOC_TURKISH || caseLocale == UCASE_LOC_LITHUANIAN) : (options & _FOLD_CASE_OPTIONS_MASK) == U_FOLD_CASE_DEFAULT)) { latinToLower = LatinCase::TO_LOWER_NORMAL; } else { latinToLower = LatinCase::TO_LOWER_TR_LT; } const UTrie2 *trie = ucase_getTrie(); int32_t destIndex = 0; int32_t prev = srcStart; int32_t srcIndex = srcStart; for (;;) { // fast path for simple cases char16_t lead = 0; while (srcIndex < srcLimit) { lead = src[srcIndex]; int32_t delta; if (lead < LatinCase::LONG_S) { int8_t d = latinToLower[lead]; if (d == LatinCase::EXC) { break; } ++srcIndex; if (d == 0) { continue; } delta = d; } else if (lead >= 0xd800) { break; // surrogate or higher } else { uint16_t props = UTRIE2_GET16_FROM_U16_SINGLE_LEAD(trie, lead); if (UCASE_HAS_EXCEPTION(props)) { break; } ++srcIndex; if (!UCASE_IS_UPPER_OR_TITLE(props) || (delta = UCASE_GET_DELTA(props)) == 0) { continue; } } lead += static_cast(delta); destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, srcIndex - 1 - prev, options, edits); if (destIndex >= 0) { destIndex = appendUChar(dest, destIndex, destCapacity, lead); if (edits != nullptr) { edits->addReplace(1, 1); } } if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } prev = srcIndex; } if (srcIndex >= srcLimit) { break; } // slow path int32_t cpStart = srcIndex++; char16_t trail; UChar32 c; if (U16_IS_LEAD(lead) && srcIndex < srcLimit && U16_IS_TRAIL(trail = src[srcIndex])) { c = U16_GET_SUPPLEMENTARY(lead, trail); ++srcIndex; } else { c = lead; } const char16_t *s; if (caseLocale >= 0) { csc->cpStart = cpStart; csc->cpLimit = srcIndex; c = ucase_toFullLower(c, utf16_caseContextIterator, csc, &s, caseLocale); } else { c = ucase_toFullFolding(c, &s, options); } if (c >= 0) { destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, cpStart - prev, options, edits); if (destIndex >= 0) { destIndex = appendResult(dest, destIndex, destCapacity, c, s, srcIndex - cpStart, options, edits); } if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } prev = srcIndex; } } destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, srcIndex - prev, options, edits); if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } return destIndex; } int32_t toUpper(int32_t caseLocale, uint32_t options, char16_t *dest, int32_t destCapacity, const char16_t *src, UCaseContext *csc, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode) { const int8_t *latinToUpper; if (caseLocale == UCASE_LOC_TURKISH) { latinToUpper = LatinCase::TO_UPPER_TR; } else { latinToUpper = LatinCase::TO_UPPER_NORMAL; } const UTrie2 *trie = ucase_getTrie(); int32_t destIndex = 0; int32_t prev = 0; int32_t srcIndex = 0; for (;;) { // fast path for simple cases char16_t lead = 0; while (srcIndex < srcLength) { lead = src[srcIndex]; int32_t delta; if (lead < LatinCase::LONG_S) { int8_t d = latinToUpper[lead]; if (d == LatinCase::EXC) { break; } ++srcIndex; if (d == 0) { continue; } delta = d; } else if (lead >= 0xd800) { break; // surrogate or higher } else { uint16_t props = UTRIE2_GET16_FROM_U16_SINGLE_LEAD(trie, lead); if (UCASE_HAS_EXCEPTION(props)) { break; } ++srcIndex; if (UCASE_GET_TYPE(props) != UCASE_LOWER || (delta = UCASE_GET_DELTA(props)) == 0) { continue; } } lead += static_cast(delta); destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, srcIndex - 1 - prev, options, edits); if (destIndex >= 0) { destIndex = appendUChar(dest, destIndex, destCapacity, lead); if (edits != nullptr) { edits->addReplace(1, 1); } } if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } prev = srcIndex; } if (srcIndex >= srcLength) { break; } // slow path int32_t cpStart; csc->cpStart = cpStart = srcIndex++; char16_t trail; UChar32 c; if (U16_IS_LEAD(lead) && srcIndex < srcLength && U16_IS_TRAIL(trail = src[srcIndex])) { c = U16_GET_SUPPLEMENTARY(lead, trail); ++srcIndex; } else { c = lead; } csc->cpLimit = srcIndex; const char16_t *s; c = ucase_toFullUpper(c, utf16_caseContextIterator, csc, &s, caseLocale); if (c >= 0) { destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, cpStart - prev, options, edits); if (destIndex >= 0) { destIndex = appendResult(dest, destIndex, destCapacity, c, s, srcIndex - cpStart, options, edits); } if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } prev = srcIndex; } } destIndex = appendUnchanged(dest, destIndex, destCapacity, src + prev, srcIndex - prev, options, edits); if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } return destIndex; } } // namespace U_NAMESPACE_END U_NAMESPACE_USE #if !UCONFIG_NO_BREAK_ITERATION namespace { /** * Input: c is a letter I with or without acute accent. * start is the index in src after c, and is less than segmentLimit. * If a plain i/I is followed by a plain j/J, * or an i/I with acute (precomposed or decomposed) is followed by a j/J with acute, * then we output accordingly. * * @return the src index after the titlecased sequence, or the start index if no Dutch IJ */ int32_t maybeTitleDutchIJ(const char16_t *src, UChar32 c, int32_t start, int32_t segmentLimit, char16_t *dest, int32_t &destIndex, int32_t destCapacity, uint32_t options, icu::Edits *edits) { U_ASSERT(start < segmentLimit); int32_t index = start; bool withAcute = false; // If the conditions are met, then the following variables tell us what to output. int32_t unchanged1 = 0; // code units before the j, or the whole sequence (0..3) bool doTitleJ = false; // true if the j needs to be titlecased int32_t unchanged2 = 0; // after the j (0 or 1) // next character after the first letter char16_t c2 = src[index++]; // Is the first letter an i/I with accent? if (c == u'I') { if (c2 == ACUTE) { withAcute = true; unchanged1 = 1; if (index == segmentLimit) { return start; } c2 = src[index++]; } } else { // Í withAcute = true; } // Is the next character a j/J? if (c2 == u'j') { doTitleJ = true; } else if (c2 == u'J') { ++unchanged1; } else { return start; } // A plain i/I must be followed by a plain j/J. // An i/I with acute must be followed by a j/J with acute. if (withAcute) { if (index == segmentLimit || src[index++] != ACUTE) { return start; } if (doTitleJ) { unchanged2 = 1; } else { ++unchanged1; } } // There must not be another combining mark. if (index < segmentLimit) { int32_t cp; int32_t i = index; U16_NEXT(src, i, segmentLimit, cp); uint32_t typeMask = U_GET_GC_MASK(cp); if ((typeMask & U_GC_M_MASK) != 0) { return start; } } // Output the rest of the Dutch IJ. destIndex = appendUnchanged(dest, destIndex, destCapacity, src + start, unchanged1, options, edits); start += unchanged1; if (doTitleJ) { destIndex = appendUChar(dest, destIndex, destCapacity, u'J'); if (edits != nullptr) { edits->addReplace(1, 1); } ++start; } destIndex = appendUnchanged(dest, destIndex, destCapacity, src + start, unchanged2, options, edits); U_ASSERT(start + unchanged2 == index); return index; } } // namespace U_CFUNC int32_t U_CALLCONV ustrcase_internalToTitle(int32_t caseLocale, uint32_t options, BreakIterator *iter, char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode) { if (!ustrcase_checkTitleAdjustmentOptions(options, errorCode)) { return 0; } /* set up local variables */ UCaseContext csc=UCASECONTEXT_INITIALIZER; csc.p=(void *)src; csc.limit=srcLength; int32_t destIndex=0; int32_t prev=0; bool isFirstIndex=true; /* titlecasing loop */ while(prevfirst(); } else { index=iter->next(); } if(index==UBRK_DONE || index>srcLength) { index=srcLength; } /* * Segment [prev..index[ into 3 parts: * a) skipped characters (copy as-is) [prev..titleStart[ * b) first letter (titlecase) [titleStart..titleLimit[ * c) subsequent characters (lowercase) [titleLimit..index[ */ if(prev 0) { uint32_t upper = data & UPPER_MASK; // Add a dialytika to this iota or ypsilon vowel // if we removed a tonos from the previous vowel, // and that previous vowel did not also have (or gain) a dialytika. // Adding one only to the final vowel in a longer sequence // (which does not occur in normal writing) would require lookahead. // Set the same flag as for preserving an existing dialytika. if ((data & HAS_VOWEL) != 0 && (state & (AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT | AFTER_VOWEL_WITH_COMBINING_ACCENT)) != 0 && (upper == 0x399 || upper == 0x3A5)) { data |= (state & AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT) ? HAS_DIALYTIKA : HAS_COMBINING_DIALYTIKA; } int32_t numYpogegrammeni = 0; // Map each one to a trailing, spacing, capital iota. if ((data & HAS_YPOGEGRAMMENI) != 0) { numYpogegrammeni = 1; } const UBool hasPrecomposedAccent = (data & HAS_ACCENT) != 0; // Skip combining diacritics after this Greek letter. while (nextIndex < srcLength) { uint32_t diacriticData = getDiacriticData(src[nextIndex]); if (diacriticData != 0) { data |= diacriticData; if ((diacriticData & HAS_YPOGEGRAMMENI) != 0) { ++numYpogegrammeni; } ++nextIndex; } else { break; // not a Greek diacritic } } if ((data & HAS_VOWEL_AND_ACCENT_AND_DIALYTIKA) == HAS_VOWEL_AND_ACCENT) { nextState |= hasPrecomposedAccent ? AFTER_VOWEL_WITH_PRECOMPOSED_ACCENT : AFTER_VOWEL_WITH_COMBINING_ACCENT; } // Map according to Greek rules. UBool addTonos = false; if (upper == 0x397 && (data & HAS_ACCENT) != 0 && numYpogegrammeni == 0 && (state & AFTER_CASED) == 0 && !isFollowedByCasedLetter(src, nextIndex, srcLength)) { // Keep disjunctive "or" with (only) a tonos. // We use the same "word boundary" conditions as for the Final_Sigma test. if (hasPrecomposedAccent) { upper = 0x389; // Preserve the precomposed form. } else { addTonos = true; } } else if ((data & HAS_DIALYTIKA) != 0) { // Preserve a vowel with dialytika in precomposed form if it exists. if (upper == 0x399) { upper = 0x3AA; data &= ~HAS_EITHER_DIALYTIKA; } else if (upper == 0x3A5) { upper = 0x3AB; data &= ~HAS_EITHER_DIALYTIKA; } } UBool change; if (edits == nullptr && (options & U_OMIT_UNCHANGED_TEXT) == 0) { change = true; // common, simple usage } else { // Find out first whether we are changing the text. change = src[i] != upper || numYpogegrammeni > 0; int32_t i2 = i + 1; if ((data & HAS_EITHER_DIALYTIKA) != 0) { change |= i2 >= nextIndex || src[i2] != 0x308; ++i2; } if (addTonos) { change |= i2 >= nextIndex || src[i2] != 0x301; ++i2; } int32_t oldLength = nextIndex - i; int32_t newLength = (i2 - i) + numYpogegrammeni; change |= oldLength != newLength; if (change) { if (edits != nullptr) { edits->addReplace(oldLength, newLength); } } else { if (edits != nullptr) { edits->addUnchanged(oldLength); } // Write unchanged text? change = (options & U_OMIT_UNCHANGED_TEXT) == 0; } } if (change) { destIndex=appendUChar(dest, destIndex, destCapacity, (char16_t)upper); if (destIndex >= 0 && (data & HAS_EITHER_DIALYTIKA) != 0) { destIndex=appendUChar(dest, destIndex, destCapacity, 0x308); // restore or add a dialytika } if (destIndex >= 0 && addTonos) { destIndex=appendUChar(dest, destIndex, destCapacity, 0x301); } while (destIndex >= 0 && numYpogegrammeni > 0) { destIndex=appendUChar(dest, destIndex, destCapacity, 0x399); --numYpogegrammeni; } if(destIndex<0) { errorCode=U_INDEX_OUTOFBOUNDS_ERROR; return 0; } } } else { const char16_t *s; c=ucase_toFullUpper(c, nullptr, nullptr, &s, UCASE_LOC_GREEK); destIndex = appendResult(dest, destIndex, destCapacity, c, s, nextIndex - i, options, edits); if (destIndex < 0) { errorCode = U_INDEX_OUTOFBOUNDS_ERROR; return 0; } } i = nextIndex; state = nextState; } return destIndex; } } // namespace GreekUpper U_NAMESPACE_END /* functions available in the common library (for unistr_case.cpp) */ U_CFUNC int32_t U_CALLCONV ustrcase_internalToLower(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode) { UCaseContext csc=UCASECONTEXT_INITIALIZER; csc.p=(void *)src; csc.limit=srcLength; int32_t destIndex = toLower( caseLocale, options, dest, destCapacity, src, &csc, 0, srcLength, edits, errorCode); return checkOverflowAndEditsError(destIndex, destCapacity, edits, errorCode); } U_CFUNC int32_t U_CALLCONV ustrcase_internalToUpper(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode) { int32_t destIndex; if (caseLocale == UCASE_LOC_GREEK) { destIndex = GreekUpper::toUpper(options, dest, destCapacity, src, srcLength, edits, errorCode); } else { UCaseContext csc=UCASECONTEXT_INITIALIZER; csc.p=(void *)src; csc.limit=srcLength; destIndex = toUpper( caseLocale, options, dest, destCapacity, src, &csc, srcLength, edits, errorCode); } return checkOverflowAndEditsError(destIndex, destCapacity, edits, errorCode); } U_CFUNC int32_t U_CALLCONV ustrcase_internalFold(int32_t /* caseLocale */, uint32_t options, UCASEMAP_BREAK_ITERATOR_UNUSED char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode) { int32_t destIndex = toLower( -1, options, dest, destCapacity, src, nullptr, 0, srcLength, edits, errorCode); return checkOverflowAndEditsError(destIndex, destCapacity, edits, errorCode); } U_CFUNC int32_t ustrcase_map(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, UStringCaseMapper *stringCaseMapper, icu::Edits *edits, UErrorCode &errorCode) { int32_t destLength; /* check argument values */ if(U_FAILURE(errorCode)) { return 0; } if( destCapacity<0 || (dest==nullptr && destCapacity>0) || src==nullptr || srcLength<-1 ) { errorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* get the string length */ if(srcLength==-1) { srcLength=u_strlen(src); } /* check for overlapping source and destination */ if( dest!=nullptr && ((src>=dest && src<(dest+destCapacity)) || (dest>=src && dest<(src+srcLength))) ) { errorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } if (edits != nullptr && (options & U_EDITS_NO_RESET) == 0) { edits->reset(); } destLength=stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR dest, destCapacity, src, srcLength, edits, errorCode); return u_terminateUChars(dest, destCapacity, destLength, &errorCode); } U_CFUNC int32_t ustrcase_mapWithOverlap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, UStringCaseMapper *stringCaseMapper, UErrorCode &errorCode) { char16_t buffer[300]; char16_t *temp; int32_t destLength; /* check argument values */ if(U_FAILURE(errorCode)) { return 0; } if( destCapacity<0 || (dest==nullptr && destCapacity>0) || src==nullptr || srcLength<-1 ) { errorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } /* get the string length */ if(srcLength==-1) { srcLength=u_strlen(src); } /* check for overlapping source and destination */ if( dest!=nullptr && ((src>=dest && src<(dest+destCapacity)) || (dest>=src && dest<(src+srcLength))) ) { /* overlap: provide a temporary destination buffer and later copy the result */ if(destCapacity<=UPRV_LENGTHOF(buffer)) { /* the stack buffer is large enough */ temp=buffer; } else { /* allocate a buffer */ temp=(char16_t *)uprv_malloc(destCapacity*U_SIZEOF_UCHAR); if(temp==nullptr) { errorCode=U_MEMORY_ALLOCATION_ERROR; return 0; } } } else { temp=dest; } destLength=stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR temp, destCapacity, src, srcLength, nullptr, errorCode); if(temp!=dest) { /* copy the result string to the destination buffer */ if (U_SUCCESS(errorCode) && 0 < destLength && destLength <= destCapacity) { u_memmove(dest, temp, destLength); } if(temp!=buffer) { uprv_free(temp); } } return u_terminateUChars(dest, destCapacity, destLength, &errorCode); } /* public API functions */ U_CAPI int32_t U_EXPORT2 u_strFoldCase(char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, uint32_t options, UErrorCode *pErrorCode) { return ustrcase_mapWithOverlap( UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL dest, destCapacity, src, srcLength, ustrcase_internalFold, *pErrorCode); } U_NAMESPACE_BEGIN int32_t CaseMap::fold( uint32_t options, const char16_t *src, int32_t srcLength, char16_t *dest, int32_t destCapacity, Edits *edits, UErrorCode &errorCode) { return ustrcase_map( UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL dest, destCapacity, src, srcLength, ustrcase_internalFold, edits, errorCode); } U_NAMESPACE_END /* case-insensitive string comparisons -------------------------------------- */ /* * This function is a copy of unorm_cmpEquivFold() minus the parts for * canonical equivalence. * Keep the functions in sync, and see there for how this works. * The duplication is for modularization: * It makes caseless (but not canonical caseless) matches independent of * the normalization code. */ /* stack element for previous-level source/decomposition pointers */ struct CmpEquivLevel { const char16_t *start, *s, *limit; }; typedef struct CmpEquivLevel CmpEquivLevel; /** * Internal implementation code comparing string with case fold. * This function is called from u_strcmpFold() and u_caseInsensitivePrefixMatch(). * * @param s1 input string 1 * @param length1 length of string 1, or -1 (NUL terminated) * @param s2 input string 2 * @param length2 length of string 2, or -1 (NUL terminated) * @param options compare options * @param matchLen1 (output) length of partial prefix match in s1 * @param matchLen2 (output) length of partial prefix match in s2 * @param pErrorCode receives error status * @return The result of comparison */ static int32_t _cmpFold( const char16_t *s1, int32_t length1, const char16_t *s2, int32_t length2, uint32_t options, int32_t *matchLen1, int32_t *matchLen2, UErrorCode *pErrorCode) { int32_t cmpRes = 0; /* current-level start/limit - s1/s2 as current */ const char16_t *start1, *start2, *limit1, *limit2; /* points to the original start address */ const char16_t *org1, *org2; /* points to the end of match + 1 */ const char16_t *m1, *m2; /* case folding variables */ const char16_t *p; int32_t length; /* stacks of previous-level start/current/limit */ CmpEquivLevel stack1[2], stack2[2]; /* case folding buffers, only use current-level start/limit */ char16_t fold1[UCASE_MAX_STRING_LENGTH+1], fold2[UCASE_MAX_STRING_LENGTH+1]; /* track which is the current level per string */ int32_t level1, level2; /* current code units, and code points for lookups */ UChar32 c1, c2, cp1, cp2; /* no argument error checking because this itself is not an API */ /* * assume that at least the option U_COMPARE_IGNORE_CASE is set * otherwise this function would have to behave exactly as uprv_strCompare() */ if(U_FAILURE(*pErrorCode)) { return 0; } /* initialize */ if(matchLen1) { U_ASSERT(matchLen2 !=nullptr); *matchLen1=0; *matchLen2=0; } start1=m1=org1=s1; if(length1==-1) { limit1=nullptr; } else { limit1=s1+length1; } start2=m2=org2=s2; if(length2==-1) { limit2=nullptr; } else { limit2=s2+length2; } level1=level2=0; c1=c2=-1; /* comparison loop */ for(;;) { /* * here a code unit value of -1 means "get another code unit" * below it will mean "this source is finished" */ if(c1<0) { /* get next code unit from string 1, post-increment */ for(;;) { if(s1==limit1 || ((c1=*s1)==0 && (limit1==nullptr || (options&_STRNCMP_STYLE)))) { if(level1==0) { c1=-1; break; } } else { ++s1; break; } /* reached end of level buffer, pop one level */ do { --level1; start1=stack1[level1].start; /*Not uninitialized*/ } while(start1==nullptr); s1=stack1[level1].s; /*Not uninitialized*/ limit1=stack1[level1].limit; /*Not uninitialized*/ } } if(c2<0) { /* get next code unit from string 2, post-increment */ for(;;) { if(s2==limit2 || ((c2=*s2)==0 && (limit2==nullptr || (options&_STRNCMP_STYLE)))) { if(level2==0) { c2=-1; break; } } else { ++s2; break; } /* reached end of level buffer, pop one level */ do { --level2; start2=stack2[level2].start; /*Not uninitialized*/ } while(start2==nullptr); s2=stack2[level2].s; /*Not uninitialized*/ limit2=stack2[level2].limit; /*Not uninitialized*/ } } /* * compare c1 and c2 * either variable c1, c2 is -1 only if the corresponding string is finished */ if(c1==c2) { const char16_t *next1, *next2; if(c1<0) { cmpRes=0; /* c1==c2==-1 indicating end of strings */ break; } /* * Note: Move the match positions in both strings at the same time * only when corresponding code point(s) in the original strings * are fully consumed. For example, when comparing s1="Fust" and * s2="Fu\u00dfball", s2[2] is folded into "ss", and s1[2] matches * the first code point in the case-folded data. But the second "s" * has no matching code point in s1, so this implementation returns * 2 as the prefix match length ("Fu"). */ next1=next2=nullptr; if(level1==0) { next1=s1; } else if(s1==limit1) { /* Note: This implementation only use a single level of stack. * If this code needs to be changed to use multiple levels * of stacks, the code above should check if the current * code is at the end of all stacks. */ U_ASSERT(level1==1); /* is s1 at the end of the current stack? */ next1=stack1[0].s; } if (next1!=nullptr) { if(level2==0) { next2=s2; } else if(s2==limit2) { U_ASSERT(level2==1); /* is s2 at the end of the current stack? */ next2=stack2[0].s; } if(next2!=nullptr) { m1=next1; m2=next2; } } c1=c2=-1; /* make us fetch new code units */ continue; } else if(c1<0) { cmpRes=-1; /* string 1 ends before string 2 */ break; } else if(c2<0) { cmpRes=1; /* string 2 ends before string 1 */ break; } /* c1!=c2 && c1>=0 && c2>=0 */ /* get complete code points for c1, c2 for lookups if either is a surrogate */ cp1=c1; if(U_IS_SURROGATE(c1)) { char16_t c; if(U_IS_SURROGATE_LEAD(c1)) { if(s1!=limit1 && U16_IS_TRAIL(c=*s1)) { /* advance ++s1; only below if cp1 decomposes/case-folds */ cp1=U16_GET_SUPPLEMENTARY(c1, c); } } else /* isTrail(c1) */ { if(start1<=(s1-2) && U16_IS_LEAD(c=*(s1-2))) { cp1=U16_GET_SUPPLEMENTARY(c, c1); } } } cp2=c2; if(U_IS_SURROGATE(c2)) { char16_t c; if(U_IS_SURROGATE_LEAD(c2)) { if(s2!=limit2 && U16_IS_TRAIL(c=*s2)) { /* advance ++s2; only below if cp2 decomposes/case-folds */ cp2=U16_GET_SUPPLEMENTARY(c2, c); } } else /* isTrail(c2) */ { if(start2<=(s2-2) && U16_IS_LEAD(c=*(s2-2))) { cp2=U16_GET_SUPPLEMENTARY(c, c2); } } } /* * go down one level for each string * continue with the main loop as soon as there is a real change */ if( level1==0 && (length=ucase_toFullFolding((UChar32)cp1, &p, options))>=0 ) { /* cp1 case-folds to the code point "length" or to p[length] */ if(U_IS_SURROGATE(c1)) { if(U_IS_SURROGATE_LEAD(c1)) { /* advance beyond source surrogate pair if it case-folds */ ++s1; } else /* isTrail(c1) */ { /* * we got a supplementary code point when hitting its trail surrogate, * therefore the lead surrogate must have been the same as in the other string; * compare this decomposition with the lead surrogate in the other string * remember that this simulates bulk text replacement: * the decomposition would replace the entire code point */ --s2; --m2; c2=*(s2-1); } } /* push current level pointers */ stack1[0].start=start1; stack1[0].s=s1; stack1[0].limit=limit1; ++level1; /* copy the folding result to fold1[] */ if(length<=UCASE_MAX_STRING_LENGTH) { u_memcpy(fold1, p, length); } else { int32_t i=0; U16_APPEND_UNSAFE(fold1, i, length); length=i; } /* set next level pointers to case folding */ start1=s1=fold1; limit1=fold1+length; /* get ready to read from decomposition, continue with loop */ c1=-1; continue; } if( level2==0 && (length=ucase_toFullFolding((UChar32)cp2, &p, options))>=0 ) { /* cp2 case-folds to the code point "length" or to p[length] */ if(U_IS_SURROGATE(c2)) { if(U_IS_SURROGATE_LEAD(c2)) { /* advance beyond source surrogate pair if it case-folds */ ++s2; } else /* isTrail(c2) */ { /* * we got a supplementary code point when hitting its trail surrogate, * therefore the lead surrogate must have been the same as in the other string; * compare this decomposition with the lead surrogate in the other string * remember that this simulates bulk text replacement: * the decomposition would replace the entire code point */ --s1; --m2; c1=*(s1-1); } } /* push current level pointers */ stack2[0].start=start2; stack2[0].s=s2; stack2[0].limit=limit2; ++level2; /* copy the folding result to fold2[] */ if(length<=UCASE_MAX_STRING_LENGTH) { u_memcpy(fold2, p, length); } else { int32_t i=0; U16_APPEND_UNSAFE(fold2, i, length); length=i; } /* set next level pointers to case folding */ start2=s2=fold2; limit2=fold2+length; /* get ready to read from decomposition, continue with loop */ c2=-1; continue; } /* * no decomposition/case folding, max level for both sides: * return difference result * * code point order comparison must not just return cp1-cp2 * because when single surrogates are present then the surrogate pairs * that formed cp1 and cp2 may be from different string indexes * * example: { d800 d800 dc01 } vs. { d800 dc00 }, compare at second code units * c1=d800 cp1=10001 c2=dc00 cp2=10000 * cp1-cp2>0 but c1-c2<0 and in fact in UTF-32 it is { d800 10001 } < { 10000 } * * therefore, use same fix-up as in ustring.c/uprv_strCompare() * except: uprv_strCompare() fetches c=*s while this functions fetches c=*s++ * so we have slightly different pointer/start/limit comparisons here */ if(c1>=0xd800 && c2>=0xd800 && (options&U_COMPARE_CODE_POINT_ORDER)) { /* subtract 0x2800 from BMP code points to make them smaller than supplementary ones */ if( (c1<=0xdbff && s1!=limit1 && U16_IS_TRAIL(*s1)) || (U16_IS_TRAIL(c1) && start1!=(s1-1) && U16_IS_LEAD(*(s1-2))) ) { /* part of a surrogate pair, leave >=d800 */ } else { /* BMP code point - may be surrogate code point - make =d800 */ } else { /* BMP code point - may be surrogate code point - make (m1-org1); *matchLen2=static_cast(m2-org2); } return cmpRes; } /* internal function */ U_CFUNC int32_t u_strcmpFold(const char16_t *s1, int32_t length1, const char16_t *s2, int32_t length2, uint32_t options, UErrorCode *pErrorCode) { return _cmpFold(s1, length1, s2, length2, options, nullptr, nullptr, pErrorCode); } /* public API functions */ U_CAPI int32_t U_EXPORT2 u_strCaseCompare(const char16_t *s1, int32_t length1, const char16_t *s2, int32_t length2, uint32_t options, UErrorCode *pErrorCode) { /* argument checking */ if(pErrorCode==0 || U_FAILURE(*pErrorCode)) { return 0; } if(s1==nullptr || length1<-1 || s2==nullptr || length2<-1) { *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR; return 0; } return u_strcmpFold(s1, length1, s2, length2, options|U_COMPARE_IGNORE_CASE, pErrorCode); } U_CAPI int32_t U_EXPORT2 u_strcasecmp(const char16_t *s1, const char16_t *s2, uint32_t options) { UErrorCode errorCode=U_ZERO_ERROR; return u_strcmpFold(s1, -1, s2, -1, options|U_COMPARE_IGNORE_CASE, &errorCode); } U_CAPI int32_t U_EXPORT2 u_memcasecmp(const char16_t *s1, const char16_t *s2, int32_t length, uint32_t options) { UErrorCode errorCode=U_ZERO_ERROR; return u_strcmpFold(s1, length, s2, length, options|U_COMPARE_IGNORE_CASE, &errorCode); } U_CAPI int32_t U_EXPORT2 u_strncasecmp(const char16_t *s1, const char16_t *s2, int32_t n, uint32_t options) { UErrorCode errorCode=U_ZERO_ERROR; return u_strcmpFold(s1, n, s2, n, options|(U_COMPARE_IGNORE_CASE|_STRNCMP_STYLE), &errorCode); } /* internal API - detect length of shared prefix */ U_CAPI void u_caseInsensitivePrefixMatch(const char16_t *s1, int32_t length1, const char16_t *s2, int32_t length2, uint32_t options, int32_t *matchLen1, int32_t *matchLen2, UErrorCode *pErrorCode) { _cmpFold(s1, length1, s2, length2, options, matchLen1, matchLen2, pErrorCode); }