Logo Search packages:      
Sourcecode: icu version File versions  Download package

UnicodeString UnicodeString::unescape (  )  const

Unescape a string of characters and return a string containing the result. The following escape sequences are recognized:

\uhhhh 4 hex digits; h in [0-9A-Fa-f] \Uhhhhhhhh 8 hex digits \xhh 1-2 hex digits \ooo 1-3 octal digits; o in [0-7] \cX control-X; X is masked with 0x1F

as well as the standard ANSI C escapes:

\a => U+0007, \b => U+0008, \t => U+0009, \n => U+000A, \v => U+000B, \f => U+000C, \r => U+000D, \e => U+001B, \" => U+0022, \' => U+0027, \? => U+003F, \\ => U+005C

Anything else following a backslash is generically escaped. For example, "[a\\-z]" returns "[a-z]".

If an escape sequence is ill-formed, this method returns an empty string. An example of an ill-formed sequence is "\\u" followed by fewer than 4 hex digits.

This function is similar to u_unescape() but not identical to it. The latter takes a source char*, so it does escape recognition and also invariant conversion.

a string with backslash escapes interpreted, or an empty string on error.
See also:


u_unescapeAt() ICU 2.0

Definition at line 538 of file unistr.cpp.

References append(), getBuffer(), length(), remove(), and unescapeAt().

Referenced by TextFile::readLine(), UnicodeSetTest::TestCloseOver(), NumberFormatTest::TestCurrency(), CollationAPITest::TestGetTailoredSet(), TransliteratorTest::TestGurmukhiDevanagari(), DateFormatTest::TestLocaleDateFormat(), TestMessageFormat::testMsgFormatSelect(), TransliteratorTest::TestThai(), UnicodeStringTest::TestUnescape(), and BasicNormalizerTest::TestZeroIndex().

    UnicodeString result(length(), (UChar32)0, (int32_t)0); // construct with capacity
    const UChar *array = getBuffer();
    int32_t len = length();
    int32_t prev = 0;
    for (int32_t i=0;;) {
        if (i == len) {
            result.append(array, prev, len - prev);
        if (array[i++] == 0x5C /*'\\'*/) {
            result.append(array, prev, (i - 1) - prev);
            UChar32 c = unescapeAt(i); // advances i
            if (c < 0) {
                result.remove(); // return empty string
                break; // invalid escape sequence
            prev = i;
    return result;

Generated by  Doxygen 1.6.0   Back to index