2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2004-08-26 22:51:40 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
|
|
|
*
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 1999-2014, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
2004-08-26 22:51:40 +00:00
|
|
|
*
|
|
|
|
*******************************************************************************
|
|
|
|
* file name: unistr_case.cpp
|
2017-02-03 18:57:23 +00:00
|
|
|
* encoding: UTF-8
|
2004-08-26 22:51:40 +00:00
|
|
|
* tab size: 8 (not used)
|
|
|
|
* indentation:2
|
|
|
|
*
|
|
|
|
* created on: 2004aug19
|
|
|
|
* created by: Markus W. Scherer
|
|
|
|
*
|
|
|
|
* Case-mapping functions moved here from unistr.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
2017-04-12 22:39:37 +00:00
|
|
|
#include "unicode/brkiter.h"
|
2017-02-09 21:15:34 +00:00
|
|
|
#include "unicode/casemap.h"
|
|
|
|
#include "unicode/edits.h"
|
2004-08-26 22:51:40 +00:00
|
|
|
#include "unicode/putil.h"
|
|
|
|
#include "cstring.h"
|
|
|
|
#include "cmemory.h"
|
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/uchar.h"
|
2017-01-04 21:20:08 +00:00
|
|
|
#include "uassert.h"
|
2017-02-09 21:15:34 +00:00
|
|
|
#include "ucasemap_imp.h"
|
2011-07-04 23:51:56 +00:00
|
|
|
#include "uelement.h"
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Read-only implementation
|
|
|
|
//========================================
|
|
|
|
|
|
|
|
int8_t
|
|
|
|
UnicodeString::doCaseCompare(int32_t start,
|
|
|
|
int32_t length,
|
|
|
|
const UChar *srcChars,
|
|
|
|
int32_t srcStart,
|
|
|
|
int32_t srcLength,
|
|
|
|
uint32_t options) const
|
|
|
|
{
|
|
|
|
// compare illegal string values
|
|
|
|
// treat const UChar *srcChars==NULL as an empty string
|
|
|
|
if(isBogus()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pin indices to legal values
|
|
|
|
pinIndices(start, length);
|
|
|
|
|
|
|
|
if(srcChars == NULL) {
|
|
|
|
srcStart = srcLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the correct pointer
|
|
|
|
const UChar *chars = getArrayStart();
|
|
|
|
|
|
|
|
chars += start;
|
2011-11-14 19:32:51 +00:00
|
|
|
if(srcStart!=0) {
|
|
|
|
srcChars += srcStart;
|
|
|
|
}
|
2004-08-26 22:51:40 +00:00
|
|
|
|
|
|
|
if(chars != srcChars) {
|
|
|
|
UErrorCode errorCode=U_ZERO_ERROR;
|
2004-09-11 14:24:31 +00:00
|
|
|
int32_t result=u_strcmpFold(chars, length, srcChars, srcLength,
|
|
|
|
options|U_COMPARE_IGNORE_CASE, &errorCode);
|
2004-08-26 22:51:40 +00:00
|
|
|
if(result!=0) {
|
|
|
|
return (int8_t)(result >> 24 | 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// get the srcLength if necessary
|
|
|
|
if(srcLength < 0) {
|
|
|
|
srcLength = u_strlen(srcChars + srcStart);
|
|
|
|
}
|
|
|
|
if(length != srcLength) {
|
|
|
|
return (int8_t)((length - srcLength) >> 24 | 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//========================================
|
|
|
|
// Write implementation
|
|
|
|
//========================================
|
|
|
|
|
|
|
|
UnicodeString &
|
2017-01-20 04:04:58 +00:00
|
|
|
UnicodeString::caseMap(int32_t caseLocale, uint32_t options, UCASEMAP_BREAK_ITERATOR_PARAM
|
2011-06-03 05:23:57 +00:00
|
|
|
UStringCaseMapper *stringCaseMapper) {
|
2007-09-28 04:50:25 +00:00
|
|
|
if(isEmpty() || !isWritable()) {
|
2004-08-26 22:51:40 +00:00
|
|
|
// nothing to do
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2017-01-04 21:20:08 +00:00
|
|
|
UChar oldBuffer[2 * US_STACKBUF_SIZE];
|
2007-09-28 04:50:25 +00:00
|
|
|
UChar *oldArray;
|
2017-01-04 21:20:08 +00:00
|
|
|
int32_t oldLength = length();
|
|
|
|
int32_t newLength;
|
|
|
|
UBool writable = isBufferWritable();
|
|
|
|
UErrorCode errorCode = U_ZERO_ERROR;
|
|
|
|
|
2017-04-12 22:39:37 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
// Read-only alias to the original string contents for the titlecasing BreakIterator.
|
|
|
|
// We cannot set the iterator simply to *this because *this is being modified.
|
|
|
|
UnicodeString oldString;
|
|
|
|
#endif
|
|
|
|
|
2017-01-04 21:20:08 +00:00
|
|
|
// Try to avoid heap-allocating a new character array for this string.
|
|
|
|
if (writable ? oldLength <= UPRV_LENGTHOF(oldBuffer) : oldLength < US_STACKBUF_SIZE) {
|
|
|
|
// Short string: Copy the contents into a temporary buffer and
|
|
|
|
// case-map back into the current array, or into the stack buffer.
|
|
|
|
UChar *buffer = getArrayStart();
|
|
|
|
int32_t capacity;
|
|
|
|
oldArray = oldBuffer;
|
|
|
|
u_memcpy(oldBuffer, buffer, oldLength);
|
|
|
|
if (writable) {
|
|
|
|
capacity = getCapacity();
|
|
|
|
} else {
|
|
|
|
// Switch from the read-only alias or shared heap buffer to the stack buffer.
|
|
|
|
if (!cloneArrayIfNeeded(US_STACKBUF_SIZE, US_STACKBUF_SIZE, /* doCopyArray= */ FALSE)) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
U_ASSERT(fUnion.fFields.fLengthAndFlags & kUsingStackBuffer);
|
|
|
|
buffer = fUnion.fStackFields.fBuffer;
|
|
|
|
capacity = US_STACKBUF_SIZE;
|
|
|
|
}
|
2017-04-12 22:39:37 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
if (iter != nullptr) {
|
|
|
|
oldString.setTo(FALSE, oldArray, oldLength);
|
|
|
|
iter->setText(oldString);
|
|
|
|
}
|
|
|
|
#endif
|
2017-01-20 04:04:58 +00:00
|
|
|
newLength = stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
|
2017-01-09 23:52:12 +00:00
|
|
|
buffer, capacity,
|
|
|
|
oldArray, oldLength, NULL, errorCode);
|
2017-01-04 21:20:08 +00:00
|
|
|
if (U_SUCCESS(errorCode)) {
|
|
|
|
setLength(newLength);
|
|
|
|
return *this;
|
|
|
|
} else if (errorCode == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
// common overflow handling below
|
|
|
|
} else {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
2007-09-28 04:50:25 +00:00
|
|
|
} else {
|
2017-01-04 21:20:08 +00:00
|
|
|
// Longer string or read-only buffer:
|
|
|
|
// Collect only changes and then apply them to this string.
|
|
|
|
// Case mapping often changes only small parts of a string,
|
|
|
|
// and often does not change its length.
|
2007-09-28 04:50:25 +00:00
|
|
|
oldArray = getArrayStart();
|
2017-01-04 21:20:08 +00:00
|
|
|
Edits edits;
|
|
|
|
UChar replacementChars[200];
|
2017-04-12 22:39:37 +00:00
|
|
|
#if !UCONFIG_NO_BREAK_ITERATION
|
|
|
|
if (iter != nullptr) {
|
|
|
|
oldString.setTo(FALSE, oldArray, oldLength);
|
|
|
|
iter->setText(oldString);
|
|
|
|
}
|
|
|
|
#endif
|
2017-06-08 19:36:34 +00:00
|
|
|
stringCaseMapper(caseLocale, options | U_OMIT_UNCHANGED_TEXT, UCASEMAP_BREAK_ITERATOR
|
2017-01-09 23:52:12 +00:00
|
|
|
replacementChars, UPRV_LENGTHOF(replacementChars),
|
|
|
|
oldArray, oldLength, &edits, errorCode);
|
2017-01-04 21:20:08 +00:00
|
|
|
if (U_SUCCESS(errorCode)) {
|
2017-01-06 00:20:31 +00:00
|
|
|
// Grow the buffer at most once, not for multiple doReplace() calls.
|
2017-01-09 23:52:12 +00:00
|
|
|
newLength = oldLength + edits.lengthDelta();
|
2017-01-06 00:20:31 +00:00
|
|
|
if (newLength > oldLength && !cloneArrayIfNeeded(newLength, newLength)) {
|
2017-01-04 21:20:08 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2017-01-09 23:52:12 +00:00
|
|
|
for (Edits::Iterator ei = edits.getCoarseChangesIterator(); ei.next(errorCode);) {
|
|
|
|
doReplace(ei.destinationIndex(), ei.oldLength(),
|
|
|
|
replacementChars, ei.replacementIndex(), ei.newLength());
|
2017-01-04 21:20:08 +00:00
|
|
|
}
|
|
|
|
if (U_FAILURE(errorCode)) {
|
|
|
|
setToBogus();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
} else if (errorCode == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
// common overflow handling below
|
2017-01-09 23:52:12 +00:00
|
|
|
newLength = oldLength + edits.lengthDelta();
|
2017-01-04 21:20:08 +00:00
|
|
|
} else {
|
|
|
|
setToBogus();
|
|
|
|
return *this;
|
|
|
|
}
|
2007-09-28 04:50:25 +00:00
|
|
|
}
|
2004-08-26 22:51:40 +00:00
|
|
|
|
2017-01-04 21:20:08 +00:00
|
|
|
// Handle buffer overflow, newLength is known.
|
|
|
|
// We need to allocate a new buffer for the internal string case mapping function.
|
|
|
|
// This is very similar to how doReplace() keeps the old array pointer
|
|
|
|
// and deletes the old array itself after it is done.
|
|
|
|
// In addition, we are forcing cloneArrayIfNeeded() to always allocate a new array.
|
2007-09-28 04:50:25 +00:00
|
|
|
int32_t *bufferToDelete = 0;
|
2017-01-04 21:20:08 +00:00
|
|
|
if (!cloneArrayIfNeeded(newLength, newLength, FALSE, &bufferToDelete, TRUE)) {
|
2004-08-26 22:51:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2017-01-04 21:20:08 +00:00
|
|
|
errorCode = U_ZERO_ERROR;
|
2017-04-12 22:39:37 +00:00
|
|
|
// No need to iter->setText() again: The case mapper restarts via iter->first().
|
2017-01-20 04:04:58 +00:00
|
|
|
newLength = stringCaseMapper(caseLocale, options, UCASEMAP_BREAK_ITERATOR
|
2017-01-09 23:52:12 +00:00
|
|
|
getArrayStart(), getCapacity(),
|
|
|
|
oldArray, oldLength, NULL, errorCode);
|
2004-08-26 22:51:40 +00:00
|
|
|
if (bufferToDelete) {
|
|
|
|
uprv_free(bufferToDelete);
|
|
|
|
}
|
2017-01-04 21:20:08 +00:00
|
|
|
if (U_SUCCESS(errorCode)) {
|
|
|
|
setLength(newLength);
|
|
|
|
} else {
|
2004-08-26 22:51:40 +00:00
|
|
|
setToBogus();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2004-10-08 05:17:17 +00:00
|
|
|
UnicodeString &
|
|
|
|
UnicodeString::foldCase(uint32_t options) {
|
2017-01-20 04:04:58 +00:00
|
|
|
return caseMap(UCASE_LOC_ROOT, options, UCASEMAP_BREAK_ITERATOR_NULL ustrcase_internalFold);
|
2004-10-08 05:17:17 +00:00
|
|
|
}
|
|
|
|
|
2004-08-26 22:51:40 +00:00
|
|
|
U_NAMESPACE_END
|
2004-12-05 06:23:51 +00:00
|
|
|
|
|
|
|
// Defined here to reduce dependencies on break iterator
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2011-07-04 23:51:56 +00:00
|
|
|
uhash_hashCaselessUnicodeString(const UElement key) {
|
2004-12-05 06:23:51 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
const UnicodeString *str = (const UnicodeString*) key.pointer;
|
|
|
|
if (str == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// Inefficient; a better way would be to have a hash function in
|
|
|
|
// UnicodeString that does case folding on the fly.
|
|
|
|
UnicodeString copy(*str);
|
|
|
|
return copy.foldCase().hashCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Defined here to reduce dependencies on break iterator
|
|
|
|
U_CAPI UBool U_EXPORT2
|
2011-07-04 23:51:56 +00:00
|
|
|
uhash_compareCaselessUnicodeString(const UElement key1, const UElement key2) {
|
2004-12-05 06:23:51 +00:00
|
|
|
U_NAMESPACE_USE
|
|
|
|
const UnicodeString *str1 = (const UnicodeString*) key1.pointer;
|
|
|
|
const UnicodeString *str2 = (const UnicodeString*) key2.pointer;
|
|
|
|
if (str1 == str2) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
if (str1 == NULL || str2 == NULL) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return str1->caseCompare(*str2, U_FOLD_CASE_DEFAULT) == 0;
|
|
|
|
}
|