Why do I get error C3851 (a universal-character-name cannot designate a character in the basic character set)?
Asked Answered
A

1

0

I've been trying to translate an example whitespace remover from here on SO to run in a C++-cli project. The following looks fine as far as intellisense is concerned:

static String^ TrimAllWithInplaceCharArray(String^ str)
{
    unsigned int len = str->Length;
    array<wchar_t, 1>^ src = str->ToCharArray();
    int dstIdx = 0;
    for (unsigned int i = 0; i < len; i++) {
        wchar_t ch = src[i];
        switch (ch) {
            case L'\u0020': case L'\u00A0': case L'\u1680': case L'\u2000': case L'\u2001':
            case L'\u2002': case L'\u2003': case L'\u2004': case L'\u2005': case L'\u2006':
            case L'\u2007': case L'\u2008': case L'\u2009': case L'\u200A': case L'\u202F':
            case L'\u205F': case L'\u3000': case L'\u2028': case L'\u2029': case L'\u0009':
            case L'\u000A': case L'\u000B': case L'\u000C': case L'\u000D': case L'\u0085':
                continue;
            default:
                src[dstIdx++] = ch;
                break;
        }
    }
    return gcnew String(src, 0, dstIdx);
}

...but doesn't compile:

error C3851 : '\u0020' : a universal - character - name cannot designate a character in the basic character set
error C3850 : '\u0009' : a universal - character - name specifies an invalid character
error C3850 : '\u000A' : a universal - character - name specifies an invalid character
error C2196 : case value '63' already used
error C3850 : '\u000B' : a universal - character - name specifies an invalid character
error C2196 : case value '63' already used
error C3850 : '\u000C' : a universal - character - name specifies an invalid character
error C2196 : case value '63' already used
error C3850 : '\u000D' : a universal - character - name specifies an invalid character
error C2196 : case value '63' already used
error C3850 : '\u0085' : a universal - character - name specifies an invalid character
error C2196 : case value '63' already used

Am I miss-representing a data type or the literal values in some way?

Apollinaire answered 9/2, 2017 at 11:47 Comment(2)
Hard to guess, it compiles clean. It is the kind of error you'd get when the compiler sees L\u0020 instead of L'\u0020'. Or some other kind of Unicode glyph that looks like an apostrophe. So it no longer recognizes it as a literal. Typical copy/pasta accident btw. Use a hex viewer to look at the source file.Osburn
How about trying to remove the "00". Use '\u20' instead of '\u0020' for example. Just a quick guess.Pneumodynamics
P
0

See if the following works...

static String^ TrimAllWithInplaceCharArray(String^ str)
{
    unsigned int len = str->Length;
    array<wchar_t, 1>^ src = str->ToCharArray();
    int dstIdx = 0;
    for (unsigned int i = 0; i < len; i++) {
        wchar_t ch = src[i];
        switch (ch) {
        case L'\x20': case L'\xA0': case L'\u1680': case L'\u2000': case L'\u2001':
        case L'\u2002': case L'\u2003': case L'\u2004': case L'\u2005': case L'\u2006':
        case L'\u2007': case L'\u2008': case L'\u2009': case L'\u200A': case L'\u202F':
        case L'\u205F': case L'\u3000': case L'\u2028': case L'\u2029': case L'\x09':
        case L'\x0A': case L'\x0B': case L'\x0C': case L'\x0D': case L'\x85':
            continue;
        default:
            src[dstIdx++] = ch;
            break;
        }
    }
    return gcnew String(src, 0, dstIdx);
}

Found this here...

"You cannot use - u0022 because you cannot use a universal character name that designates a character in the basic character set."

Pneumodynamics answered 10/2, 2017 at 3:9 Comment(1)
Some more useful background on string literals here: #10220901Apollinaire

© 2022 - 2024 — McMap. All rights reserved.