Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : /*
4 : *******************************************************************************
5 : * Copyright (C) 2009-2015, International Business Machines Corporation and
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 :
17 : U_NAMESPACE_BEGIN
18 :
19 : // utility FieldPositionHandler
20 : // base class, null implementation
21 :
22 0 : FieldPositionHandler::~FieldPositionHandler() {
23 0 : }
24 :
25 : void
26 0 : FieldPositionHandler::addAttribute(int32_t, int32_t, int32_t) {
27 0 : }
28 :
29 : void
30 0 : FieldPositionHandler::shiftLast(int32_t) {
31 0 : }
32 :
33 : UBool
34 0 : FieldPositionHandler::isRecording(void) const {
35 0 : return FALSE;
36 : }
37 :
38 :
39 : // utility subclass FieldPositionOnlyHandler
40 :
41 0 : FieldPositionOnlyHandler::FieldPositionOnlyHandler(FieldPosition& _pos)
42 0 : : pos(_pos) {
43 0 : }
44 :
45 0 : FieldPositionOnlyHandler::~FieldPositionOnlyHandler() {
46 0 : }
47 :
48 : void
49 0 : FieldPositionOnlyHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
50 0 : if (pos.getField() == id) {
51 0 : pos.setBeginIndex(start);
52 0 : pos.setEndIndex(limit);
53 : }
54 0 : }
55 :
56 : void
57 0 : FieldPositionOnlyHandler::shiftLast(int32_t delta) {
58 0 : if (delta != 0 && pos.getField() != FieldPosition::DONT_CARE && pos.getBeginIndex() != -1) {
59 0 : pos.setBeginIndex(delta + pos.getBeginIndex());
60 0 : pos.setEndIndex(delta + pos.getEndIndex());
61 : }
62 0 : }
63 :
64 : UBool
65 0 : FieldPositionOnlyHandler::isRecording(void) const {
66 0 : return pos.getField() != FieldPosition::DONT_CARE;
67 : }
68 :
69 :
70 : // utility subclass FieldPositionIteratorHandler
71 :
72 0 : FieldPositionIteratorHandler::FieldPositionIteratorHandler(FieldPositionIterator* posIter,
73 0 : UErrorCode& _status)
74 0 : : iter(posIter), vec(NULL), status(_status) {
75 0 : if (iter && U_SUCCESS(status)) {
76 0 : vec = new UVector32(status);
77 : }
78 0 : }
79 :
80 0 : FieldPositionIteratorHandler::~FieldPositionIteratorHandler() {
81 : // setData adopts the vec regardless of status, so it's safe to null it
82 0 : if (iter) {
83 0 : iter->setData(vec, status);
84 : }
85 : // if iter is null, we never allocated vec, so no need to free it
86 0 : vec = NULL;
87 0 : }
88 :
89 : void
90 0 : FieldPositionIteratorHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
91 0 : if (iter && U_SUCCESS(status) && start < limit) {
92 0 : int32_t size = vec->size();
93 0 : vec->addElement(id, status);
94 0 : vec->addElement(start, status);
95 0 : vec->addElement(limit, status);
96 0 : if (!U_SUCCESS(status)) {
97 0 : vec->setSize(size);
98 : }
99 : }
100 0 : }
101 :
102 : void
103 0 : FieldPositionIteratorHandler::shiftLast(int32_t delta) {
104 0 : if (U_SUCCESS(status) && delta != 0) {
105 0 : int32_t i = vec->size();
106 0 : if (i > 0) {
107 0 : --i;
108 0 : vec->setElementAt(delta + vec->elementAti(i), i);
109 0 : --i;
110 0 : vec->setElementAt(delta + vec->elementAti(i), i);
111 : }
112 : }
113 0 : }
114 :
115 : UBool
116 0 : FieldPositionIteratorHandler::isRecording(void) const {
117 0 : return U_SUCCESS(status);
118 : }
119 :
120 : U_NAMESPACE_END
121 :
122 : #endif /* !UCONFIG_NO_FORMATTING */
|