• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  *   © 2016 and later: Unicode, Inc. and others.
3  *   License & terms of use: http://www.unicode.org/copyright.html
4  *************************************************************************
5  *************************************************************************
6  * COPYRIGHT:
7  * Copyright (c) 1999-2009, International Business Machines Corporation and
8  * others. All Rights Reserved.
9  *************************************************************************/
10 
11 #define __STDC_FORMAT_MACROS 1
12 #include <inttypes.h>
13 
14 #include "unicode/unistr.h"
15 #include "unicode/fmtable.h"
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 using namespace icu;
20 
21 enum {
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
check(UErrorCode & status,const char * msg)32 void 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
appendHex(uint32_t number,int8_t digits,UnicodeString & target)41 static 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
escape(const UnicodeString & source)53 UnicodeString 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
uprintf(const UnicodeString & str)71 void 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) {
84         delete[] buf;
85     }
86 }
87 
88 // Create a display string for a formattable
formattableToString(const Formattable & f)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:
95         {
96             char buf[256];
97             sprintf(buf, "%gD", f.getDouble());
98             return UnicodeString(buf, "");
99         }
100     case Formattable::kLong:
101         {
102             char buf[256];
103             sprintf(buf, "%" PRId32 "L", f.getLong());
104             return UnicodeString(buf, "");
105         }
106     case Formattable::kInt64:
107         {
108             char buf[256];
109             sprintf(buf, "%" PRId64 "L", f.getInt64());
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 }
132