<commit_before>/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "q3cstring.h"
#include "qregexp.h"
#include "qdatastream.h"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>

QT_BEGIN_NAMESPACE

/*****************************************************************************
  Q3CString member functions
 *****************************************************************************/

/*!
    \class Q3CString
    \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:
    \snippet doc/src/snippets/code/src_qt3support_tools_q3cstring.cpp 0

    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:
    \snippet doc/src/snippets/code/src.qt3support.tools.q3cstring.cpp 1

    \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()
*/
<commit_msg>doc: Clarified what happens with a null Q3CString.<commit_after>/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (<EMAIL>)
**
** This file is part of the Qt3Support module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.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 the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://www.qtsoftware.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "q3cstring.h"
#include "qregexp.h"
#include "qdatastream.h"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>

QT_BEGIN_NAMESPACE

/*****************************************************************************
  Q3CString member functions
 *****************************************************************************/

/*!
    \class Q3CString
    \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 default constructed Q3CString is \e null, i.e. both the length
    and the data pointer are 0 and isNull() returns true.
    
    \note However, if you ask for the data pointer of a null Q3CString
    by calling data(), then because the internal representation of the
    null Q3CString is shared, it will be detached and replaced with a
    non-shared, empty representation, a non-null data pointer will be
    returned, and subsequent calls to isNull() will return false. But
    if you ask for the data pointer of a null Q3CString by calling
    constData(), the shared internal representation is not detached, a
    null data pointer is returned, and subsequent calls to isNull()
    will continue to return true.

    A Q3CString that references the empty string ("", a single '\0'
    char) is \e empty, i.e. isEmpty() returns true.  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:
    \snippet doc/src/snippets/code/src_qt3support_tools_q3cstring.cpp 0

    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.

    \note If you ask for the data pointer of a null Q3CString by
    calling data(), then because the internal representation of the
    null Q3CString is shared, it will be detached and replaced with a
    non-shared, empty representation, a non-null data pointer will be
    returned, and subsequent calls to isNull() will return false. But
    if you ask for the data pointer of a null Q3CString by calling
    constData(), the shared internal representation is not detached, a
    null data pointer is returned, and subsequent calls to isNull()
    will continue to return true.

    Example:
    \snippet doc/src/snippets/code/src.qt3support.tools.q3cstring.cpp 1

    \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()
*/

{'fraction_non_alphanumeric': 0.1162149310508797, 'fraction_numerical': 0.011840228245363765, 'mean_word_length': 3.323807565789474, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804565', 'n_tokens_mistral': 6214, 'n_tokens_neox': 5915, 'n_words': 2625}