1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2007-2012, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/unistr.h"
15 #include "unicode/ustring.h"
25 static UnicodeString **strs = NULL;
27 static const UnicodeString& _fieldString(UDebugEnumType type, int32_t field, UnicodeString& fillin) {
28 const char *str = udbg_enumName(type, field);
30 return fillin.remove();
32 return fillin = UnicodeString(str, ""); // optimize?
37 static void udbg_cleanup(void) {
39 for(int t=0;t<=UDBG_ENUM_COUNT;t++) {
47 static UBool tu_cleanup(void)
53 static void udbg_register_cleanup(void) {
54 ucln_registerCleanup(UCLN_TOOLUTIL, tu_cleanup);
58 static void udbg_setup(void) {
60 udbg_register_cleanup();
61 //fprintf(stderr,"Initializing string cache..\n");
63 UnicodeString **newStrs = new UnicodeString*[UDBG_ENUM_COUNT+1];
64 for(int t=0;t<UDBG_ENUM_COUNT;t++) {
65 int32_t c = udbg_enumCount((UDebugEnumType)t);
66 newStrs[t] = new UnicodeString[c+1];
67 for(int f=0;f<=c;f++) {
68 _fieldString((UDebugEnumType)t, f, newStrs[t][f]);
71 newStrs[UDBG_ENUM_COUNT] = new UnicodeString[1]; // empty string
79 U_TOOLUTIL_API const UnicodeString& U_EXPORT2 udbg_enumString(UDebugEnumType type, int32_t field) {
83 if(type<0||type>=UDBG_ENUM_COUNT) {
84 // use UDBG_ENUM_COUNT,0 to mean an empty string
85 //fprintf(stderr, "** returning out of range on %d\n",type);
87 return strs[UDBG_ENUM_COUNT][0];
89 int32_t count = udbg_enumCount(type);
90 //fprintf(stderr, "enumString [%d,%d]: typecount %d, fieldcount %d\n", type,field,UDBG_ENUM_COUNT,count);
92 if(field<0 || field > count) {
93 return strs[type][count];
94 } else { return strs[type][field];
98 U_CAPI int32_t U_EXPORT2 udbg_enumByString(UDebugEnumType type, const UnicodeString& string) {
99 if(type<0||type>=UDBG_ENUM_COUNT) {
103 udbg_enumString(type,0);
105 /// printf("type=%d\n", type); fflush(stdout);
106 for(int i=0;i<udbg_enumCount(type);i++) {
107 // printf("i=%d/%d\n", i, udbg_enumCount(type)); fflush(stdout);
108 if(string == (strs[type][i])) {
115 // from DataMap::utoi
117 udbg_stoi(const UnicodeString &s)
120 const UChar *u = s.getBuffer();
121 int32_t len = s.length();
122 u_UCharsToChars(u, ch, len);
123 ch[len] = 0; /* include terminating \0 */
129 udbg_stod(const UnicodeString &s)
132 const UChar *u = s.getBuffer();
133 int32_t len = s.length();
134 u_UCharsToChars(u, ch, len);
135 ch[len] = 0; /* include terminating \0 */
139 U_CAPI UnicodeString *
140 udbg_escape(const UnicodeString &src, UnicodeString *dst)
143 for (int32_t i = 0; i < src.length(); ++i) {
145 if(ICU_Utility::isUnprintable(c)) {
146 *dst += UnicodeString("[");
147 ICU_Utility::escapeUnprintable(*dst, c);
148 *dst += UnicodeString("]");