2001-06-11 19:51:46 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 2001, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 05/24/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2001-11-16 23:51:15 +00:00
|
|
|
#include "tolowtrn.h"
|
2001-11-30 00:57:29 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "ustr_imp.h"
|
|
|
|
#include "cpputils.h"
|
2001-11-30 00:06:13 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-06-11 19:51:46 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2001-10-11 23:54:55 +00:00
|
|
|
const char LowercaseTransliterator::_ID[] = "Any-Lower";
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
LowercaseTransliterator::LowercaseTransliterator(const Locale& theLoc) : Transliterator(_ID, 0),
|
|
|
|
loc(theLoc) , buffer(0) {
|
|
|
|
buffer = new UChar[u_getMaxCaseExpansion()];
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
LowercaseTransliterator::~LowercaseTransliterator() {
|
|
|
|
delete [] buffer;
|
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
LowercaseTransliterator::LowercaseTransliterator(const LowercaseTransliterator& o) :
|
2001-11-30 00:57:29 +00:00
|
|
|
Transliterator(o),
|
|
|
|
loc(o.loc), buffer(0) {
|
|
|
|
buffer = new UChar[u_getMaxCaseExpansion()];
|
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
LowercaseTransliterator& LowercaseTransliterator::operator=(
|
|
|
|
const LowercaseTransliterator& o) {
|
2001-11-30 00:57:29 +00:00
|
|
|
Transliterator::operator=(o);
|
2001-06-11 19:51:46 +00:00
|
|
|
loc = o.loc;
|
2001-11-30 00:57:29 +00:00
|
|
|
uprv_arrayCopy((const UChar*)o.buffer, 0, this->buffer, 0, u_getMaxCaseExpansion());
|
2001-06-11 19:51:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* LowercaseTransliterator::clone(void) const {
|
|
|
|
return new LowercaseTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2001-11-30 00:57:29 +00:00
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
2001-06-11 19:51:46 +00:00
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
void LowercaseTransliterator::handleTransliterate(Replaceable& text,
|
|
|
|
UTransPosition& offsets,
|
|
|
|
UBool isIncremental) const
|
|
|
|
{
|
|
|
|
int32_t textPos = offsets.start;
|
|
|
|
if (textPos >= offsets.limit) return;
|
2001-06-11 19:51:46 +00:00
|
|
|
|
2001-11-30 00:57:29 +00:00
|
|
|
// get string for context
|
2002-02-26 17:50:59 +00:00
|
|
|
|
2001-11-30 00:57:29 +00:00
|
|
|
UnicodeString original;
|
2002-02-26 17:50:59 +00:00
|
|
|
text.extractBetween(offsets.contextStart, offsets.contextLimit, original);
|
2001-11-30 00:57:29 +00:00
|
|
|
|
|
|
|
// Walk through original string
|
|
|
|
// If there is a case change, modify corresponding position in replaceable
|
|
|
|
|
|
|
|
int32_t i = textPos - offsets.contextStart;
|
|
|
|
int32_t limit = offsets.limit - offsets.contextStart;
|
2001-11-30 23:53:55 +00:00
|
|
|
UChar32 cp, bufferCH;
|
2001-11-30 00:57:29 +00:00
|
|
|
int32_t oldLen;
|
|
|
|
|
|
|
|
for (; i < limit; ) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
int32_t s = i;
|
2001-11-30 23:53:55 +00:00
|
|
|
bufferCH = original.char32At(s);
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2001-11-30 00:57:29 +00:00
|
|
|
UTF_GET_CHAR(original.getBuffer(), 0, i, original.length(), cp);
|
|
|
|
oldLen = UTF_CHAR_LENGTH(cp);
|
|
|
|
i += oldLen;
|
2001-12-04 02:16:58 +00:00
|
|
|
int32_t len = u_strToLower(buffer, u_getMaxCaseExpansion(), original.getBuffer()+s, i-s, loc.getName(), &status);
|
2001-11-30 00:57:29 +00:00
|
|
|
/* Skip checking of status code here because the buffer should not have overflowed. */
|
2001-12-04 02:16:58 +00:00
|
|
|
UTF_GET_CHAR(buffer, 0, 0, len, cp);
|
2001-11-30 23:53:55 +00:00
|
|
|
if ( bufferCH != cp ) {
|
2001-11-30 00:57:29 +00:00
|
|
|
UnicodeString temp(buffer);
|
|
|
|
text.handleReplaceBetween(textPos, textPos + oldLen, temp);
|
|
|
|
if (len != oldLen) {
|
|
|
|
textPos += len;
|
|
|
|
offsets.limit += len - oldLen;
|
|
|
|
offsets.contextLimit += len - oldLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
textPos += oldLen;
|
|
|
|
}
|
|
|
|
offsets.start = offsets.limit;
|
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|