1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/uchriter.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,365 @@ 1.4 +/* 1.5 +****************************************************************************** 1.6 +* Copyright (C) 1998-2012, International Business Machines Corporation and 1.7 +* others. All Rights Reserved. 1.8 +****************************************************************************** 1.9 +*/ 1.10 + 1.11 +#include "utypeinfo.h" // for 'typeid' to work 1.12 + 1.13 +#include "unicode/uchriter.h" 1.14 +#include "unicode/ustring.h" 1.15 +#include "unicode/utf16.h" 1.16 +#include "ustr_imp.h" 1.17 + 1.18 +U_NAMESPACE_BEGIN 1.19 + 1.20 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UCharCharacterIterator) 1.21 + 1.22 +UCharCharacterIterator::UCharCharacterIterator() 1.23 + : CharacterIterator(), 1.24 + text(0) 1.25 +{ 1.26 + // never default construct! 1.27 +} 1.28 + 1.29 +UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, 1.30 + int32_t length) 1.31 + : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0), 1.32 + text(textPtr) 1.33 +{ 1.34 +} 1.35 + 1.36 +UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, 1.37 + int32_t length, 1.38 + int32_t position) 1.39 + : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, position), 1.40 + text(textPtr) 1.41 +{ 1.42 +} 1.43 + 1.44 +UCharCharacterIterator::UCharCharacterIterator(const UChar* textPtr, 1.45 + int32_t length, 1.46 + int32_t textBegin, 1.47 + int32_t textEnd, 1.48 + int32_t position) 1.49 + : CharacterIterator(textPtr != 0 ? (length>=0 ? length : u_strlen(textPtr)) : 0, textBegin, textEnd, position), 1.50 + text(textPtr) 1.51 +{ 1.52 +} 1.53 + 1.54 +UCharCharacterIterator::UCharCharacterIterator(const UCharCharacterIterator& that) 1.55 +: CharacterIterator(that), 1.56 + text(that.text) 1.57 +{ 1.58 +} 1.59 + 1.60 +UCharCharacterIterator& 1.61 +UCharCharacterIterator::operator=(const UCharCharacterIterator& that) { 1.62 + CharacterIterator::operator=(that); 1.63 + text = that.text; 1.64 + return *this; 1.65 +} 1.66 + 1.67 +UCharCharacterIterator::~UCharCharacterIterator() { 1.68 +} 1.69 + 1.70 +UBool 1.71 +UCharCharacterIterator::operator==(const ForwardCharacterIterator& that) const { 1.72 + if (this == &that) { 1.73 + return TRUE; 1.74 + } 1.75 + if (typeid(*this) != typeid(that)) { 1.76 + return FALSE; 1.77 + } 1.78 + 1.79 + UCharCharacterIterator& realThat = (UCharCharacterIterator&)that; 1.80 + 1.81 + return text == realThat.text 1.82 + && textLength == realThat.textLength 1.83 + && pos == realThat.pos 1.84 + && begin == realThat.begin 1.85 + && end == realThat.end; 1.86 +} 1.87 + 1.88 +int32_t 1.89 +UCharCharacterIterator::hashCode() const { 1.90 + return ustr_hashUCharsN(text, textLength) ^ pos ^ begin ^ end; 1.91 +} 1.92 + 1.93 +CharacterIterator* 1.94 +UCharCharacterIterator::clone() const { 1.95 + return new UCharCharacterIterator(*this); 1.96 +} 1.97 + 1.98 +UChar 1.99 +UCharCharacterIterator::first() { 1.100 + pos = begin; 1.101 + if(pos < end) { 1.102 + return text[pos]; 1.103 + } else { 1.104 + return DONE; 1.105 + } 1.106 +} 1.107 + 1.108 +UChar 1.109 +UCharCharacterIterator::firstPostInc() { 1.110 + pos = begin; 1.111 + if(pos < end) { 1.112 + return text[pos++]; 1.113 + } else { 1.114 + return DONE; 1.115 + } 1.116 +} 1.117 + 1.118 +UChar 1.119 +UCharCharacterIterator::last() { 1.120 + pos = end; 1.121 + if(pos > begin) { 1.122 + return text[--pos]; 1.123 + } else { 1.124 + return DONE; 1.125 + } 1.126 +} 1.127 + 1.128 +UChar 1.129 +UCharCharacterIterator::setIndex(int32_t position) { 1.130 + if(position < begin) { 1.131 + pos = begin; 1.132 + } else if(position > end) { 1.133 + pos = end; 1.134 + } else { 1.135 + pos = position; 1.136 + } 1.137 + if(pos < end) { 1.138 + return text[pos]; 1.139 + } else { 1.140 + return DONE; 1.141 + } 1.142 +} 1.143 + 1.144 +UChar 1.145 +UCharCharacterIterator::current() const { 1.146 + if (pos >= begin && pos < end) { 1.147 + return text[pos]; 1.148 + } else { 1.149 + return DONE; 1.150 + } 1.151 +} 1.152 + 1.153 +UChar 1.154 +UCharCharacterIterator::next() { 1.155 + if (pos + 1 < end) { 1.156 + return text[++pos]; 1.157 + } else { 1.158 + /* make current() return DONE */ 1.159 + pos = end; 1.160 + return DONE; 1.161 + } 1.162 +} 1.163 + 1.164 +UChar 1.165 +UCharCharacterIterator::nextPostInc() { 1.166 + if (pos < end) { 1.167 + return text[pos++]; 1.168 + } else { 1.169 + return DONE; 1.170 + } 1.171 +} 1.172 + 1.173 +UBool 1.174 +UCharCharacterIterator::hasNext() { 1.175 + return (UBool)(pos < end ? TRUE : FALSE); 1.176 +} 1.177 + 1.178 +UChar 1.179 +UCharCharacterIterator::previous() { 1.180 + if (pos > begin) { 1.181 + return text[--pos]; 1.182 + } else { 1.183 + return DONE; 1.184 + } 1.185 +} 1.186 + 1.187 +UBool 1.188 +UCharCharacterIterator::hasPrevious() { 1.189 + return (UBool)(pos > begin ? TRUE : FALSE); 1.190 +} 1.191 + 1.192 +UChar32 1.193 +UCharCharacterIterator::first32() { 1.194 + pos = begin; 1.195 + if(pos < end) { 1.196 + int32_t i = pos; 1.197 + UChar32 c; 1.198 + U16_NEXT(text, i, end, c); 1.199 + return c; 1.200 + } else { 1.201 + return DONE; 1.202 + } 1.203 +} 1.204 + 1.205 +UChar32 1.206 +UCharCharacterIterator::first32PostInc() { 1.207 + pos = begin; 1.208 + if(pos < end) { 1.209 + UChar32 c; 1.210 + U16_NEXT(text, pos, end, c); 1.211 + return c; 1.212 + } else { 1.213 + return DONE; 1.214 + } 1.215 +} 1.216 + 1.217 +UChar32 1.218 +UCharCharacterIterator::last32() { 1.219 + pos = end; 1.220 + if(pos > begin) { 1.221 + UChar32 c; 1.222 + U16_PREV(text, begin, pos, c); 1.223 + return c; 1.224 + } else { 1.225 + return DONE; 1.226 + } 1.227 +} 1.228 + 1.229 +UChar32 1.230 +UCharCharacterIterator::setIndex32(int32_t position) { 1.231 + if(position < begin) { 1.232 + position = begin; 1.233 + } else if(position > end) { 1.234 + position = end; 1.235 + } 1.236 + if(position < end) { 1.237 + U16_SET_CP_START(text, begin, position); 1.238 + int32_t i = this->pos = position; 1.239 + UChar32 c; 1.240 + U16_NEXT(text, i, end, c); 1.241 + return c; 1.242 + } else { 1.243 + this->pos = position; 1.244 + return DONE; 1.245 + } 1.246 +} 1.247 + 1.248 +UChar32 1.249 +UCharCharacterIterator::current32() const { 1.250 + if (pos >= begin && pos < end) { 1.251 + UChar32 c; 1.252 + U16_GET(text, begin, pos, end, c); 1.253 + return c; 1.254 + } else { 1.255 + return DONE; 1.256 + } 1.257 +} 1.258 + 1.259 +UChar32 1.260 +UCharCharacterIterator::next32() { 1.261 + if (pos < end) { 1.262 + U16_FWD_1(text, pos, end); 1.263 + if(pos < end) { 1.264 + int32_t i = pos; 1.265 + UChar32 c; 1.266 + U16_NEXT(text, i, end, c); 1.267 + return c; 1.268 + } 1.269 + } 1.270 + /* make current() return DONE */ 1.271 + pos = end; 1.272 + return DONE; 1.273 +} 1.274 + 1.275 +UChar32 1.276 +UCharCharacterIterator::next32PostInc() { 1.277 + if (pos < end) { 1.278 + UChar32 c; 1.279 + U16_NEXT(text, pos, end, c); 1.280 + return c; 1.281 + } else { 1.282 + return DONE; 1.283 + } 1.284 +} 1.285 + 1.286 +UChar32 1.287 +UCharCharacterIterator::previous32() { 1.288 + if (pos > begin) { 1.289 + UChar32 c; 1.290 + U16_PREV(text, begin, pos, c); 1.291 + return c; 1.292 + } else { 1.293 + return DONE; 1.294 + } 1.295 +} 1.296 + 1.297 +int32_t 1.298 +UCharCharacterIterator::move(int32_t delta, CharacterIterator::EOrigin origin) { 1.299 + switch(origin) { 1.300 + case kStart: 1.301 + pos = begin + delta; 1.302 + break; 1.303 + case kCurrent: 1.304 + pos += delta; 1.305 + break; 1.306 + case kEnd: 1.307 + pos = end + delta; 1.308 + break; 1.309 + default: 1.310 + break; 1.311 + } 1.312 + 1.313 + if(pos < begin) { 1.314 + pos = begin; 1.315 + } else if(pos > end) { 1.316 + pos = end; 1.317 + } 1.318 + 1.319 + return pos; 1.320 +} 1.321 + 1.322 +int32_t 1.323 +UCharCharacterIterator::move32(int32_t delta, CharacterIterator::EOrigin origin) { 1.324 + // this implementation relies on the "safe" version of the UTF macros 1.325 + // (or the trustworthiness of the caller) 1.326 + switch(origin) { 1.327 + case kStart: 1.328 + pos = begin; 1.329 + if(delta > 0) { 1.330 + U16_FWD_N(text, pos, end, delta); 1.331 + } 1.332 + break; 1.333 + case kCurrent: 1.334 + if(delta > 0) { 1.335 + U16_FWD_N(text, pos, end, delta); 1.336 + } else { 1.337 + U16_BACK_N(text, begin, pos, -delta); 1.338 + } 1.339 + break; 1.340 + case kEnd: 1.341 + pos = end; 1.342 + if(delta < 0) { 1.343 + U16_BACK_N(text, begin, pos, -delta); 1.344 + } 1.345 + break; 1.346 + default: 1.347 + break; 1.348 + } 1.349 + 1.350 + return pos; 1.351 +} 1.352 + 1.353 +void UCharCharacterIterator::setText(const UChar* newText, 1.354 + int32_t newTextLength) { 1.355 + text = newText; 1.356 + if(newText == 0 || newTextLength < 0) { 1.357 + newTextLength = 0; 1.358 + } 1.359 + end = textLength = newTextLength; 1.360 + pos = begin = 0; 1.361 +} 1.362 + 1.363 +void 1.364 +UCharCharacterIterator::getText(UnicodeString& result) { 1.365 + result = UnicodeString(text, textLength); 1.366 +} 1.367 + 1.368 +U_NAMESPACE_END