]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/numfmt/util.cpp
1 /********************************************************************
3 * Copyright (c) 1999-2003, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/unistr.h"
8 #include "unicode/fmtable.h"
16 U_LEFT_SQUARE_BRACKET
=0x5b,
18 U_RIGHT_SQUARE_BRACKET
=0x5d,
22 // Verify that a UErrorCode is successful; exit(1) if not
23 void check(UErrorCode
& status
, const char* msg
) {
24 if (U_FAILURE(status
)) {
25 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
28 // printf("Ok: %s\n", msg);
31 // Append a hex string to the target
32 static UnicodeString
& appendHex(uint32_t number
,
34 UnicodeString
& target
) {
37 digit
= (number
>> ((--digits
) * 4)) & 0xF;
38 target
+= (UChar
)(digit
< 10 ? 0x30 + digit
: 0x41 - 10 + digit
);
43 // Replace nonprintable characters with unicode escapes
44 UnicodeString
escape(const UnicodeString
&source
) {
47 target
+= (UChar
)U_DQUOTE
;
48 for (i
=0; i
<source
.length(); ++i
) {
50 if (ch
< 0x09 || (ch
> 0x0D && ch
< 0x20) || ch
> 0x7E) {
51 (target
+= (UChar
)U_BACKSLASH
) += (UChar
)U_SMALL_U
;
52 appendHex(ch
, 4, target
);
57 target
+= (UChar
)U_DQUOTE
;
61 // Print the given string to stdout using the UTF-8 converter
62 void uprintf(const UnicodeString
&str
) {
63 char stackBuffer
[100];
66 int32_t bufLen
= str
.extract(0, 0x7fffffff, stackBuffer
, sizeof(stackBuffer
), "UTF-8");
67 if(bufLen
< sizeof(stackBuffer
)) {
70 buf
= new char[bufLen
+ 1];
71 bufLen
= str
.extract(0, 0x7fffffff, buf
, bufLen
+ 1, "UTF-8");
74 if(buf
!= stackBuffer
) {
79 // Create a display string for a formattable
80 UnicodeString
formattableToString(const Formattable
& f
) {
81 switch (f
.getType()) {
82 case Formattable::kDate
:
83 // TODO: Finish implementing this
84 return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
85 case Formattable::kDouble
:
88 sprintf(buf
, "%gD", f
.getDouble());
89 return UnicodeString(buf
, "");
91 case Formattable::kLong
:
92 case Formattable::kInt64
:
95 sprintf(buf
, "%ldL", f
.getLong());
96 return UnicodeString(buf
, "");
98 case Formattable::kString
:
99 return UnicodeString((UChar
)U_DQUOTE
).append(f
.getString()).append((UChar
)U_DQUOTE
);
100 case Formattable::kArray
:
103 const Formattable
* array
= f
.getArray(count
);
104 UnicodeString
result((UChar
)U_LEFT_SQUARE_BRACKET
);
105 for (i
=0; i
<count
; ++i
) {
107 (result
+= (UChar
)U_COMMA
) += (UChar
)U_SPACE
;
109 result
+= formattableToString(array
[i
]);
111 result
+= (UChar
)U_RIGHT_SQUARE_BRACKET
;
115 return UNICODE_STRING_SIMPLE("INVALID_Formattable");