• 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#License
4  ***********************************************************************
5  ***********************************************************************
6  * COPYRIGHT:
7  * Copyright (c) 1999-2010, International Business Machines Corporation and
8  * others. All Rights Reserved.
9  ***********************************************************************/
10 
11 #include "unicode/unistr.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 using namespace icu;
16 
17 // Verify that a UErrorCode is successful; exit(1) if not
check(UErrorCode & status,const char * msg)18 void check(UErrorCode& status, const char* msg) {
19     if (U_FAILURE(status)) {
20         printf("ERROR: %s (%s)\n", u_errorName(status), msg);
21         exit(1);
22     }
23     // printf("Ok: %s\n", msg);
24 }
25 
26 // Append a hex string to the target
appendHex(uint32_t number,int8_t digits,UnicodeString & target)27 static UnicodeString& appendHex(uint32_t number,
28                          int8_t digits,
29                          UnicodeString& target) {
30     static const UnicodeString DIGIT_STRING("0123456789ABCDEF");
31     while (digits > 0) {
32         target += DIGIT_STRING[(number >> ((--digits) * 4)) & 0xF];
33     }
34     return target;
35 }
36 
37 // Replace nonprintable characters with unicode escapes
escape(const UnicodeString & source)38 UnicodeString escape(const UnicodeString &source) {
39     int32_t i;
40     UnicodeString target;
41     target += "\"";
42     for (i=0; i<source.length(); ++i) {
43         UChar ch = source[i];
44         if (ch < 0x09 || (ch > 0x0A && ch < 0x20) || ch > 0x7E) {
45             target += "\\u";
46             appendHex(ch, 4, target);
47         } else {
48             target += ch;
49         }
50     }
51     target += "\"";
52     return target;
53 }
54 
55 // Print the given string to stdout
uprintf(const UnicodeString & str)56 void uprintf(const UnicodeString &str) {
57     char *buf = 0;
58     int32_t len = str.length();
59     // int32_t bufLen = str.extract(0, len, buf); // Preflight
60     /* Preflighting seems to be broken now, so assume 1-1 conversion,
61        plus some slop. */
62     int32_t bufLen = len + 16;
63 	int32_t actualLen;
64     buf = new char[bufLen + 1];
65     actualLen = str.extract(0, len, buf/*, bufLen*/); // Default codepage conversion
66     buf[actualLen] = 0;
67     printf("%s", buf);
68     delete [] buf;
69 }
70