2001-06-11 19:51:46 +00:00
|
|
|
/*
|
|
|
|
**********************************************************************
|
2007-06-08 22:31:20 +00:00
|
|
|
* Copyright (C) 2001-2007, 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"
|
2004-09-06 15:57:11 +00:00
|
|
|
#include "ucase.h"
|
2001-11-30 00:57:29 +00:00
|
|
|
#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
|
|
|
|
2004-12-28 20:06:43 +00:00
|
|
|
TitlecaseTransliterator::TitlecaseTransliterator() :
|
|
|
|
CaseMapTransliterator(UNICODE_STRING("Any-Title", 9), NULL)
|
2002-07-16 02:47:19 +00:00
|
|
|
{
|
2001-06-29 21:19:49 +00:00
|
|
|
// Need to look back 2 characters in the case of "can't"
|
|
|
|
setMaximumContextLength(2);
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
2001-11-30 00:57:29 +00:00
|
|
|
TitlecaseTransliterator::~TitlecaseTransliterator() {
|
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
TitlecaseTransliterator::TitlecaseTransliterator(const TitlecaseTransliterator& o) :
|
2004-09-06 15:57:11 +00:00
|
|
|
CaseMapTransliterator(o)
|
2002-07-16 02:47:19 +00:00
|
|
|
{
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator.
|
|
|
|
*/
|
2007-06-08 22:31:20 +00:00
|
|
|
/*TitlecaseTransliterator& TitlecaseTransliterator::operator=(
|
2001-06-11 19:51:46 +00:00
|
|
|
const TitlecaseTransliterator& o) {
|
2004-09-06 15:57:11 +00:00
|
|
|
CaseMapTransliterator::operator=(o);
|
2001-06-11 19:51:46 +00:00
|
|
|
return *this;
|
2007-06-08 22:31:20 +00:00
|
|
|
}*/
|
2001-06-11 19:51:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transliterator API.
|
|
|
|
*/
|
|
|
|
Transliterator* TitlecaseTransliterator::clone(void) const {
|
|
|
|
return new TitlecaseTransliterator(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link Transliterator#handleTransliterate}.
|
|
|
|
*/
|
|
|
|
void TitlecaseTransliterator::handleTransliterate(
|
|
|
|
Replaceable& text, UTransPosition& offsets,
|
2004-09-06 15:57:11 +00:00
|
|
|
UBool isIncremental) const
|
2003-12-06 00:52:51 +00:00
|
|
|
{
|
2004-09-06 15:57:11 +00:00
|
|
|
// TODO reimplement, see ustrcase.c
|
|
|
|
// using a real word break iterator
|
|
|
|
// instead of just looking for a transition between cased and uncased characters
|
|
|
|
// call CaseMapTransliterator::handleTransliterate() for lowercasing? (set fMap)
|
|
|
|
// needs to take isIncremental into account because case mappings are context-sensitive
|
|
|
|
// also detect when lowercasing function did not finish because of context
|
|
|
|
|
|
|
|
if (offsets.start >= offsets.limit) {
|
2003-05-02 23:14:53 +00:00
|
|
|
return;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
|
|
|
|
2004-09-06 15:57:11 +00:00
|
|
|
// case type: >0 cased (UCASE_LOWER etc.) ==0 uncased <0 case-ignorable
|
|
|
|
int32_t type;
|
|
|
|
|
2001-11-01 04:37:27 +00:00
|
|
|
// Our mode; we are either converting letter toTitle or
|
|
|
|
// toLower.
|
|
|
|
UBool doTitle = TRUE;
|
|
|
|
|
2004-09-06 15:57:11 +00:00
|
|
|
// Determine if there is a preceding context of cased case-ignorable*,
|
2001-11-01 04:37:27 +00:00
|
|
|
// 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;
|
2004-09-06 15:57:11 +00:00
|
|
|
for (start = offsets.start - 1; start >= offsets.contextStart; start -= U16_LENGTH(c)) {
|
2001-12-01 22:37:34 +00:00
|
|
|
c = text.char32At(start);
|
2004-09-06 15:57:11 +00:00
|
|
|
type=ucase_getTypeOrIgnorable(fCsp, c);
|
|
|
|
if(type>0) { // cased
|
|
|
|
doTitle=FALSE;
|
|
|
|
break;
|
|
|
|
} else if(type==0) { // uncased but not ignorable
|
|
|
|
break;
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2004-09-06 15:57:11 +00:00
|
|
|
// else (type<0) case-ignorable: continue
|
2001-06-11 19:51:46 +00:00
|
|
|
}
|
2001-11-01 04:37:27 +00:00
|
|
|
|
2004-09-06 15:57:11 +00:00
|
|
|
// Convert things after a cased character toLower; things
|
|
|
|
// after an uncased, non-case-ignorable character toTitle. Case-ignorable
|
2001-11-01 04:37:27 +00:00
|
|
|
// characters are copied directly and do not change the mode.
|
2004-10-27 22:55:57 +00:00
|
|
|
UCaseContext csc;
|
|
|
|
uprv_memset(&csc, 0, sizeof(csc));
|
|
|
|
csc.p = &text;
|
2004-09-06 15:57:11 +00:00
|
|
|
csc.start = offsets.contextStart;
|
|
|
|
csc.limit = offsets.contextLimit;
|
|
|
|
|
|
|
|
UnicodeString tmp;
|
|
|
|
const UChar *s;
|
|
|
|
int32_t textPos, delta, result, locCache=0;
|
|
|
|
|
|
|
|
for(textPos=offsets.start; textPos<offsets.limit;) {
|
|
|
|
csc.cpStart=textPos;
|
|
|
|
c=text.char32At(textPos);
|
|
|
|
csc.cpLimit=textPos+=U16_LENGTH(c);
|
|
|
|
|
|
|
|
type=ucase_getTypeOrIgnorable(fCsp, c);
|
|
|
|
if(type>=0) { // not case-ignorable
|
|
|
|
if(doTitle) {
|
2004-12-28 20:06:43 +00:00
|
|
|
result=ucase_toFullTitle(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
|
2001-11-30 00:57:29 +00:00
|
|
|
} else {
|
2004-12-28 20:06:43 +00:00
|
|
|
result=ucase_toFullLower(fCsp, c, utrans_rep_caseContextIterator, &csc, &s, "", &locCache);
|
2004-09-06 15:57:11 +00:00
|
|
|
}
|
|
|
|
doTitle = (UBool)(type==0); // doTitle=isUncased
|
|
|
|
|
|
|
|
if(csc.b1 && isIncremental) {
|
|
|
|
// fMap() tried to look beyond the context limit
|
|
|
|
// wait for more input
|
2005-02-21 22:50:27 +00:00
|
|
|
offsets.start=csc.cpStart;
|
|
|
|
return;
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
2004-09-06 15:57:11 +00:00
|
|
|
|
|
|
|
if(result>=0) {
|
|
|
|
// replace the current code point with its full case mapping result
|
|
|
|
// see UCASE_MAX_STRING_LENGTH
|
|
|
|
if(result<=UCASE_MAX_STRING_LENGTH) {
|
|
|
|
// string s[result]
|
|
|
|
tmp.setTo(FALSE, s, result);
|
|
|
|
delta=result-U16_LENGTH(c);
|
|
|
|
} else {
|
|
|
|
// single code point
|
|
|
|
tmp.setTo(result);
|
|
|
|
delta=tmp.length()-U16_LENGTH(c);
|
|
|
|
}
|
|
|
|
text.handleReplaceBetween(csc.cpStart, textPos, tmp);
|
|
|
|
if(delta!=0) {
|
|
|
|
textPos+=delta;
|
|
|
|
csc.limit=offsets.contextLimit+=delta;
|
|
|
|
offsets.limit+=delta;
|
2001-11-30 00:57:29 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-01 04:37:27 +00:00
|
|
|
}
|
2001-11-13 23:47:11 +00:00
|
|
|
}
|
2004-09-06 15:57:11 +00:00
|
|
|
offsets.start=textPos;
|
2001-11-13 23:47:11 +00:00
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|