]> git.saurik.com Git - apple/icu.git/blame - icuSources/i18n/fphdlimp.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / i18n / fphdlimp.cpp
CommitLineData
729e4ab9
A
1/*
2*******************************************************************************
3* Copyright (C) 2009-2010, International Business Machines Corporation and
4* others. All Rights Reserved.
5*******************************************************************************
6*/
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_FORMATTING
11
12#include "fphdlimp.h"
13#include "uvectr32.h"
14
15U_NAMESPACE_BEGIN
16
17// utility FieldPositionHandler
18// base class, null implementation
19
20FieldPositionHandler::~FieldPositionHandler() {
21}
22
23void
24FieldPositionHandler::addAttribute(int32_t, int32_t, int32_t) {
25}
26
27void
28FieldPositionHandler::shiftLast(int32_t) {
29}
30
31UBool
32FieldPositionHandler::isRecording(void) {
33 return FALSE;
34}
35
36
37// utility subclass FieldPositionOnlyHandler
38
39FieldPositionOnlyHandler::FieldPositionOnlyHandler(FieldPosition& _pos)
40 : pos(_pos) {
41}
42
43FieldPositionOnlyHandler::~FieldPositionOnlyHandler() {
44}
45
46void
47FieldPositionOnlyHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
48 if (pos.getField() == id) {
49 pos.setBeginIndex(start);
50 pos.setEndIndex(limit);
51 }
52}
53
54void
55FieldPositionOnlyHandler::shiftLast(int32_t delta) {
56 if (delta != 0 && pos.getField() != FieldPosition::DONT_CARE && pos.getBeginIndex() != -1) {
57 pos.setBeginIndex(delta + pos.getBeginIndex());
58 pos.setEndIndex(delta + pos.getEndIndex());
59 }
60}
61
62UBool
63FieldPositionOnlyHandler::isRecording(void) {
64 return pos.getField() != FieldPosition::DONT_CARE;
65}
66
67
68// utility subclass FieldPositionIteratorHandler
69
70FieldPositionIteratorHandler::FieldPositionIteratorHandler(FieldPositionIterator* posIter,
71 UErrorCode& _status)
72 : iter(posIter), vec(NULL), status(_status) {
73 if (iter && U_SUCCESS(status)) {
74 vec = new UVector32(status);
75 }
76}
77
78FieldPositionIteratorHandler::~FieldPositionIteratorHandler() {
79 // setData adopts the vec regardless of status, so it's safe to null it
80 if (iter) {
81 iter->setData(vec, status);
82 }
83 // if iter is null, we never allocated vec, so no need to free it
84 vec = NULL;
85}
86
87void
88FieldPositionIteratorHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
89 if (iter && U_SUCCESS(status) && start < limit) {
90 int32_t size = vec->size();
91 vec->addElement(id, status);
92 vec->addElement(start, status);
93 vec->addElement(limit, status);
94 if (!U_SUCCESS(status)) {
95 vec->setSize(size);
96 }
97 }
98}
99
100void
101FieldPositionIteratorHandler::shiftLast(int32_t delta) {
102 if (U_SUCCESS(status) && delta != 0) {
103 int32_t i = vec->size();
104 if (i > 0) {
105 --i;
106 vec->setElementAt(delta + vec->elementAti(i), i);
107 --i;
108 vec->setElementAt(delta + vec->elementAti(i), i);
109 }
110 }
111}
112
113UBool
114FieldPositionIteratorHandler::isRecording(void) {
115 return U_SUCCESS(status);
116}
117
118U_NAMESPACE_END
119
120#endif /* !UCONFIG_NO_FORMATTING */