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-30 00:57:29 +00:00
|
|
|
#include "unicode/ustring.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/uchar.h"
|
|
|
|
#include "toupptrn.h"
|
2001-11-30 00:57:29 +00:00
|
|
|
#include "ustr_imp.h"
|
|
|
|
#include "cpputils.h"
|
2001-06-11 19:51:46 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
const char UppercaseTransliterator::fgClassID=0;
|
|
|
|
|
2001-10-11 23:54:55 +00:00
|
|
|
const char UppercaseTransliterator::_ID[] = "Any-Upper";
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
UppercaseTransliterator::UppercaseTransliterator(const Locale& theLoc) :
|
|
|
|
Transliterator(_ID, 0),
|
|
|
|
loc(theLoc),
|
2002-07-16 02:47:19 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
|
|
|
buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0]));
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
UppercaseTransliterator::~UppercaseTransliterator() {
|
2002-07-16 02:47:19 +00:00
|
|
|
uprv_free(buffer);
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
UppercaseTransliterator::UppercaseTransliterator(const UppercaseTransliterator& o) :
|
2001-11-30 00:57:29 +00:00
|
|
|
Transliterator(o),
|
|
|
|
loc(o.loc),
|
2002-07-16 02:47:19 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
|
|
|
buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0]));
|
2001-11-30 00:57:29 +00:00
|
|
|
uprv_arrayCopy(o.buffer, 0, this->buffer, 0, u_getMaxCaseExpansion());
|
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
|
|
|
UppercaseTransliterator& UppercaseTransliterator::operator=(
|
|
|
|
const UppercaseTransliterator& 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(o.buffer, 0, this->buffer, 0, u_getMaxCaseExpansion());
|
2001-06-11 19:51:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* UppercaseTransliterator::clone(void) const {
|
|
|
|
return new UppercaseTransliterator(*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 UppercaseTransliterator::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
|
|
|
|
|
|
|
|
UnicodeString original;
|
2002-02-26 17:50:59 +00:00
|
|
|
text.extractBetween(offsets.contextStart, offsets.contextLimit, original);
|
2002-03-19 22:09:07 +00:00
|
|
|
|
|
|
|
UCharIterator iter;
|
|
|
|
uiter_setReplaceable(&iter, &text);
|
|
|
|
iter.start = offsets.contextStart;
|
|
|
|
iter.limit = offsets.contextLimit;
|
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;
|
2002-03-19 22:09:07 +00:00
|
|
|
UChar32 cp;
|
2001-11-30 00:57:29 +00:00
|
|
|
int32_t oldLen;
|
|
|
|
|
|
|
|
for (; i < limit; ) {
|
|
|
|
UTF_GET_CHAR(original.getBuffer(), 0, i, original.length(), cp);
|
|
|
|
oldLen = UTF_CHAR_LENGTH(cp);
|
|
|
|
i += oldLen;
|
2002-03-19 22:09:07 +00:00
|
|
|
iter.index = i; // Point _past_ current char
|
|
|
|
int32_t newLen = u_internalToUpper(cp, &iter, buffer, u_getMaxCaseExpansion(), loc.getName());
|
|
|
|
if (newLen >= 0) {
|
|
|
|
UnicodeString temp(buffer, newLen);
|
2001-11-30 00:57:29 +00:00
|
|
|
text.handleReplaceBetween(textPos, textPos + oldLen, temp);
|
2002-03-19 22:09:07 +00:00
|
|
|
if (newLen != oldLen) {
|
|
|
|
textPos += newLen;
|
|
|
|
offsets.limit += newLen - oldLen;
|
|
|
|
offsets.contextLimit += newLen - oldLen;
|
2001-11-30 00:57:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
textPos += oldLen;
|
|
|
|
}
|
|
|
|
offsets.start = offsets.limit;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|