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.
23const int32_t SimpleFwdCharIterator::kInvalidHashCode = 0;
24const int32_t SimpleFwdCharIterator::kEmptyHashCode = 1;
25
26#if 0 // not used
27SimpleFwdCharIterator::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
44SimpleFwdCharIterator::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
68SimpleFwdCharIterator::~SimpleFwdCharIterator() {
69    delete[] fStart;
70}
71
72#if 0 // not used
73bool 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
94int32_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
104UClassID SimpleFwdCharIterator::getDynamicClassID(void) const {
105    return NULL;
106}
107
108UChar SimpleFwdCharIterator::nextPostInc(void) {
109    if(fCurrent == fEnd) {
110        return ForwardCharacterIterator::DONE;
111    } else {
112        return *(fCurrent)++;
113    }
114}
115
116UChar32 SimpleFwdCharIterator::next32PostInc(void) {
117    return ForwardCharacterIterator::DONE;
118}
119
120UBool SimpleFwdCharIterator::hasNext() {
121    return fCurrent < fEnd;
122}
123