2001-06-11 19:51:46 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2003-06-03 20:58:22 +00:00
|
|
|
* Copyright (C) 2001-2003, 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
|
|
|
|
|
2001-06-11 19:51:46 +00:00
|
|
|
#include "unicode/uchar.h"
|
2001-11-01 04:37:27 +00:00
|
|
|
#include "unicode/uniset.h"
|
2002-07-12 21:42:24 +00:00
|
|
|
#include "unicode/ustring.h"
|
|
|
|
#include "titletrn.h"
|
2003-05-02 23:14:53 +00:00
|
|
|
#include "umutex.h"
|
2001-11-13 23:47:11 +00:00
|
|
|
#include "ucln_in.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(TitlecaseTransliterator)
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2001-06-11 19:51:46 +00:00
|
|
|
/**
|
|
|
|
* ID for this transliterator.
|
|
|
|
*/
|
2003-10-07 15:49:47 +00:00
|
|
|
const char CURR_ID[] = "Any-Title";
|
2001-06-11 19:51:46 +00:00
|
|
|
|
2001-11-01 04:37:27 +00:00
|
|
|
/**
|
|
|
|
* The set of characters we skip. These are neither cased nor
|
2003-05-02 23:14:53 +00:00
|
|
|
* non-cased, to us; we copy them verbatim. INVARIANT: Either SKIP
|
|
|
|
* and CASED are both NULL, or neither is NULL.
|
2001-11-01 04:37:27 +00:00
|
|
|
*/
|
2001-11-14 17:23:01 +00:00
|
|
|
static UnicodeSet* SKIP = NULL;
|
2001-11-01 04:37:27 +00:00
|
|
|
|
|
|
|
/**
|
2003-05-02 23:14:53 +00:00
|
|
|
* The set of characters that cause the next non-SKIP character to be
|
|
|
|
* lowercased. INVARIANT: Either SKIP and CASED are both NULL, or
|
|
|
|
* neither is NULL.
|
2001-11-01 04:37:27 +00:00
|
|
|
*/
|
2001-11-14 17:23:01 +00:00
|
|
|
static UnicodeSet* CASED = NULL;
|
2001-11-01 04:37:27 +00:00
|
|
|
|
2001-11-30 00:57:29 +00:00
|
|
|
TitlecaseTransliterator::TitlecaseTransliterator(const Locale& theLoc) :
|
2003-10-07 15:49:47 +00:00
|
|
|
Transliterator(UnicodeString(CURR_ID, ""), 0),
|
2001-11-30 00:57:29 +00:00
|
|
|
loc(theLoc),
|
2002-07-16 02:47:19 +00:00
|
|
|
buffer(0)
|
|
|
|
{
|
|
|
|
buffer = (UChar *)uprv_malloc(u_getMaxCaseExpansion()*sizeof(buffer[0]));
|
2001-06-29 21:19:49 +00:00
|
|
|
// Need to look back 2 characters in the case of "can't"
|
|
|
|
setMaximumContextLength(2);
|
2003-05-02 23:14:53 +00:00
|
|
|
|
|
|
|
umtx_lock(NULL);
|
|
|
|
UBool f = (SKIP == NULL);
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
|
|
|
|
if (f) {
|
|
|
|
UErrorCode ec = U_ZERO_ERROR;
|
|
|
|
UnicodeSet* skip =
|
|
|
|
new UnicodeSet(UNICODE_STRING_SIMPLE("[\\u00AD \\u2019 \\' [:Mn:] [:Me:] [:Cf:] [:Lm:] [:Sk:]]"), ec);
|
|
|
|
UnicodeSet* cased =
|
|
|
|
new UnicodeSet(UNICODE_STRING_SIMPLE("[[:Lu:] [:Ll:] [:Lt:]]"), ec);
|
|
|
|
if (skip != NULL && cased != NULL && U_SUCCESS(ec)) {
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if (SKIP == NULL) {
|
|
|
|
SKIP = skip;
|
|
|
|
CASED = cased;
|
|
|
|
skip = cased = NULL;
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
}
|
|
|
|
delete skip;
|
|
|
|
delete cased;
|
|
|
|
ucln_i18n_registerCleanup();
|
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
TitlecaseTransliterator::~TitlecaseTransliterator() {
|
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.
|
|
|
|
*/
|
|
|
|
TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator& 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.
|
|
|
|
*/
|
|
|
|
TitlecaseTransliterator& TitlecaseTransliterator::operator=(
|
|
|
|
const TitlecaseTransliterator& o) {
|
|
|
|
Transliterator::operator=(o);
|
2001-11-30 00:57:29 +00:00
|
|
|
loc = o.loc;
|
|
|
|
uprv_arrayCopy(o.buffer, 0, this->buffer, 0, u_getMaxCaseExpansion());
|
2001-06-11 19:51:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* TitlecaseTransliterator::clone(void) const {
|
|
|
|
return new TitlecaseTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void TitlecaseTransliterator::handleTransliterate(
|
|
|
|
Replaceable& text, UTransPosition& offsets,
|
2003-12-06 00:52:51 +00:00
|
|
|
UBool /*isIncremental*/) const
|
|
|
|
{
|
|
|
|
/* TODO: Verify that isIncremental can be ignored */
|
2001-11-01 04:37:27 +00:00
|
|
|
if (SKIP == NULL) {
|
2003-05-02 23:14:53 +00:00
|
|
|
return;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2001-11-01 04:37:27 +00:00
|
|
|
// Our mode; we are either converting letter toTitle or
|
|
|
|
// toLower.
|
|
|
|
UBool doTitle = TRUE;
|
|
|
|
|
|
|
|
// Determine if there is a preceding context of CASED SKIP*,
|
|
|
|
// in which case we want to start in toLower mode. If the
|
|
|
|
// prior context is anything else (including empty) then start
|
|
|
|
// in toTitle mode.
|
2001-12-01 22:37:34 +00:00
|
|
|
UChar32 c;
|
|
|
|
int32_t start;
|
|
|
|
for (start = offsets.start - 1; start >= offsets.contextStart; start -= UTF_CHAR_LENGTH(c)) {
|
|
|
|
c = text.char32At(start);
|
2001-11-01 04:37:27 +00:00
|
|
|
if (SKIP->contains(c)) {
|
|
|
|
continue;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-11-01 04:37:27 +00:00
|
|
|
doTitle = !CASED->contains(c);
|
|
|
|
break;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-11-01 04:37:27 +00:00
|
|
|
|
|
|
|
// Convert things after a CASED character toLower; things
|
|
|
|
// after a non-CASED, non-SKIP character toTitle. SKIP
|
|
|
|
// characters are copied directly and do not change the mode.
|
2001-11-30 00:57:29 +00:00
|
|
|
int32_t textPos = offsets.start;
|
|
|
|
if (textPos >= offsets.limit) return;
|
|
|
|
|
|
|
|
UnicodeString original;
|
2002-03-19 21:54:13 +00:00
|
|
|
text.extractBetween(offsets.contextStart, offsets.contextLimit, original);
|
|
|
|
|
|
|
|
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-02-20 02:10:29 +00:00
|
|
|
UChar32 cp;
|
2001-11-30 00:57:29 +00:00
|
|
|
int32_t oldLen;
|
|
|
|
int32_t newLen;
|
|
|
|
|
|
|
|
for (; i < limit; ) {
|
|
|
|
UTF_GET_CHAR(original.getBuffer(), 0, i, original.length(), cp);
|
|
|
|
oldLen = UTF_CHAR_LENGTH(cp);
|
|
|
|
i += oldLen;
|
2002-03-19 21:54:13 +00:00
|
|
|
iter.index = i; // Point _past_ current char
|
2001-11-30 00:57:29 +00:00
|
|
|
if (!SKIP->contains(cp)) {
|
|
|
|
if (doTitle) {
|
2002-03-19 21:54:13 +00:00
|
|
|
newLen = u_internalToTitle(cp, &iter, buffer, u_getMaxCaseExpansion(), loc.getName());
|
2001-11-30 00:57:29 +00:00
|
|
|
} else {
|
2002-03-19 21:54:13 +00:00
|
|
|
newLen = u_internalToLower(cp, &iter, buffer, u_getMaxCaseExpansion(), loc.getName());
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
|
|
|
doTitle = !CASED->contains(cp);
|
|
|
|
if (newLen >= 0) {
|
|
|
|
UnicodeString temp(buffer, newLen);
|
|
|
|
text.handleReplaceBetween(textPos, textPos + oldLen, temp);
|
|
|
|
if (newLen != oldLen) {
|
|
|
|
textPos += newLen;
|
|
|
|
offsets.limit += newLen - oldLen;
|
|
|
|
offsets.contextLimit += newLen - oldLen;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2001-11-01 04:37:27 +00:00
|
|
|
}
|
2001-11-30 00:57:29 +00:00
|
|
|
textPos += oldLen;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-11-30 00:57:29 +00:00
|
|
|
offsets.start = offsets.limit;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-10-08 23:26:58 +00:00
|
|
|
|
2001-11-13 23:47:11 +00:00
|
|
|
/**
|
|
|
|
* Static memory cleanup function.
|
|
|
|
*/
|
|
|
|
void TitlecaseTransliterator::cleanup() {
|
|
|
|
if (SKIP != NULL) {
|
|
|
|
delete SKIP; SKIP = NULL;
|
|
|
|
delete CASED; CASED = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|