Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ********************************************************************** |
michael@0 | 3 | * Copyright (c) 2002-2006, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ********************************************************************** |
michael@0 | 6 | */ |
michael@0 | 7 | #include "unicode/usetiter.h" |
michael@0 | 8 | #include "unicode/uniset.h" |
michael@0 | 9 | #include "unicode/unistr.h" |
michael@0 | 10 | #include "uvector.h" |
michael@0 | 11 | |
michael@0 | 12 | U_NAMESPACE_BEGIN |
michael@0 | 13 | |
michael@0 | 14 | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UnicodeSetIterator) |
michael@0 | 15 | |
michael@0 | 16 | /** |
michael@0 | 17 | * Create an iterator |
michael@0 | 18 | * @param set set to iterate over |
michael@0 | 19 | */ |
michael@0 | 20 | UnicodeSetIterator::UnicodeSetIterator(const UnicodeSet& uSet) { |
michael@0 | 21 | cpString = NULL; |
michael@0 | 22 | reset(uSet); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * Create an iterator. Convenience for when the contents are to be set later. |
michael@0 | 27 | */ |
michael@0 | 28 | UnicodeSetIterator::UnicodeSetIterator() { |
michael@0 | 29 | this->set = NULL; |
michael@0 | 30 | cpString = NULL; |
michael@0 | 31 | reset(); |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | UnicodeSetIterator::~UnicodeSetIterator() { |
michael@0 | 35 | delete cpString; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | /** |
michael@0 | 39 | * Returns the next element in the set. |
michael@0 | 40 | * @return true if there was another element in the set. |
michael@0 | 41 | * if so, if codepoint == IS_STRING, the value is a string in the string field |
michael@0 | 42 | * else the value is a single code point in the codepoint field. |
michael@0 | 43 | * <br>You are guaranteed that the codepoints are in sorted order, and the strings are in sorted order, |
michael@0 | 44 | * and that all code points are returned before any strings are returned. |
michael@0 | 45 | * <br>Note also that the codepointEnd is undefined after calling this method. |
michael@0 | 46 | */ |
michael@0 | 47 | UBool UnicodeSetIterator::next() { |
michael@0 | 48 | if (nextElement <= endElement) { |
michael@0 | 49 | codepoint = codepointEnd = nextElement++; |
michael@0 | 50 | string = NULL; |
michael@0 | 51 | return TRUE; |
michael@0 | 52 | } |
michael@0 | 53 | if (range < endRange) { |
michael@0 | 54 | loadRange(++range); |
michael@0 | 55 | codepoint = codepointEnd = nextElement++; |
michael@0 | 56 | string = NULL; |
michael@0 | 57 | return TRUE; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | if (nextString >= stringCount) return FALSE; |
michael@0 | 61 | codepoint = (UChar32)IS_STRING; // signal that value is actually a string |
michael@0 | 62 | string = (const UnicodeString*) set->strings->elementAt(nextString++); |
michael@0 | 63 | return TRUE; |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | /** |
michael@0 | 67 | * @return true if there was another element in the set. |
michael@0 | 68 | * if so, if codepoint == IS_STRING, the value is a string in the string field |
michael@0 | 69 | * else the value is a range of codepoints in the <codepoint, codepointEnd> fields. |
michael@0 | 70 | * <br>Note that the codepoints are in sorted order, and the strings are in sorted order, |
michael@0 | 71 | * and that all code points are returned before any strings are returned. |
michael@0 | 72 | * <br>You are guaranteed that the ranges are in sorted order, and the strings are in sorted order, |
michael@0 | 73 | * and that all ranges are returned before any strings are returned. |
michael@0 | 74 | * <br>You are also guaranteed that ranges are disjoint and non-contiguous. |
michael@0 | 75 | * <br>Note also that the codepointEnd is undefined after calling this method. |
michael@0 | 76 | */ |
michael@0 | 77 | UBool UnicodeSetIterator::nextRange() { |
michael@0 | 78 | string = NULL; |
michael@0 | 79 | if (nextElement <= endElement) { |
michael@0 | 80 | codepointEnd = endElement; |
michael@0 | 81 | codepoint = nextElement; |
michael@0 | 82 | nextElement = endElement+1; |
michael@0 | 83 | return TRUE; |
michael@0 | 84 | } |
michael@0 | 85 | if (range < endRange) { |
michael@0 | 86 | loadRange(++range); |
michael@0 | 87 | codepointEnd = endElement; |
michael@0 | 88 | codepoint = nextElement; |
michael@0 | 89 | nextElement = endElement+1; |
michael@0 | 90 | return TRUE; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | if (nextString >= stringCount) return FALSE; |
michael@0 | 94 | codepoint = (UChar32)IS_STRING; // signal that value is actually a string |
michael@0 | 95 | string = (const UnicodeString*) set->strings->elementAt(nextString++); |
michael@0 | 96 | return TRUE; |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | /** |
michael@0 | 100 | *@param set the set to iterate over. This allows reuse of the iterator. |
michael@0 | 101 | */ |
michael@0 | 102 | void UnicodeSetIterator::reset(const UnicodeSet& uSet) { |
michael@0 | 103 | this->set = &uSet; |
michael@0 | 104 | reset(); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * Resets to the start, to allow the iteration to start over again. |
michael@0 | 109 | */ |
michael@0 | 110 | void UnicodeSetIterator::reset() { |
michael@0 | 111 | if (set == NULL) { |
michael@0 | 112 | // Set up indices to empty iteration |
michael@0 | 113 | endRange = -1; |
michael@0 | 114 | stringCount = 0; |
michael@0 | 115 | } else { |
michael@0 | 116 | endRange = set->getRangeCount() - 1; |
michael@0 | 117 | stringCount = set->strings->size(); |
michael@0 | 118 | } |
michael@0 | 119 | range = 0; |
michael@0 | 120 | endElement = -1; |
michael@0 | 121 | nextElement = 0; |
michael@0 | 122 | if (endRange >= 0) { |
michael@0 | 123 | loadRange(range); |
michael@0 | 124 | } |
michael@0 | 125 | nextString = 0; |
michael@0 | 126 | string = NULL; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | void UnicodeSetIterator::loadRange(int32_t iRange) { |
michael@0 | 130 | nextElement = set->getRangeStart(iRange); |
michael@0 | 131 | endElement = set->getRangeEnd(iRange); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | |
michael@0 | 135 | const UnicodeString& UnicodeSetIterator::getString() { |
michael@0 | 136 | if (string==NULL && codepoint!=(UChar32)IS_STRING) { |
michael@0 | 137 | if (cpString == NULL) { |
michael@0 | 138 | cpString = new UnicodeString(); |
michael@0 | 139 | } |
michael@0 | 140 | if (cpString != NULL) { |
michael@0 | 141 | cpString->setTo((UChar32)codepoint); |
michael@0 | 142 | } |
michael@0 | 143 | string = cpString; |
michael@0 | 144 | } |
michael@0 | 145 | return *string; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | U_NAMESPACE_END |
michael@0 | 149 | |
michael@0 | 150 | //eof |