1// Copyright (C) 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4**********************************************************************
5*   Copyright (C) 2001-2009, International Business Machines
6*   Corporation and others.  All Rights Reserved.
7**********************************************************************
8*   Date        Name        Description
9*   05/23/00    aliu        Creation.
10**********************************************************************
11*/
12
13#include "unicode/unistr.h"
14#include "testutil.h"
15
16static const UChar HEX[16]={48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70};
17
18UnicodeString &TestUtility::appendHex(UnicodeString &buf, UChar32 ch) {
19    if (ch >= 0x10000) {
20        if (ch >= 0x100000) {
21            buf.append(HEX[0xF&(ch>>20)]);
22        }
23        buf.append(HEX[0xF&(ch>>16)]);
24    }
25    buf.append(HEX[0xF&(ch>>12)]);
26    buf.append(HEX[0xF&(ch>>8)]);
27    buf.append(HEX[0xF&(ch>>4)]);
28    buf.append(HEX[0xF&ch]);
29    return buf;
30}
31
32UnicodeString TestUtility::hex(UChar32 ch) {
33    UnicodeString buf;
34    appendHex(buf, ch);
35    return buf;
36}
37
38UnicodeString TestUtility::hex(const UnicodeString& s) {
39    return hex(s, 44 /*,*/);
40}
41
42UnicodeString TestUtility::hex(const UnicodeString& s, UChar sep) {
43    UnicodeString result;
44    if (s.isEmpty()) return result;
45    UChar32 c;
46    for (int32_t i = 0; i < s.length(); i += U16_LENGTH(c)) {
47        c = s.char32At(i);
48        if (i > 0) {
49            result.append(sep);
50        }
51        appendHex(result, c);
52    }
53    return result;
54}
55
56UnicodeString TestUtility::hex(const uint8_t* bytes, int32_t len) {
57	UnicodeString buf;
58	for (int32_t i = 0; i < len; ++i) {
59		buf.append(HEX[0x0F & (bytes[i] >> 4)]);
60		buf.append(HEX[0x0F & bytes[i]]);
61	}
62	return buf;
63}
64