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