1// Copyright (C) 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html 3/* 4 * Copyright (C) 2015, International Business Machines 5 * Corporation and others. All Rights Reserved. 6 * 7 * file name: digitaffix.cpp 8 */ 9 10#include "unicode/utypes.h" 11 12#if !UCONFIG_NO_FORMATTING 13 14#include "digitaffix.h" 15#include "fphdlimp.h" 16#include "uassert.h" 17#include "unistrappender.h" 18 19U_NAMESPACE_BEGIN 20 21DigitAffix::DigitAffix() : fAffix(), fAnnotations() { 22} 23 24DigitAffix::DigitAffix( 25 const UChar *value, int32_t charCount, int32_t fieldId) 26 : fAffix(value, charCount), 27 fAnnotations(charCount, (UChar) fieldId, charCount) { 28} 29 30void 31DigitAffix::remove() { 32 fAffix.remove(); 33 fAnnotations.remove(); 34} 35 36void 37DigitAffix::appendUChar(UChar value, int32_t fieldId) { 38 fAffix.append(value); 39 fAnnotations.append((UChar) fieldId); 40} 41 42void 43DigitAffix::append(const UnicodeString &value, int32_t fieldId) { 44 fAffix.append(value); 45 { 46 UnicodeStringAppender appender(fAnnotations); 47 int32_t len = value.length(); 48 for (int32_t i = 0; i < len; ++i) { 49 appender.append((UChar) fieldId); 50 } 51 } 52} 53 54void 55DigitAffix::setTo(const UnicodeString &value, int32_t fieldId) { 56 fAffix = value; 57 fAnnotations.remove(); 58 { 59 UnicodeStringAppender appender(fAnnotations); 60 int32_t len = value.length(); 61 for (int32_t i = 0; i < len; ++i) { 62 appender.append((UChar) fieldId); 63 } 64 } 65} 66 67void 68DigitAffix::append(const UChar *value, int32_t charCount, int32_t fieldId) { 69 fAffix.append(value, charCount); 70 { 71 UnicodeStringAppender appender(fAnnotations); 72 for (int32_t i = 0; i < charCount; ++i) { 73 appender.append((UChar) fieldId); 74 } 75 } 76} 77 78UnicodeString & 79DigitAffix::format(FieldPositionHandler &handler, UnicodeString &appendTo) const { 80 int32_t len = fAffix.length(); 81 if (len == 0) { 82 return appendTo; 83 } 84 if (!handler.isRecording()) { 85 return appendTo.append(fAffix); 86 } 87 U_ASSERT(fAffix.length() == fAnnotations.length()); 88 int32_t appendToStart = appendTo.length(); 89 int32_t lastId = (int32_t) fAnnotations.charAt(0); 90 int32_t lastIdStart = 0; 91 for (int32_t i = 1; i < len; ++i) { 92 int32_t id = (int32_t) fAnnotations.charAt(i); 93 if (id != lastId) { 94 if (lastId != UNUM_FIELD_COUNT) { 95 handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + i); 96 } 97 lastId = id; 98 lastIdStart = i; 99 } 100 } 101 if (lastId != UNUM_FIELD_COUNT) { 102 handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + len); 103 } 104 return appendTo.append(fAffix); 105} 106 107U_NAMESPACE_END 108 109#endif /* #if !UCONFIG_NO_FORMATTING */ 110