12e5b6d6dSopenharmony_ci// © 2016 and later: Unicode, Inc. and others. 22e5b6d6dSopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html 32e5b6d6dSopenharmony_ci/******************************************************************** 42e5b6d6dSopenharmony_ci * COPYRIGHT: 52e5b6d6dSopenharmony_ci * Copyright (c) 1997-2003, International Business Machines Corporation and 62e5b6d6dSopenharmony_ci * others. All Rights Reserved. 72e5b6d6dSopenharmony_ci ********************************************************************/ 82e5b6d6dSopenharmony_ci/* file name: sfwdchit.cpp 92e5b6d6dSopenharmony_ci* encoding: UTF-8 102e5b6d6dSopenharmony_ci* tab size: 8 (not used) 112e5b6d6dSopenharmony_ci* indentation:4 122e5b6d6dSopenharmony_ci*/ 132e5b6d6dSopenharmony_ci 142e5b6d6dSopenharmony_ci#include "sfwdchit.h" 152e5b6d6dSopenharmony_ci#include "unicode/ustring.h" 162e5b6d6dSopenharmony_ci#include "unicode/unistr.h" 172e5b6d6dSopenharmony_ci#include "uhash.h" 182e5b6d6dSopenharmony_ci#include "cmemory.h" 192e5b6d6dSopenharmony_ci 202e5b6d6dSopenharmony_ci// A hash code of kInvalidHashCode indicates that the has code needs 212e5b6d6dSopenharmony_ci// to be computed. A hash code of kEmptyHashCode is used for empty keys 222e5b6d6dSopenharmony_ci// and for any key whose computed hash code is kInvalidHashCode. 232e5b6d6dSopenharmony_ciconst int32_t SimpleFwdCharIterator::kInvalidHashCode = 0; 242e5b6d6dSopenharmony_ciconst int32_t SimpleFwdCharIterator::kEmptyHashCode = 1; 252e5b6d6dSopenharmony_ci 262e5b6d6dSopenharmony_ci#if 0 // not used 272e5b6d6dSopenharmony_ciSimpleFwdCharIterator::SimpleFwdCharIterator(const UnicodeString& s) { 282e5b6d6dSopenharmony_ci 292e5b6d6dSopenharmony_ci fHashCode = kInvalidHashCode; 302e5b6d6dSopenharmony_ci fLen = s.length(); 312e5b6d6dSopenharmony_ci fStart = new UChar[fLen]; 322e5b6d6dSopenharmony_ci if(fStart == NULL) { 332e5b6d6dSopenharmony_ci fBogus = true; 342e5b6d6dSopenharmony_ci } else { 352e5b6d6dSopenharmony_ci fEnd = fStart+fLen; 362e5b6d6dSopenharmony_ci fCurrent = fStart; 372e5b6d6dSopenharmony_ci fBogus = false; 382e5b6d6dSopenharmony_ci s.extract(0, fLen, fStart); 392e5b6d6dSopenharmony_ci } 402e5b6d6dSopenharmony_ci 412e5b6d6dSopenharmony_ci} 422e5b6d6dSopenharmony_ci#endif 432e5b6d6dSopenharmony_ci 442e5b6d6dSopenharmony_ciSimpleFwdCharIterator::SimpleFwdCharIterator(UChar *s, int32_t len, UBool adopt) { 452e5b6d6dSopenharmony_ci 462e5b6d6dSopenharmony_ci fHashCode = kInvalidHashCode; 472e5b6d6dSopenharmony_ci 482e5b6d6dSopenharmony_ci fLen = len==-1 ? u_strlen(s) : len; 492e5b6d6dSopenharmony_ci 502e5b6d6dSopenharmony_ci if(adopt == false) { 512e5b6d6dSopenharmony_ci fStart = new UChar[fLen]; 522e5b6d6dSopenharmony_ci if(fStart == NULL) { 532e5b6d6dSopenharmony_ci fBogus = true; 542e5b6d6dSopenharmony_ci } else { 552e5b6d6dSopenharmony_ci uprv_memcpy(fStart, s, fLen); 562e5b6d6dSopenharmony_ci fEnd = fStart+fLen; 572e5b6d6dSopenharmony_ci fCurrent = fStart; 582e5b6d6dSopenharmony_ci fBogus = false; 592e5b6d6dSopenharmony_ci } 602e5b6d6dSopenharmony_ci } else { // adopt = true 612e5b6d6dSopenharmony_ci fCurrent = fStart = s; 622e5b6d6dSopenharmony_ci fEnd = fStart + fLen; 632e5b6d6dSopenharmony_ci fBogus = false; 642e5b6d6dSopenharmony_ci } 652e5b6d6dSopenharmony_ci 662e5b6d6dSopenharmony_ci} 672e5b6d6dSopenharmony_ci 682e5b6d6dSopenharmony_ciSimpleFwdCharIterator::~SimpleFwdCharIterator() { 692e5b6d6dSopenharmony_ci delete[] fStart; 702e5b6d6dSopenharmony_ci} 712e5b6d6dSopenharmony_ci 722e5b6d6dSopenharmony_ci#if 0 // not used 732e5b6d6dSopenharmony_cibool SimpleFwdCharIterator::operator==(const ForwardCharacterIterator& that) const { 742e5b6d6dSopenharmony_ci if(this == &that) { 752e5b6d6dSopenharmony_ci return true; 762e5b6d6dSopenharmony_ci } 772e5b6d6dSopenharmony_ci/* 782e5b6d6dSopenharmony_ci if(that->fHashCode != kInvalidHashCode && this->fHashCode = that->fHashCode) { 792e5b6d6dSopenharmony_ci return true; 802e5b6d6dSopenharmony_ci } 812e5b6d6dSopenharmony_ci 822e5b6d6dSopenharmony_ci if(this->fStart == that->fStart) { 832e5b6d6dSopenharmony_ci return true; 842e5b6d6dSopenharmony_ci } 852e5b6d6dSopenharmony_ci 862e5b6d6dSopenharmony_ci if(this->fLen == that->fLen && uprv_memcmp(this->fStart, that->fStart, this->fLen) { 872e5b6d6dSopenharmony_ci return true; 882e5b6d6dSopenharmony_ci } 892e5b6d6dSopenharmony_ci*/ 902e5b6d6dSopenharmony_ci return false; 912e5b6d6dSopenharmony_ci} 922e5b6d6dSopenharmony_ci#endif 932e5b6d6dSopenharmony_ci 942e5b6d6dSopenharmony_ciint32_t SimpleFwdCharIterator::hashCode(void) const { 952e5b6d6dSopenharmony_ci if (fHashCode == kInvalidHashCode) 962e5b6d6dSopenharmony_ci { 972e5b6d6dSopenharmony_ci UHashTok key; 982e5b6d6dSopenharmony_ci key.pointer = fStart; 992e5b6d6dSopenharmony_ci ((SimpleFwdCharIterator *)this)->fHashCode = uhash_hashUChars(key); 1002e5b6d6dSopenharmony_ci } 1012e5b6d6dSopenharmony_ci return fHashCode; 1022e5b6d6dSopenharmony_ci} 1032e5b6d6dSopenharmony_ci 1042e5b6d6dSopenharmony_ciUClassID SimpleFwdCharIterator::getDynamicClassID(void) const { 1052e5b6d6dSopenharmony_ci return NULL; 1062e5b6d6dSopenharmony_ci} 1072e5b6d6dSopenharmony_ci 1082e5b6d6dSopenharmony_ciUChar SimpleFwdCharIterator::nextPostInc(void) { 1092e5b6d6dSopenharmony_ci if(fCurrent == fEnd) { 1102e5b6d6dSopenharmony_ci return ForwardCharacterIterator::DONE; 1112e5b6d6dSopenharmony_ci } else { 1122e5b6d6dSopenharmony_ci return *(fCurrent)++; 1132e5b6d6dSopenharmony_ci } 1142e5b6d6dSopenharmony_ci} 1152e5b6d6dSopenharmony_ci 1162e5b6d6dSopenharmony_ciUChar32 SimpleFwdCharIterator::next32PostInc(void) { 1172e5b6d6dSopenharmony_ci return ForwardCharacterIterator::DONE; 1182e5b6d6dSopenharmony_ci} 1192e5b6d6dSopenharmony_ci 1202e5b6d6dSopenharmony_ciUBool SimpleFwdCharIterator::hasNext() { 1212e5b6d6dSopenharmony_ci return fCurrent < fEnd; 1222e5b6d6dSopenharmony_ci} 123