]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/datefmt/util.cpp
1 /********************************************************************
3 * Copyright (c) 1999-2002,2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/unistr.h"
11 // Verify that a UErrorCode is successful; exit(1) if not
12 void check(UErrorCode
& status
, const char* msg
) {
13 if (U_FAILURE(status
)) {
14 printf("ERROR: %s (%s)\n", u_errorName(status
), msg
);
17 // printf("Ok: %s\n", msg);
20 // Append a hex string to the target
21 static UnicodeString
& appendHex(uint32_t number
,
23 UnicodeString
& target
) {
24 static const UnicodeString
DIGIT_STRING("0123456789ABCDEF");
26 target
+= DIGIT_STRING
[(number
>> ((--digits
) * 4)) & 0xF];
31 // Replace nonprintable characters with unicode escapes
32 UnicodeString
escape(const UnicodeString
&source
) {
36 for (i
=0; i
<source
.length(); ++i
) {
38 if (ch
< 0x09 || (ch
> 0x0A && ch
< 0x20) || ch
> 0x7E) {
40 appendHex(ch
, 4, target
);
49 // Print the given string to stdout
50 void uprintf(const UnicodeString
&str
) {
52 int32_t len
= str
.length();
53 // int32_t bufLen = str.extract(0, len, buf); // Preflight
54 /* Preflighting seems to be broken now, so assume 1-1 conversion,
56 int32_t bufLen
= len
+ 16;
58 buf
= new char[bufLen
+ 1];
59 actualLen
= str
.extract(0, len
, buf
/*, bufLen*/); // Default codepage conversion