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