• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  *   Copyright (C) 2016 and later: Unicode, Inc. and others.
3  *   License & terms of use: http://www.unicode.org/copyright.html#License
4  *************************************************************************
5  *************************************************************************
6  * COPYRIGHT:
7  * Copyright (c) 1999-2009, International Business Machines Corporation and
8  * others. All Rights Reserved.
9  *************************************************************************/
10 
11 #include "unicode/unistr.h"
12 #include "unicode/fmtable.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 
16 enum {
17     U_SPACE=0x20,
18     U_DQUOTE=0x22,
19     U_COMMA=0x2c,
20     U_LEFT_SQUARE_BRACKET=0x5b,
21     U_BACKSLASH=0x5c,
22     U_RIGHT_SQUARE_BRACKET=0x5d,
23     U_SMALL_U=0x75
24 };
25 
26 // Verify that a UErrorCode is successful; exit(1) if not
check(UErrorCode & status,const char * msg)27 void check(UErrorCode& status, const char* msg) {
28     if (U_FAILURE(status)) {
29         printf("ERROR: %s (%s)\n", u_errorName(status), msg);
30         exit(1);
31     }
32     // printf("Ok: %s\n", msg);
33 }
34 
35 // Append a hex string to the target
appendHex(uint32_t number,int8_t digits,UnicodeString & target)36 static UnicodeString& appendHex(uint32_t number,
37                          int8_t digits,
38                          UnicodeString& target) {
39     uint32_t digit;
40     while (digits > 0) {
41         digit = (number >> ((--digits) * 4)) & 0xF;
42         target += (UChar)(digit < 10 ? 0x30 + digit : 0x41 - 10 + digit);
43     }
44     return target;
45 }
46 
47 // Replace nonprintable characters with unicode escapes
escape(const UnicodeString & source)48 UnicodeString escape(const UnicodeString &source) {
49     int32_t i;
50     UnicodeString target;
51     target += (UChar)U_DQUOTE;
52     for (i=0; i<source.length(); ++i) {
53         UChar ch = source[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);
57         } else {
58             target += ch;
59         }
60     }
61     target += (UChar)U_DQUOTE;
62     return target;
63 }
64 
65 // Print the given string to stdout using the UTF-8 converter
uprintf(const UnicodeString & str)66 void uprintf(const UnicodeString &str) {
67     char stackBuffer[100];
68     char *buf = 0;
69 
70     int32_t bufLen = str.extract(0, 0x7fffffff, stackBuffer, sizeof(stackBuffer), "UTF-8");
71     if(bufLen < sizeof(stackBuffer)) {
72         buf = stackBuffer;
73     } else {
74         buf = new char[bufLen + 1];
75         bufLen = str.extract(0, 0x7fffffff, buf, bufLen + 1, "UTF-8");
76     }
77     printf("%s", buf);
78     if(buf != stackBuffer) {
79         delete[] buf;
80     }
81 }
82 
83 // Create a display string for a formattable
formattableToString(const Formattable & f)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:
90         {
91             char buf[256];
92             sprintf(buf, "%gD", f.getDouble());
93             return UnicodeString(buf, "");
94         }
95     case Formattable::kLong:
96     case Formattable::kInt64:
97         {
98             char buf[256];
99             sprintf(buf, "%ldL", f.getLong());
100             return UnicodeString(buf, "");
101         }
102     case Formattable::kString:
103         return UnicodeString((UChar)U_DQUOTE).append(f.getString()).append((UChar)U_DQUOTE);
104     case Formattable::kArray:
105         {
106             int32_t i, count;
107             const Formattable* array = f.getArray(count);
108             UnicodeString result((UChar)U_LEFT_SQUARE_BRACKET);
109             for (i=0; i<count; ++i) {
110                 if (i > 0) {
111                     (result += (UChar)U_COMMA) += (UChar)U_SPACE;
112                 }
113                 result += formattableToString(array[i]);
114             }
115             result += (UChar)U_RIGHT_SQUARE_BRACKET;
116             return result;
117         }
118     default:
119         return UNICODE_STRING_SIMPLE("INVALID_Formattable");
120     }
121 }
122