1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4  * COPYRIGHT:
5  * Copyright (c) 1997-2003, International Business Machines Corporation and
6  * others. All Rights Reserved.
7  ********************************************************************/
8 /*   file name:  sfwdchit.cpp
9 *   encoding:   UTF-8
10 *   tab size:   8 (not used)
11 *   indentation:4
12 */
13 
14 #include "sfwdchit.h"
15 #include "unicode/ustring.h"
16 #include "unicode/unistr.h"
17 #include "uhash.h"
18 #include "cmemory.h"
19 
20 // A hash code of kInvalidHashCode indicates that the has code needs
21 // to be computed. A hash code of kEmptyHashCode is used for empty keys
22 // and for any key whose computed hash code is kInvalidHashCode.
23 const int32_t SimpleFwdCharIterator::kInvalidHashCode = 0;
24 const int32_t SimpleFwdCharIterator::kEmptyHashCode = 1;
25 
26 #if 0 // not used
27 SimpleFwdCharIterator::SimpleFwdCharIterator(const UnicodeString& s) {
28 
29     fHashCode = kInvalidHashCode;
30     fLen = s.length();
31     fStart = new UChar[fLen];
32     if(fStart == NULL) {
33         fBogus = TRUE;
34     } else {
35         fEnd = fStart+fLen;
36         fCurrent = fStart;
37         fBogus = FALSE;
38         s.extract(0, fLen, fStart);
39     }
40 
41 }
42 #endif
43 
SimpleFwdCharIterator(UChar * s,int32_t len,UBool adopt)44 SimpleFwdCharIterator::SimpleFwdCharIterator(UChar *s, int32_t len, UBool adopt) {
45 
46     fHashCode = kInvalidHashCode;
47 
48     fLen = len==-1 ? u_strlen(s) : len;
49 
50     if(adopt == FALSE) {
51         fStart = new UChar[fLen];
52         if(fStart == NULL) {
53             fBogus = TRUE;
54         } else {
55             uprv_memcpy(fStart, s, fLen);
56             fEnd = fStart+fLen;
57             fCurrent = fStart;
58             fBogus = FALSE;
59         }
60     } else { // adopt = TRUE
61         fCurrent = fStart = s;
62         fEnd = fStart + fLen;
63         fBogus = FALSE;
64     }
65 
66 }
67 
~SimpleFwdCharIterator()68 SimpleFwdCharIterator::~SimpleFwdCharIterator() {
69     delete[] fStart;
70 }
71 
72 #if 0 // not used
73 UBool SimpleFwdCharIterator::operator==(const ForwardCharacterIterator& that) const {
74     if(this == &that) {
75         return TRUE;
76     }
77 /*
78     if(that->fHashCode != kInvalidHashCode && this->fHashCode = that->fHashCode) {
79         return TRUE;
80     }
81 
82     if(this->fStart == that->fStart) {
83         return TRUE;
84     }
85 
86     if(this->fLen == that->fLen && uprv_memcmp(this->fStart, that->fStart, this->fLen) {
87         return TRUE;
88     }
89 */
90     return FALSE;
91 }
92 #endif
93 
hashCode(void) const94 int32_t SimpleFwdCharIterator::hashCode(void) const {
95     if (fHashCode == kInvalidHashCode)
96     {
97         UHashTok key;
98         key.pointer = fStart;
99         ((SimpleFwdCharIterator *)this)->fHashCode = uhash_hashUChars(key);
100     }
101     return fHashCode;
102 }
103 
getDynamicClassID(void) const104 UClassID SimpleFwdCharIterator::getDynamicClassID(void) const {
105     return NULL;
106 }
107 
nextPostInc(void)108 UChar SimpleFwdCharIterator::nextPostInc(void) {
109     if(fCurrent == fEnd) {
110         return ForwardCharacterIterator::DONE;
111     } else {
112         return *(fCurrent)++;
113     }
114 }
115 
next32PostInc(void)116 UChar32 SimpleFwdCharIterator::next32PostInc(void) {
117     return ForwardCharacterIterator::DONE;
118 }
119 
hasNext()120 UBool SimpleFwdCharIterator::hasNext() {
121     return fCurrent < fEnd;
122 }
123