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