Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * Copyright (C) 2009-2012, International Business Machines Corporation and |
michael@0 | 4 | * others. All Rights Reserved. |
michael@0 | 5 | ****************************************************************************** |
michael@0 | 6 | * Date Name Description |
michael@0 | 7 | * 12/14/09 doug Creation. |
michael@0 | 8 | ****************************************************************************** |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "unicode/utypes.h" |
michael@0 | 12 | |
michael@0 | 13 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 14 | |
michael@0 | 15 | #include "unicode/fpositer.h" |
michael@0 | 16 | #include "cmemory.h" |
michael@0 | 17 | #include "uvectr32.h" |
michael@0 | 18 | |
michael@0 | 19 | U_NAMESPACE_BEGIN |
michael@0 | 20 | |
michael@0 | 21 | FieldPositionIterator::~FieldPositionIterator() { |
michael@0 | 22 | delete data; |
michael@0 | 23 | data = NULL; |
michael@0 | 24 | pos = -1; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | FieldPositionIterator::FieldPositionIterator() |
michael@0 | 28 | : data(NULL), pos(-1) { |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs) |
michael@0 | 32 | : UObject(rhs), data(NULL), pos(rhs.pos) { |
michael@0 | 33 | |
michael@0 | 34 | if (rhs.data) { |
michael@0 | 35 | UErrorCode status = U_ZERO_ERROR; |
michael@0 | 36 | data = new UVector32(status); |
michael@0 | 37 | data->assign(*rhs.data, status); |
michael@0 | 38 | if (status != U_ZERO_ERROR) { |
michael@0 | 39 | delete data; |
michael@0 | 40 | data = NULL; |
michael@0 | 41 | pos = -1; |
michael@0 | 42 | } |
michael@0 | 43 | } |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | UBool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const { |
michael@0 | 47 | if (&rhs == this) { |
michael@0 | 48 | return TRUE; |
michael@0 | 49 | } |
michael@0 | 50 | if (pos != rhs.pos) { |
michael@0 | 51 | return FALSE; |
michael@0 | 52 | } |
michael@0 | 53 | if (!data) { |
michael@0 | 54 | return rhs.data == NULL; |
michael@0 | 55 | } |
michael@0 | 56 | return rhs.data ? data->operator==(*rhs.data) : FALSE; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) { |
michael@0 | 60 | // Verify that adopt has valid data, and update status if it doesn't. |
michael@0 | 61 | if (U_SUCCESS(status)) { |
michael@0 | 62 | if (adopt) { |
michael@0 | 63 | if ((adopt->size() % 3) != 0) { |
michael@0 | 64 | status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 65 | } else { |
michael@0 | 66 | for (int i = 1; i < adopt->size(); i += 3) { |
michael@0 | 67 | if (adopt->elementAti(i) >= adopt->elementAti(i+1)) { |
michael@0 | 68 | status = U_ILLEGAL_ARGUMENT_ERROR; |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // We own the data, even if status is in error, so we need to delete it now |
michael@0 | 77 | // if we're not keeping track of it. |
michael@0 | 78 | if (!U_SUCCESS(status)) { |
michael@0 | 79 | delete adopt; |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | delete data; |
michael@0 | 84 | data = adopt; |
michael@0 | 85 | pos = adopt == NULL ? -1 : 0; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | UBool FieldPositionIterator::next(FieldPosition& fp) { |
michael@0 | 89 | if (pos == -1) { |
michael@0 | 90 | return FALSE; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | fp.setField(data->elementAti(pos++)); |
michael@0 | 94 | fp.setBeginIndex(data->elementAti(pos++)); |
michael@0 | 95 | fp.setEndIndex(data->elementAti(pos++)); |
michael@0 | 96 | |
michael@0 | 97 | if (pos == data->size()) { |
michael@0 | 98 | pos = -1; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | return TRUE; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | U_NAMESPACE_END |
michael@0 | 105 | |
michael@0 | 106 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 107 |