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 : * 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 :
19 : U_NAMESPACE_BEGIN
20 :
21 0 : DigitAffix::DigitAffix() : fAffix(), fAnnotations() {
22 0 : }
23 :
24 0 : DigitAffix::DigitAffix(
25 0 : const UChar *value, int32_t charCount, int32_t fieldId)
26 : : fAffix(value, charCount),
27 0 : fAnnotations(charCount, (UChar) fieldId, charCount) {
28 0 : }
29 :
30 : void
31 0 : DigitAffix::remove() {
32 0 : fAffix.remove();
33 0 : fAnnotations.remove();
34 0 : }
35 :
36 : void
37 0 : DigitAffix::appendUChar(UChar value, int32_t fieldId) {
38 0 : fAffix.append(value);
39 0 : fAnnotations.append((UChar) fieldId);
40 0 : }
41 :
42 : void
43 0 : DigitAffix::append(const UnicodeString &value, int32_t fieldId) {
44 0 : fAffix.append(value);
45 : {
46 0 : UnicodeStringAppender appender(fAnnotations);
47 0 : int32_t len = value.length();
48 0 : for (int32_t i = 0; i < len; ++i) {
49 0 : appender.append((UChar) fieldId);
50 : }
51 : }
52 0 : }
53 :
54 : void
55 0 : DigitAffix::setTo(const UnicodeString &value, int32_t fieldId) {
56 0 : fAffix = value;
57 0 : fAnnotations.remove();
58 : {
59 0 : UnicodeStringAppender appender(fAnnotations);
60 0 : int32_t len = value.length();
61 0 : for (int32_t i = 0; i < len; ++i) {
62 0 : appender.append((UChar) fieldId);
63 : }
64 : }
65 0 : }
66 :
67 : void
68 0 : DigitAffix::append(const UChar *value, int32_t charCount, int32_t fieldId) {
69 0 : fAffix.append(value, charCount);
70 : {
71 0 : UnicodeStringAppender appender(fAnnotations);
72 0 : for (int32_t i = 0; i < charCount; ++i) {
73 0 : appender.append((UChar) fieldId);
74 : }
75 : }
76 0 : }
77 :
78 : UnicodeString &
79 0 : DigitAffix::format(FieldPositionHandler &handler, UnicodeString &appendTo) const {
80 0 : int32_t len = fAffix.length();
81 0 : if (len == 0) {
82 0 : return appendTo;
83 : }
84 0 : if (!handler.isRecording()) {
85 0 : return appendTo.append(fAffix);
86 : }
87 0 : U_ASSERT(fAffix.length() == fAnnotations.length());
88 0 : int32_t appendToStart = appendTo.length();
89 0 : int32_t lastId = (int32_t) fAnnotations.charAt(0);
90 0 : int32_t lastIdStart = 0;
91 0 : for (int32_t i = 1; i < len; ++i) {
92 0 : int32_t id = (int32_t) fAnnotations.charAt(i);
93 0 : if (id != lastId) {
94 0 : if (lastId != UNUM_FIELD_COUNT) {
95 0 : handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + i);
96 : }
97 0 : lastId = id;
98 0 : lastIdStart = i;
99 : }
100 : }
101 0 : if (lastId != UNUM_FIELD_COUNT) {
102 0 : handler.addAttribute(lastId, appendToStart + lastIdStart, appendToStart + len);
103 : }
104 0 : return appendTo.append(fAffix);
105 : }
106 :
107 : U_NAMESPACE_END
108 :
109 : #endif /* #if !UCONFIG_NO_FORMATTING */
|