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