void print_stderr_OChar_color(BashColors fg, BashColors bg, BashAttr attr, const OChar *text) { U_STRING_INIT(colorstr, "%c[%d;%d;%dm%s", 14); u_fprintf_u(u_stderr, colorstr, 0x1B, attr, fg + 30, bg + 40, text); reset(); }
U_CFUNC UResourceBundle *u_wmsg_setPath(const char *path, UErrorCode *err) { if(U_FAILURE(*err)) { return 0; } if(gBundle != NULL) { *err = U_ILLEGAL_ARGUMENT_ERROR; return 0; } else { UResourceBundle *b = NULL; b = ures_open(path, NULL, err); if(U_FAILURE(*err)) { return 0; } gBundle = b; U_STRING_INIT(gNoFormatting, " (UCONFIG_NO_FORMATTING see uconfig.h)", 38); } return gBundle; }
static void demoUnicodeStringInit() { // *** Make sure to read about invariant characters in utypes.h! *** // Initialization of Unicode strings from C literals works _only_ for // invariant characters! printf("\n* demoUnicodeStringInit() ---------- ***\n\n"); // the string literal is 32 chars long - this must be counted for the macro UnicodeString invariantOnly=UNICODE_STRING("such characters are safe 123 %-.", 32); /* * In C, we need two macros: one to declare the UChar[] array, and * one to populate it; the second one is a noop on platforms where * wchar_t is compatible with UChar and ASCII-based. * The length of the string literal must be counted for both macros. */ /* declare the invString array for the string */ U_STRING_DECL(invString, "such characters are safe 123 %-.", 32); /* populate it with the characters */ U_STRING_INIT(invString, "such characters are safe 123 %-.", 32); // compare the C and C++ strings printf("C and C++ Unicode strings are equal: %d\n", invariantOnly==UnicodeString(TRUE, invString, 32)); /* * convert between char * and UChar * strings that * contain only invariant characters */ static const char *cs1="such characters are safe 123 %-."; static UChar us1[40]; static char cs2[40]; u_charsToUChars(cs1, us1, 33); /* include the terminating NUL */ u_UCharsToChars(us1, cs2, 33); printf("char * -> UChar * -> char * with only " "invariant characters: \"%s\"\n", cs2); // initialize a UnicodeString from a string literal that contains // escape sequences written with invariant characters // do not forget to duplicate the backslashes for ICU to see them // then, count each double backslash only once! UnicodeString german=UNICODE_STRING( "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", 64). unescape(); printUnicodeString("german UnicodeString from unescaping:\n ", german); /* * C: convert and unescape a char * string with only invariant * characters to fill a UChar * string */ UChar buffer[200]; int32_t length; length=u_unescape( "Sch\\u00f6nes Auto: \\u20ac 11240.\\fPrivates Zeichen: \\U00102345\\n", buffer, UPRV_LENGTHOF(buffer)); printf("german C Unicode string from char * unescaping: (length %d)\n ", length); printUnicodeString("", UnicodeString(buffer)); }
static void TestMessageWithUnusedArgNumber() { UErrorCode errorCode = U_ZERO_ERROR; U_STRING_DECL(pattern, "abc {1} def", 11); UChar x[2] = { 0x78, 0 }; // "x" UChar y[2] = { 0x79, 0 }; // "y" U_STRING_DECL(expected, "abc y def", 9); UChar result[20]; int32_t length; U_STRING_INIT(pattern, "abc {1} def", 11); U_STRING_INIT(expected, "abc y def", 9); length = u_formatMessage("en", pattern, -1, result, LENGTHOF(result), &errorCode, x, y); if (U_FAILURE(errorCode) || length != u_strlen(expected) || u_strcmp(result, expected) != 0) { log_err("u_formatMessage(pattern with only {1}, 2 args) failed: result length %d, UErrorCode %s \n", (int)length, u_errorName(errorCode)); } }
static void TestBadPattern(void) { UErrorCode status = U_ZERO_ERROR; USet *pat; U_STRING_DECL(pattern, "[", 1); U_STRING_INIT(pattern, "[", 1); pat = uset_openPatternOptions(pattern, u_strlen(pattern), 0, &status); if (pat != NULL || U_SUCCESS(status)) { log_err("uset_openPatternOptions did not fail as expected %s\n", u_errorName(status)); } }
void StringTest::Test_U_STRING() { U_STRING_INIT(ustringVar, "aZ0 -", 5); if( sizeof(ustringVar)/sizeof(*ustringVar)!=6 || ustringVar[0]!=0x61 || ustringVar[1]!=0x5a || ustringVar[2]!=0x30 || ustringVar[3]!=0x20 || ustringVar[4]!=0x2d || ustringVar[5]!=0 ) { errln("Test_U_STRING: U_STRING_DECL with U_STRING_INIT does not work right! " "See putil.h and utypes.h with platform.h."); } }
static void TestSerialized() { uint16_t buffer[1000]; USerializedSet sset; USet *set; UErrorCode errorCode; UChar32 c; int32_t length; /* use a pattern that generates both BMP and supplementary code points */ U_STRING_DECL(pattern, "[:Cf:]", 6); U_STRING_INIT(pattern, "[:Cf:]", 6); errorCode=U_ZERO_ERROR; set=uset_openPattern(pattern, -1, &errorCode); if(U_FAILURE(errorCode)) { log_data_err("uset_openPattern([:Cf:]) failed - %s (Are you missing data?)\n", u_errorName(errorCode)); return; } length=uset_serialize(set, buffer, UPRV_LENGTHOF(buffer), &errorCode); if(U_FAILURE(errorCode)) { log_err("unable to uset_serialize([:Cf:]) - %s\n", u_errorName(errorCode)); uset_close(set); return; } uset_getSerializedSet(&sset, buffer, length); for(c=0; c<=0x10ffff; ++c) { if(uset_contains(set, c)!=uset_serializedContains(&sset, c)) { log_err("uset_contains(U+%04x)!=uset_serializedContains(U+%04x)\n", c); break; } } uset_close(set); }
void SSearchTest::monkeyTest(char *params) { // ook! UErrorCode status = U_ZERO_ERROR; //UCollator *coll = ucol_open(NULL, &status); UCollator *coll = ucol_openFromShortString("S1", FALSE, NULL, &status); if (U_FAILURE(status)) { errcheckln(status, "Failed to create collator in MonkeyTest! - %s", u_errorName(status)); return; } CollData *monkeyData = new CollData(coll, status); USet *expansions = uset_openEmpty(); USet *contractions = uset_openEmpty(); ucol_getContractionsAndExpansions(coll, contractions, expansions, FALSE, &status); U_STRING_DECL(letter_pattern, "[[:letter:]-[:ideographic:]-[:hangul:]]", 39); U_STRING_INIT(letter_pattern, "[[:letter:]-[:ideographic:]-[:hangul:]]", 39); USet *letters = uset_openPattern(letter_pattern, 39, &status); SetMonkey letterMonkey(letters); StringSetMonkey contractionMonkey(contractions, coll, monkeyData); StringSetMonkey expansionMonkey(expansions, coll, monkeyData); UnicodeString testCase; UnicodeString alternate; UnicodeString pattern, altPattern; UnicodeString prefix, altPrefix; UnicodeString suffix, altSuffix; Monkey *monkeys[] = { &letterMonkey, &contractionMonkey, &expansionMonkey, &contractionMonkey, &expansionMonkey, &contractionMonkey, &expansionMonkey, &contractionMonkey, &expansionMonkey}; int32_t monkeyCount = sizeof(monkeys) / sizeof(monkeys[0]); // int32_t nonMatchCount = 0; UCollationStrength strengths[] = {UCOL_PRIMARY, UCOL_SECONDARY, UCOL_TERTIARY}; const char *strengthNames[] = {"primary", "secondary", "tertiary"}; int32_t strengthCount = sizeof(strengths) / sizeof(strengths[0]); int32_t loopCount = quick? 1000 : 10000; int32_t firstStrength = 0; int32_t lastStrength = strengthCount - 1; //*/ 0; if (params != NULL) { #if !UCONFIG_NO_REGULAR_EXPRESSIONS UnicodeString p(params); loopCount = getIntParam("loop", p, loopCount); m_seed = getIntParam("seed", p, m_seed); RegexMatcher m(" *strength *= *(primary|secondary|tertiary) *", p, 0, status); if (m.find()) { UnicodeString breakType = m.group(1, status); for (int32_t s = 0; s < strengthCount; s += 1) { if (breakType == strengthNames[s]) { firstStrength = lastStrength = s; break; } } m.reset(); p = m.replaceFirst("", status); } if (RegexMatcher("\\S", p, 0, status).find()) { // Each option is stripped out of the option string as it is processed. // All options have been checked. The option string should have been completely emptied.. char buf[100]; p.extract(buf, sizeof(buf), NULL, status); buf[sizeof(buf)-1] = 0; errln("Unrecognized or extra parameter: %s\n", buf); return; } #else infoln("SSearchTest built with UCONFIG_NO_REGULAR_EXPRESSIONS: ignoring parameters."); #endif } for(int32_t s = firstStrength; s <= lastStrength; s += 1) { int32_t notFoundCount = 0; logln("Setting strength to %s.", strengthNames[s]); ucol_setStrength(coll, strengths[s]); // TODO: try alternate prefix and suffix too? // TODO: alterntaes are only equal at primary strength. Is this OK? for(int32_t t = 0; t < loopCount; t += 1) { uint32_t seed = m_seed; // int32_t nmc = 0; generateTestCase(coll, monkeys, monkeyCount, pattern, altPattern); generateTestCase(coll, monkeys, monkeyCount, prefix, altPrefix); generateTestCase(coll, monkeys, monkeyCount, suffix, altSuffix); // pattern notFoundCount += monkeyTestCase(coll, pattern, pattern, altPattern, "pattern", strengthNames[s], seed); testCase.remove(); testCase.append(prefix); testCase.append(/*alt*/pattern); // prefix + pattern notFoundCount += monkeyTestCase(coll, testCase, pattern, altPattern, "prefix + pattern", strengthNames[s], seed); testCase.append(suffix); // prefix + pattern + suffix notFoundCount += monkeyTestCase(coll, testCase, pattern, altPattern, "prefix + pattern + suffix", strengthNames[s], seed); testCase.remove(); testCase.append(pattern); testCase.append(suffix); // pattern + suffix notFoundCount += monkeyTestCase(coll, testCase, pattern, altPattern, "pattern + suffix", strengthNames[s], seed); } logln("For strength %s the not found count is %d.", strengthNames[s], notFoundCount); } uset_close(contractions); uset_close(expansions); uset_close(letters); delete monkeyData; ucol_close(coll); }
CollData::CollData(UCollator *collator, UErrorCode &status) : coll(NULL), ceToCharsStartingWith(NULL) { // [:c:] == [[:cn:][:cc:][:co:][:cf:][:cs:]] // i.e. other, control, private use, format, surrogate U_STRING_DECL(test_pattern, "[[:assigned:]-[:c:]]", 20); U_STRING_INIT(test_pattern, "[[:assigned:]-[:c:]]", 20); USet *charsToTest = uset_openPattern(test_pattern, 20, &status); // Han ext. A, Han, Jamo, Hangul, Han Ext. B // i.e. all the characers we handle implicitly U_STRING_DECL(remove_pattern, "[[\\u3400-\\u9FFF][\\u1100-\\u11F9][\\uAC00-\\uD7AF][\\U00020000-\\U0002A6DF]]", 70); U_STRING_INIT(remove_pattern, "[[\\u3400-\\u9FFF][\\u1100-\\u11F9][\\uAC00-\\uD7AF][\\U00020000-\\U0002A6DF]]", 70); USet *charsToRemove = uset_openPattern(remove_pattern, 70, &status); if (U_FAILURE(status)) { return; } USet *expansions = uset_openEmpty(); USet *contractions = uset_openEmpty(); int32_t itemCount; ceToCharsStartingWith = new CEToStringsMap(status); if (U_FAILURE(status)) { goto bail; } #ifdef CLONE_COLLATOR coll = ucol_safeClone(collator, NULL, NULL, &status); if (U_FAILURE(status)) { goto bail; } #else coll = collator; #endif ucol_getContractionsAndExpansions(coll, contractions, expansions, FALSE, &status); uset_addAll(charsToTest, contractions); uset_addAll(charsToTest, expansions); uset_removeAll(charsToTest, charsToRemove); itemCount = uset_getItemCount(charsToTest); for(int32_t item = 0; item < itemCount; item += 1) { UChar32 start = 0, end = 0; UChar buffer[16]; int32_t len = uset_getItem(charsToTest, item, &start, &end, buffer, 16, &status); if (len == 0) { for (UChar32 ch = start; ch <= end; ch += 1) { UnicodeString *st = new UnicodeString(ch); if (st == NULL) { status = U_MEMORY_ALLOCATION_ERROR; break; } CEList *ceList = new CEList(coll, *st, status); ceToCharsStartingWith->put(ceList->get(0), st, status); delete ceList; delete st; } } else if (len > 0) { UnicodeString *st = new UnicodeString(buffer, len); if (st == NULL) { status = U_MEMORY_ALLOCATION_ERROR; break; } CEList *ceList = new CEList(coll, *st, status); ceToCharsStartingWith->put(ceList->get(0), st, status); delete ceList; delete st; } else { // shouldn't happen... } if (U_FAILURE(status)) { break; } } bail: uset_close(contractions); uset_close(expansions); uset_close(charsToRemove); uset_close(charsToTest); if (U_FAILURE(status)) { return; } UnicodeSet hanRanges(UNICODE_STRING_SIMPLE("[:Unified_Ideograph:]"), status); if (U_FAILURE(status)) { return; } UnicodeSetIterator hanIter(hanRanges); UnicodeString hanString; while(hanIter.nextRange()) { hanString.append(hanIter.getCodepoint()); hanString.append(hanIter.getCodepointEnd()); } // TODO: Why U+11FF? The old code had an outdated UCOL_LAST_T_JAMO=0x11F9, // but as of Unicode 6.3 the 11xx block is filled, // and there are also more Jamo T at U+D7CB..U+D7FB. // Maybe use [:HST=T:] and look for the end of the last range? // Maybe use script boundary mappings instead of this code?? UChar jamoRanges[] = {Hangul::JAMO_L_BASE, Hangul::JAMO_V_BASE, Hangul::JAMO_T_BASE + 1, 0x11FF}; UnicodeString jamoString(FALSE, jamoRanges, UPRV_LENGTHOF(jamoRanges)); CEList hanList(coll, hanString, status); CEList jamoList(coll, jamoString, status); int32_t j = 0; if (U_FAILURE(status)) { return; } for (int32_t c = 0; c < jamoList.size(); c += 1) { uint32_t jce = jamoList[c]; if (! isContinuation(jce)) { jamoLimits[j++] = jce; } } jamoLimits[3] += (1 << UCOL_PRIMARYORDERSHIFT); minHan = 0xFFFFFFFF; maxHan = 0; for(int32_t h = 0; h < hanList.size(); h += 2) { uint32_t han = (uint32_t) hanList[h]; if (han < minHan) { minHan = han; } if (han > maxHan) { maxHan = han; } } maxHan += (1 << UCOL_PRIMARYORDERSHIFT); }
void CharIterTest::TestConstructionAndEqualityUChariter() { U_STRING_DECL(testText, "Now is the time for all good men to come to the aid of their country.", 69); U_STRING_DECL(testText2, "Don't bother using this string.", 31); U_STRING_INIT(testText, "Now is the time for all good men to come to the aid of their country.", 69); U_STRING_INIT(testText2, "Don't bother using this string.", 31); UnicodeString result, result4, result5; UCharCharacterIterator* test1 = new UCharCharacterIterator(testText, u_strlen(testText)); UCharCharacterIterator* test2 = new UCharCharacterIterator(testText, u_strlen(testText), 5); UCharCharacterIterator* test3 = new UCharCharacterIterator(testText, u_strlen(testText), 2, 20, 5); UCharCharacterIterator* test4 = new UCharCharacterIterator(testText2, u_strlen(testText2)); UCharCharacterIterator* test5 = (UCharCharacterIterator*)test1->clone(); UCharCharacterIterator* test6 = new UCharCharacterIterator(*test1); // j785: length=-1 will use u_strlen() UCharCharacterIterator* test7a = new UCharCharacterIterator(testText, -1); UCharCharacterIterator* test7b = new UCharCharacterIterator(testText, -1); UCharCharacterIterator* test7c = new UCharCharacterIterator(testText, -1, 2, 20, 5); // Bad parameters. UCharCharacterIterator* test8a = new UCharCharacterIterator(testText, -1, -1, 20, 5); UCharCharacterIterator* test8b = new UCharCharacterIterator(testText, -1, 2, 100, 5); UCharCharacterIterator* test8c = new UCharCharacterIterator(testText, -1, 2, 20, 100); if (test8a->startIndex() < 0) errln("Construction failed: startIndex is negative"); if (test8b->endIndex() != u_strlen(testText)) errln("Construction failed: endIndex is different from the text length"); if (test8c->getIndex() < test8c->startIndex() || test8c->endIndex() < test8c->getIndex()) errln("Construction failed: index is invalid"); if (*test1 == *test2 || *test1 == *test3 || *test1 == *test4 ) errln("Construction or operator== failed: Unequal objects compared equal"); if (*test1 != *test5 ) errln("clone() or equals() failed: Two clones tested unequal"); if (*test6 != *test1 ) errln("copy construction or equals() failed: Two copies tested unequal"); if (test1->hashCode() == test2->hashCode() || test1->hashCode() == test3->hashCode() || test1->hashCode() == test4->hashCode()) errln("hashCode() failed: different objects have same hash code"); if (test1->hashCode() != test5->hashCode()) errln("hashCode() failed: identical objects have different hash codes"); test7a->getText(result); test7b->getText(result4); test7c->getText(result5); if(result != UnicodeString(testText) || result4 != result || result5 != result) errln("error in construction"); test1->getText(result); test4->getText(result4); test5->getText(result5); if(result != result5 || result == result4) errln("getText() failed"); test5->setText(testText2, u_strlen(testText2)); test5->getText(result5); if(result == result5 || result4 != result5) errln("setText() or getText() failed"); test5->setText(testText, u_strlen(testText)); test5->getText(result5); if(result != result5 || result == result4) errln("setText() or getText() round-trip failed"); test1->setIndex(5); if (*test1 != *test2 || *test1 == *test5) errln("setIndex() failed"); test8b->setIndex32(5); if (test8b->getIndex()!=5) errln("setIndex32() failed"); *test1 = *test3; if (*test1 != *test3 || *test1 == *test5) errln("operator= failed"); delete test1; delete test2; delete test3; delete test4; delete test5; delete test6; delete test7a; delete test7b; delete test7c; delete test8a; delete test8b; delete test8c; }
static USet *openIDSet() { UErrorCode errorCode = U_ZERO_ERROR; U_STRING_DECL(pattern, "[:ID_Continue:]", 15); U_STRING_INIT(pattern, "[:ID_Continue:]", 15); return uset_openPattern(pattern, 15, &errorCode); }
static void TestRegisterUnregister() { UErrorCode status=U_ZERO_ERROR; UTransliterator* t1=NULL; UTransliterator* rules=NULL, *rules2; UTransliterator* inverse1=NULL; UChar rule[]= { 0x0061, 0x003c, 0x003e, 0x0063}; /*a<>b*/ U_STRING_DECL(ID, "TestA-TestB", 11); U_STRING_INIT(ID, "TestA-TestB", 11); /* Make sure it doesn't exist */ t1=utrans_open("TestA-TestB", UTRANS_FORWARD,NULL,0,NULL, &status); if(t1 != NULL || U_SUCCESS(status)) { log_err("FAIL: TestA-TestB already registered\n"); return; } status=U_ZERO_ERROR; /* Check inverse too */ inverse1=utrans_open("TestA-TestB", UTRANS_REVERSE, NULL,0,NULL,&status); if(inverse1 != NULL || U_SUCCESS(status)) { log_err("FAIL: TestA-TestB already registered\n"); return; } status=U_ZERO_ERROR; /* Create it */ rules=utrans_open("TestA-TestB",UTRANS_FORWARD, rule, 4, NULL, &status); if(U_FAILURE(status)) { log_err("FAIL: utrans_openRules(a<>B) failed with error=%s\n", myErrorName(status)); return; } /* clone it so we can register it a second time */ rules2=utrans_clone(rules, &status); if(U_FAILURE(status)) { log_err("FAIL: utrans_clone(a<>B) failed with error=%s\n", myErrorName(status)); return; } status=U_ZERO_ERROR; /* Register it */ utrans_register(rules, &status); if(U_FAILURE(status)) { log_err("FAIL: utrans_register failed with error=%s\n", myErrorName(status)); return; } status=U_ZERO_ERROR; /* Now check again -- should exist now*/ t1= utrans_open("TestA-TestB", UTRANS_FORWARD, NULL,0,NULL,&status); if(U_FAILURE(status) || t1 == NULL) { log_err("FAIL: TestA-TestB not registered\n"); return; } utrans_close(t1); /*unregister the instance*/ status=U_ZERO_ERROR; utrans_unregister("TestA-TestB"); /* now Make sure it doesn't exist */ t1=utrans_open("TestA-TestB", UTRANS_FORWARD,NULL,0,NULL, &status); if(U_SUCCESS(status) || t1 != NULL) { log_err("FAIL: TestA-TestB isn't unregistered\n"); return; } utrans_close(t1); /* now with utrans_unregisterID(const UChar *) */ status=U_ZERO_ERROR; utrans_register(rules2, &status); if(U_FAILURE(status)) { log_err("FAIL: 2nd utrans_register failed with error=%s\n", myErrorName(status)); return; } status=U_ZERO_ERROR; /* Now check again -- should exist now*/ t1= utrans_open("TestA-TestB", UTRANS_FORWARD, NULL,0,NULL,&status); if(U_FAILURE(status) || t1 == NULL) { log_err("FAIL: 2nd TestA-TestB not registered\n"); return; } utrans_close(t1); /*unregister the instance*/ status=U_ZERO_ERROR; utrans_unregisterID(ID, -1); /* now Make sure it doesn't exist */ t1=utrans_openU(ID, -1, UTRANS_FORWARD,NULL,0,NULL, &status); if(U_SUCCESS(status) || t1 != NULL) { log_err("FAIL: 2nd TestA-TestB isn't unregistered\n"); return; } utrans_close(t1); utrans_close(inverse1); }