• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
~FieldPositionHandler()22 FieldPositionHandler::~FieldPositionHandler() {
23 }
24 
setShift(int32_t delta)25 void FieldPositionHandler::setShift(int32_t delta) {
26   fShift = delta;
27 }
28 
29 
30 // utility subclass FieldPositionOnlyHandler
31 
FieldPositionOnlyHandler(FieldPosition & _pos)32 FieldPositionOnlyHandler::FieldPositionOnlyHandler(FieldPosition& _pos)
33   : pos(_pos) {
34 }
35 
~FieldPositionOnlyHandler()36 FieldPositionOnlyHandler::~FieldPositionOnlyHandler() {
37 }
38 
39 void
addAttribute(int32_t id,int32_t start,int32_t limit)40 FieldPositionOnlyHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
41   if (pos.getField() == id) {
42     pos.setBeginIndex(start + fShift);
43     pos.setEndIndex(limit + fShift);
44   }
45 }
46 
47 void
shiftLast(int32_t delta)48 FieldPositionOnlyHandler::shiftLast(int32_t delta) {
49   if (delta != 0 && pos.getField() != FieldPosition::DONT_CARE && pos.getBeginIndex() != -1) {
50     pos.setBeginIndex(delta + pos.getBeginIndex());
51     pos.setEndIndex(delta + pos.getEndIndex());
52   }
53 }
54 
55 UBool
isRecording(void) const56 FieldPositionOnlyHandler::isRecording(void) const {
57   return pos.getField() != FieldPosition::DONT_CARE;
58 }
59 
60 
61 // utility subclass FieldPositionIteratorHandler
62 
FieldPositionIteratorHandler(FieldPositionIterator * posIter,UErrorCode & _status)63 FieldPositionIteratorHandler::FieldPositionIteratorHandler(FieldPositionIterator* posIter,
64                                                            UErrorCode& _status)
65     : iter(posIter), vec(NULL), status(_status) {
66   if (iter && U_SUCCESS(status)) {
67     vec = new UVector32(status);
68   }
69 }
70 
~FieldPositionIteratorHandler()71 FieldPositionIteratorHandler::~FieldPositionIteratorHandler() {
72   // setData adopts the vec regardless of status, so it's safe to null it
73   if (iter) {
74     iter->setData(vec, status);
75   }
76   // if iter is null, we never allocated vec, so no need to free it
77   vec = NULL;
78 }
79 
80 void
addAttribute(int32_t id,int32_t start,int32_t limit)81 FieldPositionIteratorHandler::addAttribute(int32_t id, int32_t start, int32_t limit) {
82   if (iter && U_SUCCESS(status) && start < limit) {
83     int32_t size = vec->size();
84     vec->addElement(id, status);
85     vec->addElement(start + fShift, status);
86     vec->addElement(limit + fShift, status);
87     if (!U_SUCCESS(status)) {
88       vec->setSize(size);
89     }
90   }
91 }
92 
93 void
shiftLast(int32_t delta)94 FieldPositionIteratorHandler::shiftLast(int32_t delta) {
95   if (U_SUCCESS(status) && delta != 0) {
96     int32_t i = vec->size();
97     if (i > 0) {
98       --i;
99       vec->setElementAt(delta + vec->elementAti(i), i);
100       --i;
101       vec->setElementAt(delta + vec->elementAti(i), i);
102     }
103   }
104 }
105 
106 UBool
isRecording(void) const107 FieldPositionIteratorHandler::isRecording(void) const {
108   return U_SUCCESS(status);
109 }
110 
111 U_NAMESPACE_END
112 
113 #endif /* !UCONFIG_NO_FORMATTING */
114