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

📄 fast_string_concatenator.hpp

📁 用STL的方式封装了WindowsAPI、COM调用、ACE、ATL、MFC、WTL等多种组件
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        stlsoft/string/fast_string_concatenator.hpp (originally stlsoft_fast_string_concatenator.h)
 *
 * Purpose:     Fast string concatenator.
 *
 * Created:     4th November 2003 (the time added to STLSoft libraries)
 * Updated:     10th June 2006
 *
 * Thanks to:   Sean Kelly for picking up on my gratuitous use of pointers
 *              in the first implementation.
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 2003-2006, Matthew Wilson and Synesis Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
 *   any contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ////////////////////////////////////////////////////////////////////// */


/** \file stlsoft/string/fast_string_concatenator.hpp
 *
 * \brief [C++ only] Definition of the stlsoft::fast_string_concatenator
 *  class template.
 *  (\ref group__library__string "String" Library.)
 */
///
/// Fast string concatenator.

#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR
#define STLSOFT_INCL_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR_MAJOR       4
# define STLSOFT_VER_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR_MINOR       0
# define STLSOFT_VER_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR_REVISION    1
# define STLSOFT_VER_STLSOFT_STRING_HPP_FAST_STRING_CONCATENATOR_EDIT        126
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Compatibility
 */

/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
STLSOFT_COMPILER_IS_WATCOM:
[Incompatibilies-end]
 */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
# include <stlsoft/stlsoft.h>
#endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */

#if 0
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
    _MSC_VER < 1200
# error stlsoft_fast_string_concatenator.h is not compatible with Visual C++ 5.0 or earlier
#endif /* compiler */
#endif /* 0 */

#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */
template<   ss_typename_param_k S
        ,   ss_typename_param_k C
        ,   ss_typename_param_k T
        >
class fast_string_concatenator;
#ifndef _STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */

#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
# include <stlsoft/string/char_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
#include <algorithm>

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * Classes
 */

/** \brief This is used as a seed when namespace issues make the selection
 *   of the fast_string_concatenator overloads of operator +() ambiguous.
 *
 * \ingroup group__library__string
 */
class fsc_seed
{};

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION

template <ss_typename_param_k S>
class fsc_seed_t
    : public fsc_seed
{
public:
    typedef S   string_type;
};

#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

#ifndef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
# error fast_string_concatenator cannot be used when default template arguments are not supported
#endif /* !STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */

/** \brief Expression template class which provides fast string concatenation
 *
 * \ingroup group__library__string
 */
template<   ss_typename_param_k S
        ,   ss_typename_param_k C = ss_typename_type_def_k S::value_type
        ,   ss_typename_param_k T = char_traits<C>
        >
class fast_string_concatenator
{
/// \name Member types
/// @{
public:
    typedef S                                   string_type;
    typedef C                                   char_type;
    typedef T                                   traits_type;
    typedef fast_string_concatenator<S, C, T>   class_type;
    typedef ss_size_t                           size_type;
private:
#if !defined(STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE)
    typedef ss_typename_type_k S::iterator      string_iterator_type;
#endif /* !STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
/// @}

/// \name Construction
/// @{
public:
    fast_string_concatenator(string_type const &lhs, string_type const &rhs);
    fast_string_concatenator(string_type const &lhs, char_type const *rhs);
    fast_string_concatenator(string_type const &lhs, char_type const rhs);
    fast_string_concatenator(char_type const *lhs, string_type const &rhs);
    fast_string_concatenator(char_type const lhs, string_type const &rhs);

    fast_string_concatenator(class_type const &lhs, string_type const &rhs);
    fast_string_concatenator(class_type const &lhs, char_type const *rhs);
    fast_string_concatenator(class_type const &lhs, char_type const rhs);

    fast_string_concatenator(fsc_seed const &lhs, string_type const &rhs);

    // These constructors are for handling embedded braces in the concatenation sequences, and represent the pathological case
    fast_string_concatenator(class_type const &lhs, class_type const &rhs);
    fast_string_concatenator(string_type const &lhs, class_type const &rhs);
    fast_string_concatenator(char_type const *lhs, class_type const &rhs);
    fast_string_concatenator(char_type const lhs, class_type const &rhs);
/// @}

/// \name Accessors
/// @{
public:
    operator string_type() const;
/// @}

/// \name Implementation
/// @{
private:
    size_type length() const
    {
        return m_lhs.length() + m_rhs.length();
    }
#if defined(STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE)
    char_type *write(char_type *s) const
    {
        return m_rhs.write(m_lhs.write(s));
    }
#else /* ? STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
    string_iterator_type write(string_iterator_type s) const
    {
        return m_rhs.write(m_lhs.write(s));
    }
#endif /* STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */

private:
    struct Data;

    friend struct Data;

    struct Data
    {
        enum DataType
        {
                seed    // Argument was the seed type
            ,   single  // Argument was a single character
            ,   cstring // Argument was a C-string or a string object
            ,   concat  // Argument was another concatenator
        };

        /// Represents a C-style string
        struct CString
        {
            ss_size_t           len;
            char_type const     *s;
        };
        /// Represents a union of the possible concatenation types
        union DataRef
        {
            CString             cstring;
            char_type           ch;
            class_type  const   *concat;
        };
        Data(string_type const &str)
            : type(cstring)
        {
            ref.cstring.len = str.length();
            ref.cstring.s   = str.data();
        }
        Data(char_type const *s)
            : type(cstring)
        {
            ref.cstring.len = traits_type::length(s);
            ref.cstring.s   = s;
        }
        Data(char_type const ch)
            : type(single)
        {
            ref.ch = ch;
        }
        Data(class_type const &fc)
            : type(concat)
        {
            ref.concat = &fc;
        }
        Data(fsc_seed const &)
            : type(seed)
        {}

        size_type length() const
        {
            size_type  len;

            // Note that a default is not used in the switch statement because, even on very high
            // optimisations, it caused a 1-4% hit on most of the compilers
            STLSOFT_ASSERT(type == cstring || type == single || type == concat || type == seed);

            switch(type)
            {
                case    seed:
                    len = 0;
                    break;
                case    single:
                    len = 1;
                    break;
                case    cstring:
                    len = ref.cstring.len;
                    break;
                case    concat:
                    len = ref.concat->length();
                    break;
            }

            STLSOFT_ASSERT(!(len < 0));

            return len;
        }

#if defined(STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE)
        char_type *write(char_type *s) const
#else /* ? STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
        string_iterator_type write(string_iterator_type s) const
#endif /* STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
        {
            size_type  len;

            // Note that a default is not used in the switch statement because, even on very high
            // optimisations, it caused a 1-4% hit on most of the compilers
            STLSOFT_ASSERT(type == cstring || type == single || type == concat || type == seed);

#if defined(STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE)
            // Check that "iterator" is contiguous
            STLSOFT_ASSERT(&[1]&*s == s + 1);
#else /* ? STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
            // Check that iterator is random access
            STLSOFT_ASSERT(&s[1] == s + 1);
#endif /* STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */

            switch(type)
            {
                case    seed:
                    break;
                case    single:
                    *(s++) = ref.ch;
                    break;
                case    cstring:
                    len = ref.cstring.len;
#if defined(STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE)
                    memcpy(s, ref.cstring.s, sizeof(C) * (len));
#else /* ? STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
                    std::copy(&ref.cstring.s[0], &ref.cstring.s[0] + len, s);
#endif /* STLSOFT_FAST_STRING_CONCATENATION_ASSUME_CONTIGUOUS_STORAGE */
                    s += len;
                    break;
                case    concat:
                    s = ref.concat->write(s);
                    break;
            }

            return s;
        }

        DataRef         ref;
        DataType const  type;
    };
/// @}

/// \name Construction

⌨️ 快捷键说明

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