Logo Search packages:      
Sourcecode: qt4-x11 version File versions

QString QString::fromUtf8 ( const char *  str,
int  size = -1 
) [static]

Returns a QString initialized with the first size bytes of the UTF-8 string str.

If size is -1 (the default), it is taken to be qstrlen(str).

See also:
toUtf8(), fromAscii(), fromLatin1(), fromLocal8Bit()

Definition at line 3395 of file qstring.cpp.

References d, QChar::highSurrogate(), QChar::lowSurrogate(), QString(), resize(), and truncate().

Referenced by QDBusServer::address(), QAbstractFormBuilder::computeProperties(), QAbstractFormBuilder::create(), QLocale::dateFormat(), Q3Url::decode(), FindWidget::FindWidget(), QUrl::fromPercentEncoding(), QNetworkCookie::parseCookies(), QDBusError::QDBusError(), Q3Process::start(), QClipboard::text(), QLocale::timeFormat(), QCoreApplication::translate(), Q3UriDrag::uriToUnicodeUri(), and vsprintf().

{
    if (!str)
        return QString();
    if (size < 0)
        size = qstrlen(str);

    QString result;
    result.resize(size); // worst case
    ushort *qch = result.d->data;
    uint uc = 0;
    uint min_uc = 0;
    int need = 0;
    int error = -1;
    uchar ch;
    int i = 0;

    // skip utf8-encoded byte order mark
    if (size >= 3
        && (uchar)str[0] == 0xef && (uchar)str[1] == 0xbb && (uchar)str[2] == 0xbf)
        i += 3;

    for (; i < size; ++i) {
        ch = str[i];
        if (need) {
            if ((ch&0xc0) == 0x80) {
                uc = (uc << 6) | (ch & 0x3f);
                need--;
                if (!need) {
                    if (uc > 0xffffU && uc < 0x110000U) {
                        // surrogate pair
                        *qch++ = QChar::highSurrogate(uc);
                        uc = QChar::lowSurrogate(uc);
                    } else if ((uc < min_uc) || (uc >= 0xd800 && uc <= 0xdfff) || (uc >= 0xfffe)) {
                  // overlong seqence, UTF16 surrogate or BOM
                        uc = QChar::ReplacementCharacter;
                    }
                    *qch++ = uc;
                }
            } else {
                i = error;
                need = 0;
                *qch++ = QChar::ReplacementCharacter;
            }
        } else {
            if (ch < 128) {
                *qch++ = ch;
            } else if ((ch & 0xe0) == 0xc0) {
                uc = ch & 0x1f;
                need = 1;
                error = i;
                min_uc = 0x80;
            } else if ((ch & 0xf0) == 0xe0) {
                uc = ch & 0x0f;
                need = 2;
                error = i;
                min_uc = 0x800;
            } else if ((ch&0xf8) == 0xf0) {
                uc = ch & 0x07;
                need = 3;
                error = i;
                min_uc = 0x10000;
            } else {
                // Error
                *qch++ = QChar::ReplacementCharacter;
            }
        }
    }
    if (need) {
        // we have some invalid characters remaining we need to add to the string
        for (int i = error; i < size; ++i)
            *qch++ = QChar::ReplacementCharacter;
    }

    result.truncate(qch - result.d->data);
    return result;
}


Generated by  Doxygen 1.6.0   Back to index