Home | History | Annotate | Download | only in numfmt
      1 /********************************************************************
      2  *    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 using namespace icu;
     17 
     18 enum {
     19     U_SPACE=0x20,
     20     U_DQUOTE=0x22,
     21     U_COMMA=0x2c,
     22     U_LEFT_SQUARE_BRACKET=0x5b,
     23     U_BACKSLASH=0x5c,
     24     U_RIGHT_SQUARE_BRACKET=0x5d,
     25     U_SMALL_U=0x75
     26 };
     27 
     28 // Verify that a UErrorCode is successful; exit(1) if not
     29 void check(UErrorCode& status, const char* msg) {
     30     if (U_FAILURE(status)) {
     31         printf("ERROR: %s (%s)\n", u_errorName(status), msg);
     32         exit(1);
     33     }
     34     // printf("Ok: %s\n", msg);
     35 }
     36 
     37 // Append a hex string to the target
     38 static UnicodeString& appendHex(uint32_t number,
     39                          int8_t digits,
     40                          UnicodeString& target) {
     41     uint32_t digit;
     42     while (digits > 0) {
     43         digit = (number >> ((--digits) * 4)) & 0xF;
     44         target += (UChar)(digit < 10 ? 0x30 + digit : 0x41 - 10 + digit);
     45     }
     46     return target;
     47 }
     48 
     49 // Replace nonprintable characters with unicode escapes
     50 UnicodeString escape(const UnicodeString &source) {
     51     int32_t i;
     52     UnicodeString target;
     53     target += (UChar)U_DQUOTE;
     54     for (i=0; i<source.length(); ++i) {
     55         UChar ch = source[i];
     56         if (ch < 0x09 || (ch > 0x0D && ch < 0x20) || ch > 0x7E) {
     57             (target += (UChar)U_BACKSLASH) += (UChar)U_SMALL_U;
     58             appendHex(ch, 4, target);
     59         } else {
     60             target += ch;
     61         }
     62     }
     63     target += (UChar)U_DQUOTE;
     64     return target;
     65 }
     66 
     67 // Print the given string to stdout using the UTF-8 converter
     68 void uprintf(const UnicodeString &str) {
     69     char stackBuffer[100];
     70     char *buf = 0;
     71 
     72     int32_t bufLen = str.extract(0, 0x7fffffff, stackBuffer, sizeof(stackBuffer), "UTF-8");
     73     if(bufLen < sizeof(stackBuffer)) {
     74         buf = stackBuffer;
     75     } else {
     76         buf = new char[bufLen + 1];
     77         bufLen = str.extract(0, 0x7fffffff, buf, bufLen + 1, "UTF-8");
     78     }
     79     printf("%s", buf);
     80     if(buf != stackBuffer) {
     81         delete[] buf;
     82     }
     83 }
     84 
     85 // Create a display string for a formattable
     86 UnicodeString formattableToString(const Formattable& f) {
     87     switch (f.getType()) {
     88     case Formattable::kDate:
     89         // TODO: Finish implementing this
     90         return UNICODE_STRING_SIMPLE("Formattable_DATE_TBD");
     91     case Formattable::kDouble:
     92         {
     93             char buf[256];
     94             sprintf(buf, "%gD", f.getDouble());
     95             return UnicodeString(buf, "");
     96         }
     97     case Formattable::kLong:
     98     case Formattable::kInt64:
     99         {
    100             char buf[256];
    101             sprintf(buf, "%ldL", f.getLong());
    102             return UnicodeString(buf, "");
    103         }
    104     case Formattable::kString:
    105         return UnicodeString((UChar)U_DQUOTE).append(f.getString()).append((UChar)U_DQUOTE);
    106     case Formattable::kArray:
    107         {
    108             int32_t i, count;
    109             const Formattable* array = f.getArray(count);
    110             UnicodeString result((UChar)U_LEFT_SQUARE_BRACKET);
    111             for (i=0; i<count; ++i) {
    112                 if (i > 0) {
    113                     (result += (UChar)U_COMMA) += (UChar)U_SPACE;
    114                 }
    115                 result += formattableToString(array[i]);
    116             }
    117             result += (UChar)U_RIGHT_SQUARE_BRACKET;
    118             return result;
    119         }
    120     default:
    121         return UNICODE_STRING_SIMPLE("INVALID_Formattable");
    122     }
    123 }
    124