1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
*
* Copyright (C) 2001-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*
*******************************************************************************
* file name: casetrn.cpp
* encoding: UTF-8
* tab size: 8 (not used)
* indentation:4
*
* created on: 2004sep03
* created by: Markus W. Scherer
*
* Implementation class for lower-/upper-/title-casing transliterators.
*/
#include <_foundation_unicode/utypes.h>
#if !UCONFIG_NO_TRANSLITERATION
#include <_foundation_unicode/uchar.h>
#include <_foundation_unicode/ustring.h>
#include <_foundation_unicode/utf.h>
#include <_foundation_unicode/utf16.h>
#include "tolowtrn.h"
#include "ucase.h"
#include "cpputils.h"
/* case context iterator using a Replaceable */
U_CFUNC UChar32 U_CALLCONV
utrans_rep_caseContextIterator(void *context, int8_t dir)
{
U_NAMESPACE_USE
UCaseContext *csc=(UCaseContext *)context;
Replaceable *rep=(Replaceable *)csc->p;
UChar32 c;
if(dir<0) {
/* reset for backward iteration */
csc->index=csc->cpStart;
csc->dir=dir;
} else if(dir>0) {
/* reset for forward iteration */
csc->index=csc->cpLimit;
csc->dir=dir;
} else {
/* continue current iteration direction */
dir=csc->dir;
}
// automatically adjust start and limit if the Replaceable disagrees
// with the original values
if(dir<0) {
if(csc->start<csc->index) {
c=rep->char32At(csc->index-1);
if(c<0) {
csc->start=csc->index;
} else {
csc->index-=U16_LENGTH(c);
return c;
}
}
} else {
// detect, and store in csc->b1, if we hit the limit
if(csc->index<csc->limit) {
c=rep->char32At(csc->index);
if(c<0) {
csc->limit=csc->index;
csc->b1=true;
} else {
csc->index+=U16_LENGTH(c);
return c;
}
} else {
csc->b1=true;
}
}
return U_SENTINEL;
}
U_NAMESPACE_BEGIN
UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(CaseMapTransliterator)
/**
* Constructs a transliterator.
*/
CaseMapTransliterator::CaseMapTransliterator(const UnicodeString &id, UCaseMapFull *map) :
Transliterator(id, 0),
fMap(map)
{
// TODO test incremental mode with context-sensitive text (e.g. greek sigma)
// TODO need to call setMaximumContextLength()?!
}
/**
* Destructor.
*/
CaseMapTransliterator::~CaseMapTransliterator() {
}
/**
* Copy constructor.
*/
CaseMapTransliterator::CaseMapTransliterator(const CaseMapTransliterator& o) :
Transliterator(o),
fMap(o.fMap)
{
}
/**
* Assignment operator.
*/
/*CaseMapTransliterator& CaseMapTransliterator::operator=(const CaseMapTransliterator& o) {
Transliterator::operator=(o);
fMap = o.fMap;
return *this;
}*/
/**
* Transliterator API.
*/
/*CaseMapTransliterator* CaseMapTransliterator::clone() const {
return new CaseMapTransliterator(*this);
}*/
/**
* Implements {@link Transliterator#handleTransliterate}.
*/
void CaseMapTransliterator::handleTransliterate(Replaceable& text,
UTransPosition& offsets,
UBool isIncremental) const
{
if (offsets.start >= offsets.limit) {
return;
}
UCaseContext csc;
uprv_memset(&csc, 0, sizeof(csc));
csc.p = &text;
csc.start = offsets.contextStart;
csc.limit = offsets.contextLimit;
UnicodeString tmp;
const char16_t *s;
UChar32 c;
int32_t textPos, delta, result;
for(textPos=offsets.start; textPos<offsets.limit;) {
csc.cpStart=textPos;
c=text.char32At(textPos);
csc.cpLimit=textPos+=U16_LENGTH(c);
result=fMap(c, utrans_rep_caseContextIterator, &csc, &s, UCASE_LOC_ROOT);
if(csc.b1 && isIncremental) {
// fMap() tried to look beyond the context limit
// wait for more input
offsets.start=csc.cpStart;
return;
}
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;
}
}
}
offsets.start=textPos;
}
U_NAMESPACE_END
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|