]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/msgfmt/util.cpp
1 /**********************************************************************
2 * © 2016 and later: Unicode, Inc. and others.
3 * License & terms of use: http://www.unicode.org/copyright.html#License
4 ***********************************************************************
5 ***********************************************************************
7 * Copyright (c) 1999-2010, International Business Machines Corporation and
8 * others. All Rights Reserved.
9 ***********************************************************************/
11 #include "unicode/unistr.h"
15 // Verify that a UErrorCode is successful; exit(1) if not
16 void check(UErrorCode
& status
, const char* msg
) {
17 if (U_FAILURE(status
)) {
18 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
21 // printf("Ok: %s\n", msg);
24 // Append a hex string to the target
25 static UnicodeString
& appendHex(uint32_t number
,
27 UnicodeString
& target
) {
28 static const UnicodeString
DIGIT_STRING("0123456789ABCDEF");
30 target
+= DIGIT_STRING
[(number
>> ((--digits
) * 4)) & 0xF];
35 // Replace nonprintable characters with unicode escapes
36 UnicodeString
escape(const UnicodeString
&source
) {
40 for (i
=0; i
<source
.length(); ++i
) {
42 if (ch
< 0x09 || (ch
> 0x0A && ch
< 0x20) || ch
> 0x7E) {
44 appendHex(ch
, 4, target
);
53 // Print the given string to stdout
54 void uprintf(const UnicodeString
&str
) {
56 int32_t len
= str
.length();
57 // int32_t bufLen = str.extract(0, len, buf); // Preflight
58 /* Preflighting seems to be broken now, so assume 1-1 conversion,
60 int32_t bufLen
= len
+ 16;
62 buf
= new char[bufLen
+ 1];
63 actualLen
= str
.extract(0, len
, buf
/*, bufLen*/); // Default codepage conversion