]>
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 #include "unicode/unistr.h"
12 #include "unicode/fmtable.h"
20 U_LEFT_SQUARE_BRACKET
=0x5b,
22 U_RIGHT_SQUARE_BRACKET
=0x5d,
26 // Verify that a UErrorCode is successful; exit(1) if not
27 void check(UErrorCode
& status
, const char* msg
) {
28 if (U_FAILURE(status
)) {
29 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
32 // printf("Ok: %s\n", msg);
35 // Append a hex string to the target
36 static UnicodeString
& appendHex(uint32_t number
,
38 UnicodeString
& target
) {
41 digit
= (number
>> ((--digits
) * 4)) & 0xF;
42 target
+= (UChar
)(digit
< 10 ? 0x30 + digit
: 0x41 - 10 + digit
);
47 // Replace nonprintable characters with unicode escapes
48 UnicodeString
escape(const UnicodeString
&source
) {
51 target
+= (UChar
)U_DQUOTE
;
52 for (i
=0; i
<source
.length(); ++i
) {
54 if (ch
< 0x09 || (ch
> 0x0D && ch
< 0x20) || ch
> 0x7E) {
55 (target
+= (UChar
)U_BACKSLASH
) += (UChar
)U_SMALL_U
;
56 appendHex(ch
, 4, target
);
61 target
+= (UChar
)U_DQUOTE
;
65 // Print the given string to stdout using the UTF-8 converter
66 void uprintf(const UnicodeString
&str
) {
67 char stackBuffer
[100];
70 int32_t bufLen
= str
.extract(0, 0x7fffffff, stackBuffer
, sizeof(stackBuffer
), "UTF-8");
71 if(bufLen
< sizeof(stackBuffer
)) {
74 buf
= new char[bufLen
+ 1];
75 bufLen
= str
.extract(0, 0x7fffffff, buf
, bufLen
+ 1, "UTF-8");
78 if(buf
!= stackBuffer
) {
83 // Create a display string for a formattable
84 UnicodeString
formattableToString(const Formattable
& f
) {
85 switch (f
.getType()) {
86 case Formattable::kDate
:
87 // TODO: Finish implementing this
88 return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
89 case Formattable::kDouble
:
92 sprintf(buf
, "%gD", f
.getDouble());
93 return UnicodeString(buf
, "");
95 case Formattable::kLong
:
96 case Formattable::kInt64
:
99 sprintf(buf
, "%ldL", f
.getLong());
100 return UnicodeString(buf
, "");
102 case Formattable::kString
:
103 return UnicodeString((UChar
)U_DQUOTE
).append(f
.getString()).append((UChar
)U_DQUOTE
);
104 case Formattable::kArray
:
107 const Formattable
* array
= f
.getArray(count
);
108 UnicodeString
result((UChar
)U_LEFT_SQUARE_BRACKET
);
109 for (i
=0; i
<count
; ++i
) {
111 (result
+= (UChar
)U_COMMA
) += (UChar
)U_SPACE
;
113 result
+= formattableToString(array
[i
]);
115 result
+= (UChar
)U_RIGHT_SQUARE_BRACKET
;
119 return UNICODE_STRING_SIMPLE("INVALID_Formattable");