1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 * Copyright (C) 2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 * file name: digitaffix.cpp
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "digitaffix.h"
17 #include "unistrappender.h"
21 DigitAffix::DigitAffix() : fAffix(), fAnnotations() {
24 DigitAffix::DigitAffix(
25 const UChar *value, int32_t charCount, int32_t fieldId)
26 : fAffix(value, charCount),
27 fAnnotations(charCount, (UChar) fieldId, charCount) {
31 DigitAffix::remove() {
33 fAnnotations.remove();
37 DigitAffix::appendUChar(UChar value, int32_t fieldId) {
39 fAnnotations.append((UChar) fieldId);
43 DigitAffix::append(const UnicodeString &value, int32_t fieldId) {
46 UnicodeStringAppender appender(fAnnotations);
47 int32_t len = value.length();
48 for (int32_t i = 0; i < len; ++i) {
49 appender.append((UChar) fieldId);
55 DigitAffix::setTo(const UnicodeString &value, int32_t fieldId) {
57 fAnnotations.remove();
59 UnicodeStringAppender appender(fAnnotations);
60 int32_t len = value.length();
61 for (int32_t i = 0; i < len; ++i) {
62 appender.append((UChar) fieldId);
68 DigitAffix::append(const UChar *value, int32_t charCount, int32_t fieldId) {
69 fAffix.append(value, charCount);
71 UnicodeStringAppender appender(fAnnotations);
72 for (int32_t i = 0; i < charCount; ++i) {
73 appender.append((UChar) fieldId);
79 DigitAffix::format(FieldPositionHandler &handler, UnicodeString &appendTo) const {
80 int32_t len = fAffix.length();
84 if (!handler.isRecording()) {
85 return appendTo.append(fAffix);
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);
94 if (lastId != UNUM_FIELD_COUNT) {
95 handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + i);
101 if (lastId != UNUM_FIELD_COUNT) {
102 handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + len);
104 return appendTo.append(fAffix);
109 #endif /* #if !UCONFIG_NO_FORMATTING */