ICU-1126 complete implementation of u_strToTitle()

X-SVN-Rev: 7732
This commit is contained in:
Markus Scherer 2002-02-21 17:58:51 +00:00
parent c3d1945687
commit ac808ef260
3 changed files with 48 additions and 12 deletions

View file

@ -1488,9 +1488,14 @@ single:
return (result==c) ? -length : length;
}
/*
* Lowercases [srcStart..srcLimit[ but takes
* context [0..srcLength[ into account.
*/
U_CFUNC int32_t
u_internalStrToLower(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
int32_t srcStart, int32_t srcLimit,
const char *locale,
UErrorCode *pErrorCode) {
UCharIterator iter;
@ -1504,6 +1509,9 @@ u_internalStrToLower(UChar *dest, int32_t destCapacity,
* If we do not have real character properties data,
* then we only do a fixed-length ASCII case mapping.
*/
src+=srcStart;
srcLength-=srcStart;
if(srcLength<=destCapacity) {
destIndex=srcLength;
*pErrorCode=U_USING_DEFAULT_ERROR;
@ -1528,9 +1536,10 @@ u_internalStrToLower(UChar *dest, int32_t destCapacity,
uiter_setString(&iter, src, srcLength);
/* case mapping loop */
srcIndex=destIndex=0;
while(srcIndex<srcLength) {
UTF_NEXT_CHAR(src, srcIndex, srcLength, c);
srcIndex=srcStart;
destIndex=0;
while(srcIndex<srcLimit) {
UTF_NEXT_CHAR(src, srcIndex, srcLimit, c);
GET_PROPS_UNSAFE(c, props);
if(!PROPS_VALUE_IS_EXCEPTION(props)) {
if((1UL<<GET_CATEGORY(props))&(1UL<<U_UPPERCASE_LETTER|1UL<<U_TITLECASE_LETTER)) {

View file

@ -19,6 +19,9 @@
#include "unicode/ucnv.h"
#include "unicode/uiter.h"
/** Simple declaration for u_strToTitle() to avoid including unicode/ubrk.h. */
typedef void *UBreakIterator;
/**
* Are the Unicode properties loaded?
* This must be used before internal functions are called that do
@ -52,13 +55,17 @@ u_growBufferFromStatic(void *context,
int32_t length);
/*
* Internal string casing functions implementing ustring.c and UnicodeString
* case mapping functions.
* Internal string casing functions implementing
* ustring.h/ustrcase.c and UnicodeString case mapping functions.
*
* Lowercases [srcStart..srcLimit[ but takes
* context [0..srcLength[ into account.
* @internal
*/
U_CFUNC int32_t
u_internalStrToLower(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
int32_t srcStart, int32_t srcLimit,
const char *locale,
UErrorCode *pErrorCode);
@ -71,6 +78,16 @@ u_internalStrToUpper(UChar *dest, int32_t destCapacity,
const char *locale,
UErrorCode *pErrorCode);
/**
* @internal
*/
U_CFUNC int32_t
u_internalStrToTitle(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UBreakIterator *titleIter,
const char *locale,
UErrorCode *pErrorCode);
/**
* Internal case folding function.
* @internal

View file

@ -32,7 +32,7 @@
*
* Must get titleIter!=NULL.
*/
static int32_t
U_CFUNC int32_t
u_internalStrToTitle(UChar *dest, int32_t destCapacity,
const UChar *src, int32_t srcLength,
UBreakIterator *titleIter,
@ -41,31 +41,39 @@ u_internalStrToTitle(UChar *dest, int32_t destCapacity,
UCharIterator iter;
UChar32 c;
int32_t prev, index, destIndex, length;
UBool isFirstIndex;
/* set up local variables */
uiter_setString(&iter, src, srcLength);
destIndex=0;
prev=0;
isFirstIndex=TRUE;
/* titlecasing loop */
while(prev<srcLength) {
/* find next index where to titlecase */
index=ubrk_next(titleIter);
if(index==UBRK_DONE) {
if(isFirstIndex) {
isFirstIndex=FALSE;
index=ubrk_first(titleIter);
} else {
index=ubrk_next(titleIter);
}
if(index==UBRK_DONE || index>srcLength) {
index=srcLength;
}
/* lowercase [prev..index[ */
/* ### TODO this really needs to pass the full UCharIterator into the internal strToXXX functions so that they see the full context! */
if(prev<index) {
if(destIndex<destCapacity) {
length=u_internalStrToLower(dest+destIndex, destCapacity-destIndex,
src+prev, index-prev,
src, srcLength,
prev, index,
locale,
pErrorCode);
} else {
length=u_internalStrToLower(NULL, 0,
src+prev, index-prev,
src, srcLength,
prev, index,
locale,
pErrorCode);
}
@ -163,7 +171,9 @@ u_strCaseMap(UChar *dest, int32_t destCapacity,
ownTitleIter=FALSE;
if(toWhichCase==TO_LOWER) {
destLength=u_internalStrToLower(temp, destCapacity, src, srcLength,
destLength=u_internalStrToLower(temp, destCapacity,
src, srcLength,
0, srcLength,
locale, pErrorCode);
} else if(toWhichCase==TO_UPPER) {
destLength=u_internalStrToUpper(temp, destCapacity, src, srcLength,