]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/fpositer.cpp
ICU-511.25.tar.gz
[apple/icu.git] / icuSources / i18n / fpositer.cpp
CommitLineData
729e4ab9
A
1/*
2******************************************************************************
51004dcb 3* Copyright (C) 2009-2012, International Business Machines Corporation and
729e4ab9
A
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
19U_NAMESPACE_BEGIN
20
729e4ab9
A
21FieldPositionIterator::~FieldPositionIterator() {
22 delete data;
23 data = NULL;
24 pos = -1;
25}
26
27FieldPositionIterator::FieldPositionIterator()
28 : data(NULL), pos(-1) {
29}
30
31FieldPositionIterator::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
46UBool 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
59void 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
88UBool 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
104U_NAMESPACE_END
105
106#endif /* #if !UCONFIG_NO_FORMATTING */
107