/src/icu/icu4c/source/i18n/fpositer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ****************************************************************************** |
5 | | * Copyright (C) 2009-2012, International Business Machines Corporation and |
6 | | * others. All Rights Reserved. |
7 | | ****************************************************************************** |
8 | | * Date Name Description |
9 | | * 12/14/09 doug Creation. |
10 | | ****************************************************************************** |
11 | | */ |
12 | | |
13 | | #include "unicode/utypes.h" |
14 | | |
15 | | #if !UCONFIG_NO_FORMATTING |
16 | | |
17 | | #include "unicode/fpositer.h" |
18 | | #include "cmemory.h" |
19 | | #include "uvectr32.h" |
20 | | |
21 | | U_NAMESPACE_BEGIN |
22 | | |
23 | 0 | FieldPositionIterator::~FieldPositionIterator() { |
24 | 0 | delete data; |
25 | 0 | data = nullptr; |
26 | 0 | pos = -1; |
27 | 0 | } |
28 | | |
29 | | FieldPositionIterator::FieldPositionIterator() |
30 | 0 | : data(nullptr), pos(-1) { |
31 | 0 | } |
32 | | |
33 | | FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs) |
34 | 0 | : UObject(rhs), data(nullptr), pos(rhs.pos) { |
35 | |
|
36 | 0 | if (rhs.data) { |
37 | 0 | UErrorCode status = U_ZERO_ERROR; |
38 | 0 | data = new UVector32(status); |
39 | 0 | data->assign(*rhs.data, status); |
40 | 0 | if (status != U_ZERO_ERROR) { |
41 | 0 | delete data; |
42 | 0 | data = nullptr; |
43 | 0 | pos = -1; |
44 | 0 | } |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | 0 | bool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const { |
49 | 0 | if (&rhs == this) { |
50 | 0 | return true; |
51 | 0 | } |
52 | 0 | if (pos != rhs.pos) { |
53 | 0 | return false; |
54 | 0 | } |
55 | 0 | if (!data) { |
56 | 0 | return rhs.data == nullptr; |
57 | 0 | } |
58 | 0 | return rhs.data ? data->operator==(*rhs.data) : false; |
59 | 0 | } |
60 | | |
61 | 0 | void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) { |
62 | | // Verify that adopt has valid data, and update status if it doesn't. |
63 | 0 | if (U_SUCCESS(status)) { |
64 | 0 | if (adopt) { |
65 | 0 | if (adopt->size() == 0) { |
66 | 0 | delete adopt; |
67 | 0 | adopt = nullptr; |
68 | 0 | } else if ((adopt->size() % 4) != 0) { |
69 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
70 | 0 | } else { |
71 | 0 | for (int i = 2; i < adopt->size(); i += 4) { |
72 | 0 | if (adopt->elementAti(i) >= adopt->elementAti(i+1)) { |
73 | 0 | status = U_ILLEGAL_ARGUMENT_ERROR; |
74 | 0 | break; |
75 | 0 | } |
76 | 0 | } |
77 | 0 | } |
78 | 0 | } |
79 | 0 | } |
80 | | |
81 | | // We own the data, even if status is in error, so we need to delete it now |
82 | | // if we're not keeping track of it. |
83 | 0 | if (!U_SUCCESS(status)) { |
84 | 0 | delete adopt; |
85 | 0 | return; |
86 | 0 | } |
87 | | |
88 | 0 | delete data; |
89 | 0 | data = adopt; |
90 | 0 | pos = adopt == nullptr ? -1 : 0; |
91 | 0 | } |
92 | | |
93 | 0 | UBool FieldPositionIterator::next(FieldPosition& fp) { |
94 | 0 | if (pos == -1) { |
95 | 0 | return false; |
96 | 0 | } |
97 | | |
98 | | // Ignore the first element of the tetrad: used for field category |
99 | 0 | pos++; |
100 | 0 | fp.setField(data->elementAti(pos++)); |
101 | 0 | fp.setBeginIndex(data->elementAti(pos++)); |
102 | 0 | fp.setEndIndex(data->elementAti(pos++)); |
103 | |
|
104 | 0 | if (pos == data->size()) { |
105 | 0 | pos = -1; |
106 | 0 | } |
107 | |
|
108 | 0 | return true; |
109 | 0 | } |
110 | | |
111 | | U_NAMESPACE_END |
112 | | |
113 | | #endif /* #if !UCONFIG_NO_FORMATTING */ |
114 | | |