⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 q3cstring.cpp

📁 奇趣公司比较新的qt/emd版本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt3Support module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "q3cstring.h"#include "qregexp.h"#include "qdatastream.h"#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <ctype.h>#include <limits.h>/*****************************************************************************  Q3CString member functions *****************************************************************************//*!    \class Q3CString q3cstring.h    \reentrant    \brief The Q3CString class provides an abstraction of the classic C    zero-terminated char array (char *).    \compat    Q3CString tries to behave like a more convenient \c{const char *}.    The price of doing this is that some algorithms will perform    badly. For example, append() is O(length()) since it scans for a    null terminator. Although you might use Q3CString for text that is    never exposed to the user, for most purposes, and especially for    user-visible text, you should use QString. QString provides    implicit sharing, Unicode and other internationalization support,    and is well optimized.    Note that for the Q3CString methods that take a \c{const char *}    parameter the \c{const char *} must either be 0 (null) or not-null    and '\0' (NUL byte) terminated; otherwise the results are    undefined.    A Q3CString that has not been assigned to anything is \e null, i.e.    both the length and the data pointer is 0. A Q3CString that    references the empty string ("", a single '\0' char) is \e empty.    Both null and empty Q3CStrings are legal parameters to the methods.    Assigning \c{const char *} 0 to Q3CString produces a null Q3CString.    The length() function returns the length of the string; resize()    resizes the string and truncate() truncates the string. A string    can be filled with a character using fill(). Strings can be left    or right padded with characters using leftJustify() and    rightJustify(). Characters, strings and regular expressions can be    searched for using find() and findRev(), and counted using    contains().    Strings and characters can be inserted with insert() and appended    with append(). A string can be prepended with prepend().    Characters can be removed from the string with remove() and    replaced with replace().    Portions of a string can be extracted using left(), right() and    mid(). Whitespace can be removed using stripWhiteSpace() and    simplifyWhiteSpace(). Strings can be converted to uppercase or    lowercase with upper() and lower() respectively.    Strings that contain numbers can be converted to numbers with    toShort(), toInt(), toLong(), toULong(), toFloat() and toDouble().    Numbers can be converted to strings with setNum().    Many operators are overloaded to work with Q3CStrings. Q3CString    also supports some more obscure functions, e.g. sprintf(),    setStr() and setExpand().    \sidebar Note on Character Comparisons    In Q3CString the notion of uppercase and lowercase and of which    character is greater than or less than another character is locale    dependent. This affects functions which support a case insensitive    option or which compare or lowercase or uppercase their arguments.    Case insensitive operations and comparisons will be accurate if    both strings contain only ASCII characters. (If \c $LC_CTYPE is    set, most Unix systems do "the right thing".) Functions that this    affects include contains(), find(), findRev(), \l operator<(), \l    operator<=(), \l operator>(), \l operator>=(), lower() and    upper().    This issue does not apply to \l{QString}s since they represent    characters using Unicode.    \endsidebar    Performance note: The Q3CString methods for QRegExp searching are    implemented by converting the Q3CString to a QString and performing    the search on that. This implies a deep copy of the Q3CString data.    If you are going to perform many QRegExp searches on a large    Q3CString, you will get better performance by converting the    Q3CString to a QString yourself, and then searching in the QString.*//*!    \fn Q3CString Q3CString::left(uint len)  const    \internal*//*!    \fn Q3CString Q3CString::right(uint len) const    \internal*//*!    \fn Q3CString Q3CString::mid(uint index, uint len) const    \internal*//*!    \fn Q3CString  Q3CString::lower() const    Use QByteArray::toLower() instead.*//*!    \fn Q3CString  Q3CString::upper() const    Use QByteArray::toUpper() instead.*//*!    \fn Q3CString  Q3CString::stripWhiteSpace() const    Use QByteArray::trimmed() instead.*//*!    \fn Q3CString  Q3CString::simplifyWhiteSpace() const    Use QByteArray::simplified() instead.*//*!    \fn Q3CString& Q3CString::insert(uint index, const char *c)    \internal*//*!    \fn Q3CString& Q3CString::insert(uint index, char c)    \internal*//*!    \fn Q3CString& Q3CString::prepend(const char *c)    \internal*//*!    \fn Q3CString& Q3CString::remove(uint index, uint len)    \internal*//*!    \fn Q3CString& Q3CString::replace(uint index, uint len, const char *c)    \internal*//*!    \fn Q3CString& Q3CString::replace(char c, const Q3CString &after)    \internal*//*!    \fn Q3CString& Q3CString::replace(char c, const char *after)    \internal*//*!    \fn Q3CString& Q3CString::replace(const Q3CString &b, const Q3CString &a)    \internal*//*!    \fn Q3CString& Q3CString::replace(const char *b, const char *a)    \internal*//*!    \fn Q3CString& Q3CString::replace(char b, char a)    \internal*//*!    \fn Q3CString::Q3CString()    Constructs a null string.    \sa isNull()*//*!    \fn Q3CString::Q3CString(const QByteArray &ba)    Constructs a copy of \a ba.*//*!    \fn Q3CString::Q3CString(const Q3CString &s)    Constructs a shallow copy \a s.*//*! \fn Q3CString::Q3CString(int size)    Constructs a string with room for \a size characters, including    the '\0'-terminator. Makes a null string if \a size == 0.    If \a size \> 0, then the first and last characters in the string    are initialized to '\0'. All other characters are uninitialized.    \sa resize(), isNull()*//*! \fn Q3CString::Q3CString(const char *str)    Constructs a string that is a deep copy of \a str.    If \a str is 0 a null string is created.    \sa isNull()*//*! \fn Q3CString::Q3CString(const char *str, uint maxsize)    Constructs a string that is a deep copy of \a str. The copy will    be at most \a maxsize bytes long including the '\0'-terminator.    Example:    \code    Q3CString str("helloworld", 6); // assigns "hello" to str    \endcode    If \a str contains a 0 byte within the first \a maxsize bytes, the    resulting Q3CString will be terminated by this 0. If \a str is 0 a    null string is created.    \sa isNull()*//*!    \fn Q3CString &Q3CString::operator=(const QByteArray &ba)    Assigns byte array \a ba to this Q3CString.*//*!    \fn Q3CString &Q3CString::operator=(const Q3CString &s)    Assigns a shallow copy of \a s to this string and returns a    reference to this string.*//*!    \fn Q3CString &Q3CString::operator=(const char *str)    \overload    Assigns a deep copy of \a str to this string and returns a    reference to this string.    If \a str is 0 a null string is created.    \sa isNull()*//*    \fn bool Q3CString::isNull() const    Returns true if the string is null, i.e. if data() == 0; otherwise    returns false. A null string is also an empty string.    Example:    \code    Q3CString a;                // a.data() == 0,  a.size() == 0, a.length() == 0    Q3CString b == "";        // b.data() == "", b.size() == 1, b.length() == 0    a.isNull();                // true  because a.data() == 0    a.isEmpty();        // true  because a.length() == 0    b.isNull();                // false because b.data() == ""    b.isEmpty();        // true  because b.length() == 0    \endcode    \sa isEmpty(), length(), size()*//*    \fn bool Q3CString::isEmpty() const    Returns true if the string is empty, i.e. if length() == 0;    otherwise returns false. An empty string is not always a null    string.    See example in isNull().    \sa isNull(), length(), size()*//*    \fn uint Q3CString::length() const    Returns the length of the string, excluding the '\0'-terminator.    Equivalent to calling \c strlen(data()).    Null strings and empty strings have zero length.    \sa size(), isNull(), isEmpty()*//*    \fn bool Q3CString::truncate(uint pos)    Truncates the string at position \a pos.    Equivalent to calling \c resize(pos+1).    Example:    \code    Q3CString s = "truncate this string";    s.truncate(5);                      // s == "trunc"    \endcode    \sa resize()*//*!    Implemented as a call to the native vsprintf() (see the manual for    your C library).    If the string is shorter than 256 characters, this sprintf() calls    resize(256) to decrease the chance of memory corruption. The    string is resized back to its actual length before sprintf()    returns.    Example:    \code    Q3CString s;    s.sprintf("%d - %s", 1, "first");                // result < 256 chars    Q3CString big(25000);                        // very long string    big.sprintf("%d - %s", 2, longString);        // result < 25000 chars    \endcode    \warning All vsprintf() implementations will write past the end of    the target string (*this) if the \a format specification and    arguments happen to be longer than the target string, and some    will also fail if the target string is longer than some arbitrary    implementation limit.    Giving user-supplied arguments to sprintf() is risky: Sooner or    later someone will paste a huge line into your application.*/Q3CString &Q3CString::sprintf(const char *format, ...){    detach();    va_list ap;    va_start(ap, format);    if (size() < 256)        resize(256);                // make string big enough    qvsnprintf(data(), size(), format, ap);    resize(qstrlen(constData()));    va_end(ap);    return *this;}/*!    \fn Q3CString Q3CString::copy() const    Returns a deep copy of this string.*//*!    Returns a string of length \a width (plus one for the terminating    '\0') that contains this string padded with the \a fill character.    If the length of the string exceeds \a width and \a truncate is    false (the default), then the returned string is a copy of the    string. If the length of the string exceeds \a width and \a    truncate is true, then the returned string is a left(\a width).    Example:    \code    Q3CString s("apple");    Q3CString t = s.leftJustify(8, '.');  // t == "apple..."    \endcode    \sa rightJustify()*/Q3CString Q3CString::leftJustify(uint width, char fill, bool truncate) const{    Q3CString result;    int len = qstrlen(constData());    int padlen = width - len;    if (padlen > 0) {        result.resize(len+padlen);        memcpy(result.data(), constData(), len);        memset(result.data()+len, fill, padlen);    } else {        if (truncate)            result = left(width);        else            result = *this;    }    return result;}/*!    Returns a string of length \a width (plus one for the terminating    '\0') that contains zero or more of the \a fill character followed    by this string.    If the length of the string exceeds \a width and \a truncate is    false (the default), then the returned string is a copy of the    string. If the length of the string exceeds \a width and \a    truncate is true, then the returned string is a left(\a width).    Example:    \code    Q3CString s("pie");    Q3CString t = s.rightJustify(8, '.');  // t == ".....pie"    \endcode    \sa leftJustify()*/Q3CString Q3CString::rightJustify(uint width, char fill, bool truncate) const{    Q3CString result;    int len = qstrlen(constData());    int padlen = width - len;    if (padlen > 0) {        result.resize(len+padlen);        memset(result.data(), fill, padlen);        memcpy(result.data()+padlen, constData(), len);    } else {        if (truncate)            result = left(width);        else            result = *this;    }    return result;}/*!    Returns the string converted to a \c long value.    If \a ok is not 0: *\a ok is set to false if the string is not a    number, or if it has trailing garbage; otherwise *\a ok is set to    true.*/long Q3CString::toLong(bool *ok) const{    const char *p = constData();    long val=0;    const long max_mult = 214748364;    bool is_ok = false;    int neg = 0;    if (!p)        goto bye;    while (isspace((uchar) *p))                // skip leading space        p++;    if (*p == '-') {        p++;        neg = 1;    } else if (*p == '+') {        p++;    }    if (!isdigit((uchar) *p))        goto bye;    while (isdigit((uchar) *p)) {        if (val > max_mult || (val == max_mult && (*p-'0') > 7+neg))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -