Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / base / i18n / message_formatter.cc
blob702e51b94aaaf2eb30f3c9364daa6fbb8f84bc0c
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/i18n/message_formatter.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "base/time/time.h"
10 #include "third_party/icu/source/common/unicode/unistr.h"
11 #include "third_party/icu/source/common/unicode/utypes.h"
12 #include "third_party/icu/source/i18n/unicode/fmtable.h"
13 #include "third_party/icu/source/i18n/unicode/msgfmt.h"
15 using icu::UnicodeString;
17 namespace base {
18 namespace i18n {
19 namespace {
20 UnicodeString UnicodeStringFromStringPiece(StringPiece str) {
21 return UnicodeString::fromUTF8(
22 icu::StringPiece(str.data(), base::checked_cast<int32_t>(str.size())));
24 } // anonymous namespace
26 namespace internal {
27 MessageArg::MessageArg() : formattable(nullptr) {}
29 MessageArg::MessageArg(const char* s)
30 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
32 MessageArg::MessageArg(StringPiece s)
33 : formattable(new icu::Formattable(UnicodeStringFromStringPiece(s))) {}
35 MessageArg::MessageArg(const std::string& s)
36 : formattable(new icu::Formattable(UnicodeString::fromUTF8(s))) {}
38 MessageArg::MessageArg(const string16& s)
39 : formattable(new icu::Formattable(UnicodeString(s.data(), s.size()))) {}
41 MessageArg::MessageArg(int i) : formattable(new icu::Formattable(i)) {}
43 MessageArg::MessageArg(int64_t i) : formattable(new icu::Formattable(i)) {}
45 MessageArg::MessageArg(double d) : formattable(new icu::Formattable(d)) {}
47 MessageArg::MessageArg(const Time& t)
48 : formattable(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {}
50 MessageArg::~MessageArg() {}
52 // Tests if this argument has a value, and if so increments *count.
53 bool MessageArg::has_value(int *count) const {
54 if (formattable == nullptr)
55 return false;
57 ++*count;
58 return true;
61 } // namespace internal
63 string16 MessageFormatter::FormatWithNumberedArgs(
64 StringPiece16 msg,
65 const internal::MessageArg& arg0,
66 const internal::MessageArg& arg1,
67 const internal::MessageArg& arg2,
68 const internal::MessageArg& arg3,
69 const internal::MessageArg& arg4,
70 const internal::MessageArg& arg5,
71 const internal::MessageArg& arg6) {
72 int32_t args_count = 0;
73 icu::Formattable args[] = {
74 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
75 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
76 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
77 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
78 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
79 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
80 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
83 UnicodeString msg_string(msg.data(), msg.size());
84 UErrorCode error = U_ZERO_ERROR;
85 icu::MessageFormat format(msg_string, error);
86 icu::UnicodeString formatted;
87 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE);
88 format.format(args, args_count, formatted, ignore, error);
89 if (U_FAILURE(error)) {
90 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
91 << u_errorName(error);
92 return string16();
94 return string16(formatted.getBuffer(), formatted.length());
97 string16 MessageFormatter::FormatWithNamedArgs(
98 StringPiece16 msg,
99 StringPiece name0, const internal::MessageArg& arg0,
100 StringPiece name1, const internal::MessageArg& arg1,
101 StringPiece name2, const internal::MessageArg& arg2,
102 StringPiece name3, const internal::MessageArg& arg3,
103 StringPiece name4, const internal::MessageArg& arg4,
104 StringPiece name5, const internal::MessageArg& arg5,
105 StringPiece name6, const internal::MessageArg& arg6) {
106 icu::UnicodeString names[] = {
107 UnicodeStringFromStringPiece(name0),
108 UnicodeStringFromStringPiece(name1),
109 UnicodeStringFromStringPiece(name2),
110 UnicodeStringFromStringPiece(name3),
111 UnicodeStringFromStringPiece(name4),
112 UnicodeStringFromStringPiece(name5),
113 UnicodeStringFromStringPiece(name6),
115 int32_t args_count = 0;
116 icu::Formattable args[] = {
117 arg0.has_value(&args_count) ? *arg0.formattable : icu::Formattable(),
118 arg1.has_value(&args_count) ? *arg1.formattable : icu::Formattable(),
119 arg2.has_value(&args_count) ? *arg2.formattable : icu::Formattable(),
120 arg3.has_value(&args_count) ? *arg3.formattable : icu::Formattable(),
121 arg4.has_value(&args_count) ? *arg4.formattable : icu::Formattable(),
122 arg5.has_value(&args_count) ? *arg5.formattable : icu::Formattable(),
123 arg6.has_value(&args_count) ? *arg6.formattable : icu::Formattable(),
126 UnicodeString msg_string(msg.data(), msg.size());
127 UErrorCode error = U_ZERO_ERROR;
128 icu::MessageFormat format(msg_string, error);
130 icu::UnicodeString formatted;
131 format.format(names, args, args_count, formatted, error);
132 if (U_FAILURE(error)) {
133 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with "
134 << u_errorName(error);
135 return string16();
137 return string16(formatted.getBuffer(), formatted.length());
140 } // namespace i18n
141 } // namespace base