1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
5 * Copyright (C) 2009-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 *******************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
19 // utility FieldPositionHandler
20 // base class, null implementation
22 FieldPositionHandler::~FieldPositionHandler() {
25 void FieldPositionHandler::setShift(int32_t delta
) {
30 // utility subclass FieldPositionOnlyHandler
32 FieldPositionOnlyHandler::FieldPositionOnlyHandler(FieldPosition
& _pos
)
36 FieldPositionOnlyHandler::~FieldPositionOnlyHandler() {
40 FieldPositionOnlyHandler::addAttribute(int32_t id
, int32_t start
, int32_t limit
) {
41 if (pos
.getField() == id
&& (!acceptFirstOnly
|| !seenFirst
)) {
43 pos
.setBeginIndex(start
+ fShift
);
44 pos
.setEndIndex(limit
+ fShift
);
49 FieldPositionOnlyHandler::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());
57 FieldPositionOnlyHandler::isRecording(void) const {
58 return pos
.getField() != FieldPosition::DONT_CARE
;
61 void FieldPositionOnlyHandler::setAcceptFirstOnly(UBool acceptFirstOnly
) {
62 this->acceptFirstOnly
= acceptFirstOnly
;
66 // utility subclass FieldPositionIteratorHandler
68 FieldPositionIteratorHandler::FieldPositionIteratorHandler(FieldPositionIterator
* posIter
,
70 : iter(posIter
), vec(NULL
), status(_status
), fCategory(UFIELD_CATEGORY_UNDEFINED
) {
71 if (iter
&& U_SUCCESS(status
)) {
72 vec
= new UVector32(status
);
76 FieldPositionIteratorHandler::FieldPositionIteratorHandler(
79 : iter(nullptr), vec(vec
), status(status
), fCategory(UFIELD_CATEGORY_UNDEFINED
) {
82 FieldPositionIteratorHandler::~FieldPositionIteratorHandler() {
83 // setData adopts the vec regardless of status, so it's safe to null it
85 iter
->setData(vec
, status
);
87 // if iter is null, we never allocated vec, so no need to free it
92 FieldPositionIteratorHandler::addAttribute(int32_t id
, int32_t start
, int32_t limit
) {
93 if (vec
&& U_SUCCESS(status
) && start
< limit
) {
94 int32_t size
= vec
->size();
95 vec
->addElement(fCategory
, status
);
96 vec
->addElement(id
, status
);
97 vec
->addElement(start
+ fShift
, status
);
98 vec
->addElement(limit
+ fShift
, status
);
99 if (!U_SUCCESS(status
)) {
106 FieldPositionIteratorHandler::shiftLast(int32_t delta
) {
107 if (U_SUCCESS(status
) && delta
!= 0) {
108 int32_t i
= vec
->size();
111 vec
->setElementAt(delta
+ vec
->elementAti(i
), i
);
113 vec
->setElementAt(delta
+ vec
->elementAti(i
), i
);
119 FieldPositionIteratorHandler::isRecording(void) const {
120 return U_SUCCESS(status
);
125 #endif /* !UCONFIG_NO_FORMATTING */