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