OLD | NEW |
---|---|
(Empty) | |
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. | |
4 // Copied from i18n/localization/formatter.cc with modifications | |
5 | |
6 #include "base/i18n/message_formatter.h" | |
7 | |
8 #include "base/logging.h" | |
9 #include "third_party/icu/source/common/unicode/unistr.h" | |
10 #include "third_party/icu/source/common/unicode/utypes.h" | |
11 | |
12 | |
Avi (use Gerrit)
2015/07/30 19:19:48
one blank line
jungshik at Google
2015/07/31 21:36:15
Done.
| |
13 using icu::UnicodeString; | |
14 | |
15 namespace base { | |
16 namespace i18n { | |
17 namespace { | |
18 UnicodeString UnicodeStringFromStringPiece(StringPiece str) { | |
19 return UnicodeString::fromUTF8( | |
20 icu::StringPiece(str.data(), static_cast<int32_t>(str.size()))); | |
21 } | |
Avi (use Gerrit)
2015/07/30 19:19:48
no indentation for stuff in a namespace
jungshik at Google
2015/07/31 21:36:15
Done.
| |
22 } // anonymous namespace | |
23 | |
24 namespace internal { | |
25 MessageArg::MessageArg(const char* s) | |
26 : f(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} | |
27 | |
28 MessageArg::MessageArg(StringPiece s) | |
29 : f(new icu::Formattable(UnicodeStringFromStringPiece(s))) {} | |
30 | |
31 MessageArg::MessageArg(const std::string& s) | |
32 : f(new icu::Formattable(UnicodeString::fromUTF8(s))) {} | |
33 | |
34 MessageArg::MessageArg(const string16& s) | |
35 : f(new icu::Formattable(UnicodeString(s.data(), s.size()))) {} | |
36 | |
37 MessageArg::MessageArg(int i) : f(new icu::Formattable(i)) {} | |
38 | |
39 MessageArg::MessageArg(double d) : f(new icu::Formattable(d)) {} | |
40 | |
41 MessageArg::MessageArg(const Time& t) : | |
42 f(new icu::Formattable(static_cast<UDate>(t.ToJsTime()))) {} | |
Ryan Sleevi
2015/07/30 23:21:51
Wrong indent/wrap
jungshik at Google
2015/07/31 21:36:15
Done.
| |
43 | |
44 MessageArg::~MessageArg() { delete f; } | |
Ryan Sleevi
2015/07/30 23:21:50
Why not use a scoped_ptr<> for this?
jungshik at Google
2015/07/31 21:36:15
Done. Clang-chrome-style checker complained about
| |
45 | |
46 // Tests if this argument has a value, and if so increments *count. | |
47 bool MessageArg::def(int *count) const { | |
48 if (f != NULL) { | |
Avi (use Gerrit)
2015/07/30 19:19:48
if (f)
?
At least nullptr
jungshik at Google
2015/07/31 21:36:15
Done and renamed |f| to |formattable|
| |
49 ++*count; | |
50 return true; | |
51 } else { | |
Ryan Sleevi
2015/07/30 23:21:50
Don't else after a return
https://www.chromium.or
jungshik at Google
2015/07/31 21:36:15
Done.
| |
52 return false; | |
53 } | |
54 } | |
55 | |
56 } // namespace internal | |
57 | |
58 string16 MessageFormatter::FormatWithNumberedArgs( | |
59 StringPiece16 msg, | |
60 const internal::MessageArg& arg0, | |
61 const internal::MessageArg& arg1, | |
62 const internal::MessageArg& arg2, | |
63 const internal::MessageArg& arg3, | |
64 const internal::MessageArg& arg4, | |
65 const internal::MessageArg& arg5, | |
66 const internal::MessageArg& arg6) { | |
67 int32_t args_count = 0; | |
68 icu::Formattable args[] = { | |
69 arg0.def(&args_count) ? *arg0.f : icu::Formattable(), | |
70 arg1.def(&args_count) ? *arg1.f : icu::Formattable(), | |
71 arg2.def(&args_count) ? *arg2.f : icu::Formattable(), | |
72 arg3.def(&args_count) ? *arg3.f : icu::Formattable(), | |
73 arg4.def(&args_count) ? *arg4.f : icu::Formattable(), | |
74 arg5.def(&args_count) ? *arg5.f : icu::Formattable(), | |
75 arg6.def(&args_count) ? *arg6.f : icu::Formattable(), | |
76 }; | |
77 | |
78 UnicodeString msgString(msg.data(), msg.size()); | |
Ryan Sleevi
2015/07/30 23:21:51
msg_string
jungshik at Google
2015/07/31 21:36:15
Done.
| |
79 UErrorCode error = U_ZERO_ERROR; | |
80 icu::MessageFormat fmt(msgString, error); | |
Ryan Sleevi
2015/07/30 23:21:51
format
jungshik at Google
2015/07/31 21:36:15
Done. Also in FormatWithNamedArgs
| |
81 icu::UnicodeString formatted; | |
82 icu::FieldPosition ignore(icu::FieldPosition::DONT_CARE); | |
83 fmt.format(args, args_count, formatted, ignore, error); | |
84 if (U_FAILURE(error)) { | |
85 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with " | |
86 << u_errorName(error); | |
87 return string16(); | |
88 } | |
89 return string16(formatted.getBuffer(), formatted.length()); | |
90 } | |
91 | |
92 string16 MessageFormatter::FormatWithNamedArgs( | |
93 StringPiece16 msg, | |
94 StringPiece name0, const internal::MessageArg& arg0, | |
95 StringPiece name1, const internal::MessageArg& arg1, | |
96 StringPiece name2, const internal::MessageArg& arg2, | |
97 StringPiece name3, const internal::MessageArg& arg3, | |
98 StringPiece name4, const internal::MessageArg& arg4, | |
99 StringPiece name5, const internal::MessageArg& arg5, | |
100 StringPiece name6, const internal::MessageArg& arg6) { | |
101 icu::UnicodeString names[] = { | |
102 UnicodeStringFromStringPiece(name0), | |
103 UnicodeStringFromStringPiece(name1), | |
104 UnicodeStringFromStringPiece(name2), | |
105 UnicodeStringFromStringPiece(name3), | |
106 UnicodeStringFromStringPiece(name4), | |
107 UnicodeStringFromStringPiece(name5), | |
108 UnicodeStringFromStringPiece(name6), | |
109 }; | |
110 int32_t args_count = 0; | |
111 icu::Formattable args[] = { | |
112 arg0.def(&args_count) ? *arg0.f : icu::Formattable(), | |
113 arg1.def(&args_count) ? *arg1.f : icu::Formattable(), | |
114 arg2.def(&args_count) ? *arg2.f : icu::Formattable(), | |
115 arg3.def(&args_count) ? *arg3.f : icu::Formattable(), | |
116 arg4.def(&args_count) ? *arg4.f : icu::Formattable(), | |
117 arg5.def(&args_count) ? *arg5.f : icu::Formattable(), | |
118 arg6.def(&args_count) ? *arg6.f : icu::Formattable(), | |
119 }; | |
120 | |
121 UnicodeString msgString(msg.data(), msg.size()); | |
Ryan Sleevi
2015/07/30 23:21:50
msg_string
jungshik at Google
2015/07/31 21:36:15
Done.
| |
122 UErrorCode error = U_ZERO_ERROR; | |
123 icu::MessageFormat fmt(msgString, error); | |
Ryan Sleevi
2015/07/30 23:21:51
format
jungshik at Google
2015/07/31 21:36:15
Done.
| |
124 | |
125 icu::UnicodeString formatted; | |
126 fmt.format(names, args, args_count, formatted, error); | |
127 if (U_FAILURE(error)) { | |
128 LOG(ERROR) << "MessageFormat(" << msg.as_string() << ") failed with " | |
129 << u_errorName(error); | |
130 return string16(); | |
131 } | |
132 return string16(formatted.getBuffer(), formatted.length()); | |
133 } | |
134 | |
135 } // namespace i18n | |
136 } // namespace base | |
OLD | NEW |