]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/datefmt/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-2003, International Business Machines Corporation and
8 * others. All Rights Reserved.
9 *************************************************************************/
11 #include "unicode/unistr.h"
17 // Verify that a UErrorCode is successful; exit(1) if not
18 void check(UErrorCode
& status
, const char* msg
) {
19 if (U_FAILURE(status
)) {
20 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
23 // printf("Ok: %s\n", msg);
26 // Append a hex string to the target
27 static UnicodeString
& appendHex(uint32_t number
,
29 UnicodeString
& target
) {
30 static const UnicodeString
DIGIT_STRING("0123456789ABCDEF");
32 target
+= DIGIT_STRING
[(number
>> ((--digits
) * 4)) & 0xF];
37 // Replace nonprintable characters with unicode escapes
38 UnicodeString
escape(const UnicodeString
&source
) {
42 for (i
=0; i
<source
.length(); ++i
) {
44 if (ch
< 0x09 || (ch
> 0x0A && ch
< 0x20) || ch
> 0x7E) {
46 appendHex(ch
, 4, target
);
55 // Print the given string to stdout
56 void uprintf(const UnicodeString
&str
) {
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,
62 int32_t bufLen
= len
+ 16;
64 buf
= new char[bufLen
+ 1];
65 actualLen
= str
.extract(0, len
, buf
/*, bufLen*/); // Default codepage conversion