2001-06-11 19:51:46 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2004-07-19 21:33:03 +00:00
|
|
|
* Copyright (C) 2001-2004, International Business Machines
|
2001-06-11 19:51:46 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 05/24/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-11-30 00:57:29 +00:00
|
|
|
#include "unicode/ustring.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "tolowtrn.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
|
|
|
|
|
2003-08-31 20:53:46 +00:00
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LowercaseTransliterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2001-06-11 19:51:46 +00:00
|
|
|
/**
|
|
|
|
* Constructs a transliterator.
|
|
|
|
*/
|
2003-10-07 15:49:47 +00:00
|
|
|
LowercaseTransliterator::LowercaseTransliterator(const Locale& theLoc) :
|
2004-08-27 17:57:36 +00:00
|
|
|
Transliterator(UNICODE_STRING("Any-Lower", 9), 0),
|
2002-07-16 02:47:19 +00:00
|
|
|
loc(theLoc) , 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
|
|
|
LowercaseTransliterator::~LowercaseTransliterator() {
|
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.
|
|
|
|
*/
|
|
|
|
LowercaseTransliterator::LowercaseTransliterator(const LowercaseTransliterator& o) :
|
2001-11-30 00:57:29 +00:00
|
|
|
Transliterator(o),
|
2002-07-16 02:47:19 +00:00
|
|
|
loc(o.loc), buffer(0)
|
|
|
|
{
|
|
|
|
buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0]));
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
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,
|
2003-12-06 00:52:51 +00:00
|
|
|
UBool /*isIncremental*/) const
|
2001-11-30 00:57:29 +00:00
|
|
|
{
|
2003-12-06 00:52:51 +00:00
|
|
|
/* TODO: Verify that isIncremental can be ignored */
|
2001-11-30 00:57:29 +00:00
|
|
|
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
|
|
|
|
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_internalToLower(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-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|