📄 string_tokeniser.hpp
字号:
/* /////////////////////////////////////////////////////////////////////////
* File: stlsoft/string/string_tokeniser.hpp
*
* Purpose: String token parsing class.
*
* Created: 6th January 2001
* Updated: 22nd March 2007
*
* Home: http://stlsoft.org/
*
* Copyright (c) 2001-2007, 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/string_tokeniser.hpp
*
* \brief [C++ only] Definition of the stlsoft::string_tokeniser class
* template
* (\ref group__library__string "String" Library).
*/
#ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TOKENISER
#define STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TOKENISER
#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_TOKENISER_MAJOR 5
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_TOKENISER_MINOR 1
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_TOKENISER_REVISION 6
# define STLSOFT_VER_STLSOFT_STRING_HPP_STRING_TOKENISER_EDIT 218
#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/string_tokeniser.hpp is not compatible with Digital Mars C/C++ 3.38 or earlier
#endif /* compiler */
#if defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1100
# error stlsoft/string/string_tokeniser.hpp is not compatible with Visual C++ 5.0 or earlier
#endif /* compiler */
#ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
# include <stlsoft/util/std/iterator_helper.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
#ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
# include <stlsoft/shims/access/string.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
#ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
# include <stlsoft/collections/util/collections.hpp>
#endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
#include <iterator> // for std::distance()
#ifdef STLSOFT_UNITTEST
# ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
# include <stlsoft/string/simple_string.hpp>
# endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
# ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
# include <stlsoft/string/string_view.hpp>
# endif /* STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW */
# include <string>
#endif /* STLSOFT_UNITTEST */
/* /////////////////////////////////////////////////////////////////////////
* Compatibility
*/
#if defined(STLSOFT_STRING_TOKENISER_USE_DELIMITER_INDIRECTION) || \
( ( defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER < 1300) || \
( defined(STLSOFT_COMPILER_IS_INTEL) && \
defined(WIN32) && \
_MSC_VER < 1300))
# define STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION
#endif /* compiler */
/* /////////////////////////////////////////////////////////////////////////
* Namespace
*/
#ifndef _STLSOFT_NO_NAMESPACE
namespace stlsoft
{
#endif /* _STLSOFT_NO_NAMESPACE */
/* /////////////////////////////////////////////////////////////////////////
* Classes
*/
// string_tokeniser_ignore_blanks
/** \brief A tokenising policy for specifying whether blanks will be included or ignored (now deprecated; replaced by skip_blank_tokens)
*
* \ingroup group__library__string
*
* This policy determines whether a tokenisation will ignore blanks, or will present them as
* (empty) entries in the sequence to the caller.
*
* \param B A boolean as to whether blanks should be ignored (\c true) or preserved (\c false)
*
* \deprecated Use skip_blank_tokens instead
*/
template <ss_bool_t B>
struct string_tokeniser_ignore_blanks
{
enum { value = B };
};
/** \brief A tokenising policy for specifying whether blanks will be included or ignored
*
* \ingroup group__library__string
*
* This policy determines whether a tokenisation will ignore blanks, or will present them as
* (empty) entries in the sequence to the caller.
*
* \param B A boolean as to whether blanks should be ignored (\c true) or preserved (\c false)
*
* \note This supercedes string_tokeniser_ignore_blanks
*/
template <ss_bool_t B>
struct skip_blank_tokens
{
enum { value = B };
};
// string_tokeniser_type_traits
/** \brief A traits class for detecting features of the string type and the value type used to specialise string_tokeniser
*
* \ingroup group__library__string
*
* This traits class has three responsibilities. First, it defines a number of
* member types that are used by the string_tokeniser and
* string_tokeniser::iterator classes: value_type, const_iterator_type.
*
* Second, it provides a means by which the iterators of the string_tokeniser
* specialisation's string type can be elicited, by defining the (static)
* methods begin() and end().
*
* Third, it provides a means by which an instance of the string_tokeniser
* specialisation's value type can be constructed from a pair of iterator of
* the string type, by defining the (static) create() method.
*
* \param S The string tokeniser string type
* \param V The string tokeniser value type
*/
template< ss_typename_param_k S
, ss_typename_param_k V
>
struct string_tokeniser_type_traits
{
/// \name Member Types
/// @{
private:
/// The string type
typedef S string_type;
/// The tokeniser value type
typedef V tokeniser_value_type;
public:
/// The value type
typedef ss_typename_type_k S::value_type value_type;
/// The non-mutable (const) iterator type
typedef ss_typename_type_k S::const_iterator const_iterator_type;
/// @}
/// \name Operations
/// @{
public:
/// Returns the start of the contained sequence of the given string
static const_iterator_type begin(string_type const& s)
{
return s.begin();
}
/// Returns the end of the contained sequence of the given string
static const_iterator_type end(string_type const& s)
{
return s.end();
}
/// Creates an instance of the string from the given range [f:t)
static tokeniser_value_type create(const_iterator_type f, const_iterator_type t)
{
/* There's a bug in the Metrowerks 3.0 standard library string
* implementation, such that constructing from a range leaves
* a corrupted sequence due to an optimistic allocation
* requirement calculation result being stored as the length
*/
#if defined(STLSOFT_COMPILER_IS_MWERKS) || \
( ( defined(STLSOFT_COMPILER_IS_INTEL) || \
defined(STLSOFT_COMPILER_IS_MSVC) && \
_MSC_VER == 1300))
/// The size type
typedef ss_typename_type_k S::size_type size_type;
return tokeniser_value_type(&*f, static_cast<size_type>(t - f));
#else /* ? compiler */
return tokeniser_value_type(f, t);
#endif /* compiler */
}
/// @}
};
// string_tokeniser_comparator
/** \brief A generic comparator, used to specialise string_tokeniser, that covers most string and delimiter types
*
* \ingroup group__library__string
*
* This the default tokeniser comparator, providing functionality for both
* single-character and fixed string delimiters.
*
* \param D The delimiter type
* \param S The string type
* \param T The traits type
*/
template< ss_typename_param_k D
, ss_typename_param_k S
, ss_typename_param_k T
>
struct string_tokeniser_comparator
{
/// \name Member Types
/// @{
public:
/// The delimiter type
typedef D delimiter_type;
/// The string type
typedef S string_type;
/// The traits type
typedef T traits_type;
/// The non-mutating (const) iterator type
typedef ss_typename_type_k traits_type::const_iterator_type const_iterator;
private:
typedef string_tokeniser_comparator<D, S, T> class_type;
/// @}
/// \name Implementation
/// @{
private:
#if defined(STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT) && \
( !defined(STLSOFT_COMPILER_IS_MSVC) || \
_MSC_VER >= 1200)
/// Evaluates whether the contents of the two sequences are equivalent to the given extent
template< ss_typename_param_k I1
, ss_typename_param_k I2
>
static ss_bool_t is_equal_(I1 p1, I2 p2, ss_size_t n)
{
for(; n-- > 0; ++p1, ++p2)
{
if(*p1 != *p2)
{
return false;
}
}
return true;
}
/// Evaluates whether the delimiter and the sequence are equivalent to the extent of the delimiter
template< ss_typename_param_k D1
, ss_typename_param_k I
>
static ss_bool_t is_equal_(D1 const& delim, I &p2)
{
return class_type::is_equal_(delim.begin(), p2, delim.length());
}
/// Returns the length of the delimiter
template <ss_typename_param_k D1>
static ss_size_t get_length_(D1 const& delim)
{
return delim.length();
}
#else /* ? STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT */
/// Evaluates whether the contents of the two sequences are equivalent to the given extent
static ss_bool_t is_equal_(string_type const& lhs, ss_typename_type_k string_type::value_type const* rhs)
{
{ for(ss_size_t i = 0, n = lhs.length(); i < n; ++i)
{
if(lhs[i] != rhs[i])
{
return false;
}
}}
return true;
}
/// Returns the length of the delimiter
static ss_size_t get_length_(string_type const& s)
{
return s.length();
}
#endif // STLSOFT_CF_MEMBER_TEMPLATE_FUNCTION_SUPPORT
/// Evaluates whether the delimiter and the sequence are equivalent to the extent of the delimiter
static ss_bool_t is_equal_(ss_char_a_t const delim, const_iterator &it)
{
return delim == *it;
}
/// Evaluates whether the delimiter and the sequence are equivalent to the extent of the delimiter
static ss_bool_t is_equal_(ss_char_w_t const delim, const_iterator &it)
{
return delim == *it;
}
/// Returns the length of the delimiter
static ss_size_t get_length_(ss_char_a_t const /* delim */)
{
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -