]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/numfmt/util.cpp
1 /********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 *************************************************************************
5 *************************************************************************
7 * Copyright (c) 1999-2009, International Business Machines Corporation and
8 * others. All Rights Reserved.
9 *************************************************************************/
11 #define __STDC_FORMAT_MACROS 1
14 #include "unicode/unistr.h"
15 #include "unicode/fmtable.h"
25 U_LEFT_SQUARE_BRACKET
=0x5b,
27 U_RIGHT_SQUARE_BRACKET
=0x5d,
31 // Verify that a UErrorCode is successful; exit(1) if not
32 void check(UErrorCode
& status
, const char* msg
) {
33 if (U_FAILURE(status
)) {
34 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
37 // printf("Ok: %s\n", msg);
40 // Append a hex string to the target
41 static UnicodeString
& appendHex(uint32_t number
,
43 UnicodeString
& target
) {
46 digit
= (number
>> ((--digits
) * 4)) & 0xF;
47 target
+= (UChar
)(digit
< 10 ? 0x30 + digit
: 0x41 - 10 + digit
);
52 // Replace nonprintable characters with unicode escapes
53 UnicodeString
escape(const UnicodeString
&source
) {
56 target
+= (UChar
)U_DQUOTE
;
57 for (i
=0; i
<source
.length(); ++i
) {
59 if (ch
< 0x09 || (ch
> 0x0D && ch
< 0x20) || ch
> 0x7E) {
60 (target
+= (UChar
)U_BACKSLASH
) += (UChar
)U_SMALL_U
;
61 appendHex(ch
, 4, target
);
66 target
+= (UChar
)U_DQUOTE
;
70 // Print the given string to stdout using the UTF-8 converter
71 void uprintf(const UnicodeString
&str
) {
72 char stackBuffer
[100];
75 int32_t bufLen
= str
.extract(0, 0x7fffffff, stackBuffer
, sizeof(stackBuffer
), "UTF-8");
76 if(bufLen
< sizeof(stackBuffer
)) {
79 buf
= new char[bufLen
+ 1];
80 bufLen
= str
.extract(0, 0x7fffffff, buf
, bufLen
+ 1, "UTF-8");
83 if(buf
!= stackBuffer
) {
88 // Create a display string for a formattable
89 UnicodeString
formattableToString(const Formattable
& f
) {
90 switch (f
.getType()) {
91 case Formattable::kDate
:
92 // TODO: Finish implementing this
93 return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
94 case Formattable::kDouble
:
97 sprintf(buf
, "%gD", f
.getDouble());
98 return UnicodeString(buf
, "");
100 case Formattable::kLong
:
103 sprintf(buf
, "%" PRId32
"L", f
.getLong());
104 return UnicodeString(buf
, "");
106 case Formattable::kInt64
:
109 sprintf(buf
, "%" PRId64
"L", f
.getInt64());
110 return UnicodeString(buf
, "");
112 case Formattable::kString
:
113 return UnicodeString((UChar
)U_DQUOTE
).append(f
.getString()).append((UChar
)U_DQUOTE
);
114 case Formattable::kArray
:
117 const Formattable
* array
= f
.getArray(count
);
118 UnicodeString
result((UChar
)U_LEFT_SQUARE_BRACKET
);
119 for (i
=0; i
<count
; ++i
) {
121 (result
+= (UChar
)U_COMMA
) += (UChar
)U_SPACE
;
123 result
+= formattableToString(array
[i
]);
125 result
+= (UChar
)U_RIGHT_SQUARE_BRACKET
;
129 return UNICODE_STRING_SIMPLE("INVALID_Formattable");