📄 static_string.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/string/static_string.hpp (originally MStkStr.h, ::SynesisDev)
*
* Purpose: basic_static_string class template.
*
* Created: 11th June 1994
* Updated: 10th June 2006
*
* Home: http://stlsoft.org/
*
* Copyright (c) 1994-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/static_string.hpp
*
* \brief [C++ only] Definition of the stlsoft::basic_static_string class
* template.
* (\ref group__library__string "String" Library.)
*/
///
/// basic_static_string class template.
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STATIC_STRING
#define STLSOFT_INCL_STLSOFT_STRING_HPP_STATIC_STRING
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_STRING_HPP_STATIC_STRING_MAJOR 4
# define STLSOFT_VER_STLSOFT_STRING_HPP_STATIC_STRING_MINOR 0
# define STLSOFT_VER_STLSOFT_STRING_HPP_STATIC_STRING_REVISION 1
# define STLSOFT_VER_STLSOFT_STRING_HPP_STATIC_STRING_EDIT 173
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
/*
[Incompatibilies-start]
STLSOFT_COMPILER_IS_DMC: __DMC__<0x0839
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 defined(STLSOFT_COMPILER_IS_DMC) && \
__DMC__ < 0x0839
# error stlsoft/string/static_string.hpp is not compatible with Digital Mars C/C++ 3.38 or earlier
#endif /* compiler */
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1200
# error stlsoft/string/static_string.hpp is not compatible with Visual C++ 5.0 or earlier
#endif /* compiler */
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
# include <stlsoft/string/char_traits.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS
# include <stlsoft/collections/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_COLLECTIONS */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
# include <stlsoft/memory/auto_buffer.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
#ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
# include <stlsoft/memory/allocator_selector.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
# include <stlsoft/util/std_swap.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_GENERATORS
# include <stlsoft/util/std/iterator_generators.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_GENERATORS */
#ifdef STLSOFT_CF_EXCEPTION_SUPPORT
# include <stdexcept> // for std::out_of_range
#endif /* !STLSOFT_CF_EXCEPTION_SUPPORT */
#ifdef STLSOFT_UNITTEST
# include <string>
# include <stdio.h>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
// class static_string
/** \brief Simple string class using fixed-size static-based storage
*
* \param C The character type
* \param CCH The number of characters in the fixed-side buffer, not including the null-terminator
* \param T The traits type. On translators that support default template arguments this is defaulted to char_traits<C>
*
* \ingroup group__library__string
*/
template< ss_typename_param_k C
#if !defined(STLSOFT_COMPILER_IS_BORLAND)
, ss_size_t CCH
#else /* ? compiler */
, ss_size_t cch
#endif /* compiler */
#ifdef STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT
, ss_typename_param_k T = char_traits<C>
#else /* ? STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
, ss_typename_param_k T = /* char_traits<C> */
#endif /* STLSOFT_CF_TEMPLATE_CLASS_DEFAULT_CLASS_ARGUMENT_SUPPORT */
>
class basic_static_string
: public stl_collection_tag
{
#if !defined(STLSOFT_COMPILER_IS_BORLAND)
enum { cch = CCH };
#else /* ? compiler */
enum { CCH_ = cch };
#endif /* compiler */
/// \name Types
/// @{
public:
/// The value type
typedef C value_type;
/// The traits type
typedef T traits_type;
/// The allocator type
typedef ss_typename_type_k allocator_selector<C>::allocator_type allocator_type;
/// The current parameterisation of the type
typedef basic_static_string<C, cch, T> class_type;
/// The character type
typedef value_type char_type;
/// The pointer type
typedef value_type *pointer;
/// The non-mutable (const) pointer type
typedef value_type const *const_pointer;
/// The reference type
typedef value_type &reference;
/// The non-mutable (const) reference type
typedef value_type const &const_reference;
/// The size type
typedef ss_size_t size_type;
/// The difference type
typedef ss_ptrdiff_t difference_type;
/// The iterator type
typedef
#if !defined(STLSOFT_COMPILER_IS_BORLAND)
ss_typename_type_k
#endif /* compiler */
pointer_iterator < value_type
, pointer
, reference
>::type iterator;
/// The non-mutating (const) iterator type
typedef
#if !defined(STLSOFT_COMPILER_IS_BORLAND)
ss_typename_type_k
#endif /* compiler */
pointer_iterator < value_type const
, const_pointer
, const_reference
>::type const_iterator;
#if defined(STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT)
/// The mutating (non-const) reverse iterator type
typedef ss_typename_type_k reverse_iterator_generator < iterator
, value_type
, reference
, pointer
, difference_type
>::type reverse_iterator;
/// The non-mutating (const) reverse iterator type
typedef ss_typename_type_k const_reverse_iterator_generator < const_iterator
, value_type
, const_reference
, const_pointer
, difference_type
>::type const_reverse_iterator;
#endif /* STLSOFT_CF_BIDIRECTIONAL_ITERATOR_SUPPORT */
private:
typedef auto_buffer_old<char_type
, allocator_type
> buffer_type_;
/// @}
/// \name Construction
/// @{
public:
/// Default constructor
basic_static_string();
/// Copy constructor
basic_static_string(class_type const &rhs);
/// Construct from the given string at the specified position
basic_static_string(class_type const &s, size_type pos);
/// Construct with \c n characters from the given string at the specified position
basic_static_string(class_type const &s, size_type pos, size_type n);
/// Construct from the given character string
basic_static_string(char_type const *s);
/// Construct with \c n characters from the given character string
basic_static_string(char_type const *s, size_type n);
/// Construct with \c n characters each set to \c ch
basic_static_string(size_type n, char_type ch);
/// Construct from the range [first:last)
#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT)
basic_static_string(char_type const *f, char_type const *t);
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
template <ss_typename_param_k II>
basic_static_string(II first, II last)
: m_length(static_cast<ss_size_t>(stlsoft_ns_qual_std(distance)(first, last)))
{
STLSOFT_MESSAGE_ASSERT("incident string too large for static_string construction", !(max_size() < m_length));
buffer_type_ buffer(m_length);
stlsoft_ns_qual_std(copy)(first, last, buffer.begin());
traits_type::copy(&m_buffer[0], &buffer[0], buffer.size());
m_buffer[m_length] = '\0';
m_buffer[max_size()] = '\0';
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
/// Destructor
~basic_static_string() stlsoft_throw_0();
/// @}
/// \name Assignment
/// @{
public:
/// Assigns from the given character string
class_type &assign(const char_type *s);
/// Assigns with \c n characters from the given character string
class_type &assign(const char_type *s, size_type n);
/// Assigns with \c n characters from the given character string at the specified position
class_type &assign(class_type const &str, size_type pos, size_type n);
/// Assigns from the given string
class_type &assign(class_type const &str);
/// Assigns \c n characters with the value \c ch
class_type &assign(size_type n, char_type c);
/// Assigns from the range [first:last)
#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT)
class_type &assign(const_iterator first, const_iterator last);
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
template <ss_typename_param_k II>
class_type &assign(II first, II last)
{
# if defined(STLSOFT_COMPILER_IS_GCC) && \
__GNUC__ < 3
typedef ss_typename_type_k std::iterator_traits<II> traits_t;
return assign_(first, last, traits_t::iterator_category());
# elif defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
return assign_(first, last, stlsoft_iterator_query_category_ptr(II, first));
# else /* ? compiler */
return assign_(first, last, stlsoft_iterator_query_category(II, first));
# endif /* compiler */
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
/// Copy assignment operator
class_type const &operator =(class_type const &rhs);
/// Assignment operator
class_type const &operator =(char_type const *s);
/// Assignment operator
class_type const &operator =(char_type ch);
/// @}
/// \name Appending
/// @{
public:
/// Appends the given character string
class_type &append(char_type const *s);
/// Appends \c cch characters from the given character string
class_type &append(char_type const *s, size_type cch);
/// Assigns \c cch characters from the given character string at the specified position
class_type &append(class_type const &str, size_type pos, size_type cch);
/// Appends the given string
class_type &append(class_type const &str);
/// Appends \c cch characters with the value \c ch
class_type &append(size_type cch, char_type ch);
/// Appends the range [first:last)
#if !defined(STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT)
class_type &append(const_iterator first, const_iterator last);
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
template <ss_typename_param_k II>
class_type &append(II first, II last)
{
# if defined(STLSOFT_COMPILER_IS_GCC) && \
__GNUC__ < 3
typedef ss_typename_type_k std::iterator_traits<II> traits_t;
return append_(first, last, traits_t::iterator_category());
# elif defined(STLSOFT_COMPILER_IS_MWERKS) || \
defined(STLSOFT_COMPILER_IS_DMC)
return append_(first, last, stlsoft_iterator_query_category_ptr(II, first));
# else /* ? compiler */
return append_(first, last, stlsoft_iterator_query_category(II, first));
# endif /* compiler */
}
#endif /* STLSOFT_CF_MEMBER_TEMPLATE_RANGE_METHOD_SUPPORT */
/// Concatenation operator
class_type &operator +=(char_type ch);
/// Concatenation operator
class_type &operator +=(char_type const *s);
/// Concatenation operator
class_type &operator +=(class_type const &rhs);
/// Appends a single character
void push_back(char_type ch);
/// @}
/// \name Operations
/// @{
public:
/// Reserves at least n characters
void reserve(size_type n);
/// Swaps the contents between \c this and \c other
void swap(class_type &other);
/// Resizes the string
///
/// \param n The new size of the string
/// \param ch The value with which to initialise additional items if the string is expanded
void resize(size_type n, value_type ch = value_type());
/// Empties the string
void clear();
/// @}
/// \name Attributes
/// @{
public:
/// The number of elements in the string
size_type size() const;
/// The maximum number of elements that can be stored in the string
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -