intl/icu/source/common/uchriter.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 ******************************************************************************
michael@0 3 * Copyright (C) 1998-2012, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 ******************************************************************************
michael@0 6 */
michael@0 7
michael@0 8 #include "utypeinfo.h" // for 'typeid' to work
michael@0 9
michael@0 10 #include "unicode/uchriter.h"
michael@0 11 #include "unicode/ustring.h"
michael@0 12 #include "unicode/utf16.h"
michael@0 13 #include "ustr_imp.h"
michael@0 14
michael@0 15 U_NAMESPACE_BEGIN
michael@0 16
michael@0 17 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UCharCharacterIterator)
michael@0 18
michael@0 19 UCharCharacterIterator::UCharCharacterIterator()
michael@0 20 : CharacterIterator(),
michael@0 21 text(0)
michael@0 22 {
michael@0 23 // never default construct!
michael@0 24 }
michael@0 25
michael@0 26 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
michael@0 27 int32_t length)
michael@0 28 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0),
michael@0 29 text(textPtr)
michael@0 30 {
michael@0 31 }
michael@0 32
michael@0 33 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
michael@0 34 int32_t length,
michael@0 35 int32_t position)
michael@0 36 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, position),
michael@0 37 text(textPtr)
michael@0 38 {
michael@0 39 }
michael@0 40
michael@0 41 UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr,
michael@0 42 int32_t length,
michael@0 43 int32_t textBegin,
michael@0 44 int32_t textEnd,
michael@0 45 int32_t position)
michael@0 46 : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, textBegin, textEnd, position),
michael@0 47 text(textPtr)
michael@0 48 {
michael@0 49 }
michael@0 50
michael@0 51 UCharCharacterIterator::UCharCharacterIterator(const UCharCharacterIterator& that)
michael@0 52 : CharacterIterator(that),
michael@0 53 text(that.text)
michael@0 54 {
michael@0 55 }
michael@0 56
michael@0 57 UCharCharacterIterator&
michael@0 58 UCharCharacterIterator::operator=(const UCharCharacterIterator& that) {
michael@0 59 CharacterIterator::operator=(that);
michael@0 60 text = that.text;
michael@0 61 return *this;
michael@0 62 }
michael@0 63
michael@0 64 UCharCharacterIterator::~UCharCharacterIterator() {
michael@0 65 }
michael@0 66
michael@0 67 UBool
michael@0 68 UCharCharacterIterator::operator==(const ForwardCharacterIterator& that) const {
michael@0 69 if (this == &that) {
michael@0 70 return TRUE;
michael@0 71 }
michael@0 72 if (typeid(*this) != typeid(that)) {
michael@0 73 return FALSE;
michael@0 74 }
michael@0 75
michael@0 76 UCharCharacterIterator& realThat = (UCharCharacterIterator&)that;
michael@0 77
michael@0 78 return text == realThat.text
michael@0 79 && textLength == realThat.textLength
michael@0 80 && pos == realThat.pos
michael@0 81 && begin == realThat.begin
michael@0 82 && end == realThat.end;
michael@0 83 }
michael@0 84
michael@0 85 int32_t
michael@0 86 UCharCharacterIterator::hashCode() const {
michael@0 87 return ustr_hashUCharsN(text, textLength) ^ pos ^ begin ^ end;
michael@0 88 }
michael@0 89
michael@0 90 CharacterIterator*
michael@0 91 UCharCharacterIterator::clone() const {
michael@0 92 return new UCharCharacterIterator(*this);
michael@0 93 }
michael@0 94
michael@0 95 UChar
michael@0 96 UCharCharacterIterator::first() {
michael@0 97 pos = begin;
michael@0 98 if(pos < end) {
michael@0 99 return text[pos];
michael@0 100 } else {
michael@0 101 return DONE;
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105 UChar
michael@0 106 UCharCharacterIterator::firstPostInc() {
michael@0 107 pos = begin;
michael@0 108 if(pos < end) {
michael@0 109 return text[pos++];
michael@0 110 } else {
michael@0 111 return DONE;
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 UChar
michael@0 116 UCharCharacterIterator::last() {
michael@0 117 pos = end;
michael@0 118 if(pos > begin) {
michael@0 119 return text[--pos];
michael@0 120 } else {
michael@0 121 return DONE;
michael@0 122 }
michael@0 123 }
michael@0 124
michael@0 125 UChar
michael@0 126 UCharCharacterIterator::setIndex(int32_t position) {
michael@0 127 if(position < begin) {
michael@0 128 pos = begin;
michael@0 129 } else if(position > end) {
michael@0 130 pos = end;
michael@0 131 } else {
michael@0 132 pos = position;
michael@0 133 }
michael@0 134 if(pos < end) {
michael@0 135 return text[pos];
michael@0 136 } else {
michael@0 137 return DONE;
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 UChar
michael@0 142 UCharCharacterIterator::current() const {
michael@0 143 if (pos >= begin && pos < end) {
michael@0 144 return text[pos];
michael@0 145 } else {
michael@0 146 return DONE;
michael@0 147 }
michael@0 148 }
michael@0 149
michael@0 150 UChar
michael@0 151 UCharCharacterIterator::next() {
michael@0 152 if (pos + 1 < end) {
michael@0 153 return text[++pos];
michael@0 154 } else {
michael@0 155 /* make current() return DONE */
michael@0 156 pos = end;
michael@0 157 return DONE;
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 UChar
michael@0 162 UCharCharacterIterator::nextPostInc() {
michael@0 163 if (pos < end) {
michael@0 164 return text[pos++];
michael@0 165 } else {
michael@0 166 return DONE;
michael@0 167 }
michael@0 168 }
michael@0 169
michael@0 170 UBool
michael@0 171 UCharCharacterIterator::hasNext() {
michael@0 172 return (UBool)(pos < end ? TRUE : FALSE);
michael@0 173 }
michael@0 174
michael@0 175 UChar
michael@0 176 UCharCharacterIterator::previous() {
michael@0 177 if (pos > begin) {
michael@0 178 return text[--pos];
michael@0 179 } else {
michael@0 180 return DONE;
michael@0 181 }
michael@0 182 }
michael@0 183
michael@0 184 UBool
michael@0 185 UCharCharacterIterator::hasPrevious() {
michael@0 186 return (UBool)(pos > begin ? TRUE : FALSE);
michael@0 187 }
michael@0 188
michael@0 189 UChar32
michael@0 190 UCharCharacterIterator::first32() {
michael@0 191 pos = begin;
michael@0 192 if(pos < end) {
michael@0 193 int32_t i = pos;
michael@0 194 UChar32 c;
michael@0 195 U16_NEXT(text, i, end, c);
michael@0 196 return c;
michael@0 197 } else {
michael@0 198 return DONE;
michael@0 199 }
michael@0 200 }
michael@0 201
michael@0 202 UChar32
michael@0 203 UCharCharacterIterator::first32PostInc() {
michael@0 204 pos = begin;
michael@0 205 if(pos < end) {
michael@0 206 UChar32 c;
michael@0 207 U16_NEXT(text, pos, end, c);
michael@0 208 return c;
michael@0 209 } else {
michael@0 210 return DONE;
michael@0 211 }
michael@0 212 }
michael@0 213
michael@0 214 UChar32
michael@0 215 UCharCharacterIterator::last32() {
michael@0 216 pos = end;
michael@0 217 if(pos > begin) {
michael@0 218 UChar32 c;
michael@0 219 U16_PREV(text, begin, pos, c);
michael@0 220 return c;
michael@0 221 } else {
michael@0 222 return DONE;
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 UChar32
michael@0 227 UCharCharacterIterator::setIndex32(int32_t position) {
michael@0 228 if(position < begin) {
michael@0 229 position = begin;
michael@0 230 } else if(position > end) {
michael@0 231 position = end;
michael@0 232 }
michael@0 233 if(position < end) {
michael@0 234 U16_SET_CP_START(text, begin, position);
michael@0 235 int32_t i = this->pos = position;
michael@0 236 UChar32 c;
michael@0 237 U16_NEXT(text, i, end, c);
michael@0 238 return c;
michael@0 239 } else {
michael@0 240 this->pos = position;
michael@0 241 return DONE;
michael@0 242 }
michael@0 243 }
michael@0 244
michael@0 245 UChar32
michael@0 246 UCharCharacterIterator::current32() const {
michael@0 247 if (pos >= begin && pos < end) {
michael@0 248 UChar32 c;
michael@0 249 U16_GET(text, begin, pos, end, c);
michael@0 250 return c;
michael@0 251 } else {
michael@0 252 return DONE;
michael@0 253 }
michael@0 254 }
michael@0 255
michael@0 256 UChar32
michael@0 257 UCharCharacterIterator::next32() {
michael@0 258 if (pos < end) {
michael@0 259 U16_FWD_1(text, pos, end);
michael@0 260 if(pos < end) {
michael@0 261 int32_t i = pos;
michael@0 262 UChar32 c;
michael@0 263 U16_NEXT(text, i, end, c);
michael@0 264 return c;
michael@0 265 }
michael@0 266 }
michael@0 267 /* make current() return DONE */
michael@0 268 pos = end;
michael@0 269 return DONE;
michael@0 270 }
michael@0 271
michael@0 272 UChar32
michael@0 273 UCharCharacterIterator::next32PostInc() {
michael@0 274 if (pos < end) {
michael@0 275 UChar32 c;
michael@0 276 U16_NEXT(text, pos, end, c);
michael@0 277 return c;
michael@0 278 } else {
michael@0 279 return DONE;
michael@0 280 }
michael@0 281 }
michael@0 282
michael@0 283 UChar32
michael@0 284 UCharCharacterIterator::previous32() {
michael@0 285 if (pos > begin) {
michael@0 286 UChar32 c;
michael@0 287 U16_PREV(text, begin, pos, c);
michael@0 288 return c;
michael@0 289 } else {
michael@0 290 return DONE;
michael@0 291 }
michael@0 292 }
michael@0 293
michael@0 294 int32_t
michael@0 295 UCharCharacterIterator::move(int32_t delta, CharacterIterator::EOrigin origin) {
michael@0 296 switch(origin) {
michael@0 297 case kStart:
michael@0 298 pos = begin + delta;
michael@0 299 break;
michael@0 300 case kCurrent:
michael@0 301 pos += delta;
michael@0 302 break;
michael@0 303 case kEnd:
michael@0 304 pos = end + delta;
michael@0 305 break;
michael@0 306 default:
michael@0 307 break;
michael@0 308 }
michael@0 309
michael@0 310 if(pos < begin) {
michael@0 311 pos = begin;
michael@0 312 } else if(pos > end) {
michael@0 313 pos = end;
michael@0 314 }
michael@0 315
michael@0 316 return pos;
michael@0 317 }
michael@0 318
michael@0 319 int32_t
michael@0 320 UCharCharacterIterator::move32(int32_t delta, CharacterIterator::EOrigin origin) {
michael@0 321 // this implementation relies on the "safe" version of the UTF macros
michael@0 322 // (or the trustworthiness of the caller)
michael@0 323 switch(origin) {
michael@0 324 case kStart:
michael@0 325 pos = begin;
michael@0 326 if(delta > 0) {
michael@0 327 U16_FWD_N(text, pos, end, delta);
michael@0 328 }
michael@0 329 break;
michael@0 330 case kCurrent:
michael@0 331 if(delta > 0) {
michael@0 332 U16_FWD_N(text, pos, end, delta);
michael@0 333 } else {
michael@0 334 U16_BACK_N(text, begin, pos, -delta);
michael@0 335 }
michael@0 336 break;
michael@0 337 case kEnd:
michael@0 338 pos = end;
michael@0 339 if(delta < 0) {
michael@0 340 U16_BACK_N(text, begin, pos, -delta);
michael@0 341 }
michael@0 342 break;
michael@0 343 default:
michael@0 344 break;
michael@0 345 }
michael@0 346
michael@0 347 return pos;
michael@0 348 }
michael@0 349
michael@0 350 void UCharCharacterIterator::setText(const UChar* newText,
michael@0 351 int32_t newTextLength) {
michael@0 352 text = newText;
michael@0 353 if(newText == 0 || newTextLength < 0) {
michael@0 354 newTextLength = 0;
michael@0 355 }
michael@0 356 end = textLength = newTextLength;
michael@0 357 pos = begin = 0;
michael@0 358 }
michael@0 359
michael@0 360 void
michael@0 361 UCharCharacterIterator::getText(UnicodeString& result) {
michael@0 362 result = UnicodeString(text, textLength);
michael@0 363 }
michael@0 364
michael@0 365 U_NAMESPACE_END

mercurial