]> git.saurik.com Git - apple/icu.git/blame - icuSources/samples/numfmt/util.cpp
ICU-64243.0.1.tar.gz
[apple/icu.git] / icuSources / samples / numfmt / util.cpp
CommitLineData
b75a7d8f 1/********************************************************************
f3c0d7a5
A
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 *************************************************************************
5 *************************************************************************
b75a7d8f 6 * COPYRIGHT:
f3c0d7a5 7 * Copyright (c) 1999-2009, International Business Machines Corporation and
b75a7d8f 8 * others. All Rights Reserved.
f3c0d7a5 9 *************************************************************************/
b75a7d8f 10
3d1f044b
A
11#define __STDC_FORMAT_MACROS 1
12#include <inttypes.h>
13
b75a7d8f
A
14#include "unicode/unistr.h"
15#include "unicode/fmtable.h"
16#include <stdio.h>
17#include <stdlib.h>
18
0f5d89e8
A
19using namespace icu;
20
b75a7d8f
A
21enum {
22 U_SPACE=0x20,
23 U_DQUOTE=0x22,
24 U_COMMA=0x2c,
25 U_LEFT_SQUARE_BRACKET=0x5b,
26 U_BACKSLASH=0x5c,
27 U_RIGHT_SQUARE_BRACKET=0x5d,
28 U_SMALL_U=0x75
29};
30
31// Verify that a UErrorCode is successful; exit(1) if not
32void check(UErrorCode& status, const char* msg) {
33 if (U_FAILURE(status)) {
34 printf("ERROR: %s (%s)\n", u_errorName(status), msg);
35 exit(1);
36 }
37 // printf("Ok: %s\n", msg);
38}
39
40// Append a hex string to the target
41static UnicodeString& appendHex(uint32_t number,
42 int8_t digits,
43 UnicodeString& target) {
44 uint32_t digit;
45 while (digits > 0) {
46 digit = (number >> ((--digits) * 4)) & 0xF;
47 target += (UChar)(digit < 10 ? 0x30 + digit : 0x41 - 10 + digit);
48 }
49 return target;
50}
51
52// Replace nonprintable characters with unicode escapes
53UnicodeString escape(const UnicodeString &source) {
54 int32_t i;
55 UnicodeString target;
56 target += (UChar)U_DQUOTE;
57 for (i=0; i<source.length(); ++i) {
58 UChar ch = source[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);
62 } else {
63 target += ch;
64 }
65 }
66 target += (UChar)U_DQUOTE;
67 return target;
68}
69
70// Print the given string to stdout using the UTF-8 converter
71void uprintf(const UnicodeString &str) {
72 char stackBuffer[100];
73 char *buf = 0;
74
75 int32_t bufLen = str.extract(0, 0x7fffffff, stackBuffer, sizeof(stackBuffer), "UTF-8");
76 if(bufLen < sizeof(stackBuffer)) {
77 buf = stackBuffer;
78 } else {
79 buf = new char[bufLen + 1];
80 bufLen = str.extract(0, 0x7fffffff, buf, bufLen + 1, "UTF-8");
81 }
82 printf("%s", buf);
83 if(buf != stackBuffer) {
729e4ab9 84 delete[] buf;
b75a7d8f
A
85 }
86}
87
88// Create a display string for a formattable
89UnicodeString 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:
95 {
96 char buf[256];
97 sprintf(buf, "%gD", f.getDouble());
98 return UnicodeString(buf, "");
99 }
100 case Formattable::kLong:
3d1f044b
A
101 {
102 char buf[256];
103 sprintf(buf, "%" PRId32 "L", f.getLong());
104 return UnicodeString(buf, "");
105 }
374ca955 106 case Formattable::kInt64:
b75a7d8f
A
107 {
108 char buf[256];
3d1f044b 109 sprintf(buf, "%" PRId64 "L", f.getInt64());
b75a7d8f
A
110 return UnicodeString(buf, "");
111 }
112 case Formattable::kString:
113 return UnicodeString((UChar)U_DQUOTE).append(f.getString()).append((UChar)U_DQUOTE);
114 case Formattable::kArray:
115 {
116 int32_t i, count;
117 const Formattable* array = f.getArray(count);
118 UnicodeString result((UChar)U_LEFT_SQUARE_BRACKET);
119 for (i=0; i<count; ++i) {
120 if (i > 0) {
121 (result += (UChar)U_COMMA) += (UChar)U_SPACE;
122 }
123 result += formattableToString(array[i]);
124 }
125 result += (UChar)U_RIGHT_SQUARE_BRACKET;
126 return result;
127 }
128 default:
129 return UNICODE_STRING_SIMPLE("INVALID_Formattable");
130 }
131}