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