📄 string_tokeniser.hpp
字号:
typedef traits_type traits_type;
#else /* ? compiler */
/// The delimiter type
typedef ss_typename_type_k tokeniser_type::delimiter_type delimiter_type;
/// The value type
typedef ss_typename_type_k tokeniser_type::value_type value_type;
/// The traits type
typedef ss_typename_type_k tokeniser_type::traits_type traits_type;
#endif /* compiler */
typedef value_type effective_const_reference;
private:
typedef ss_typename_type_k traits_type::const_iterator_type underlying_iterator_type;
# if defined(STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION)
typedef delimiter_type const *delimiter_ref_type;
# else /* ? STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
typedef delimiter_type delimiter_ref_type;
# endif /* STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
/// @}
/// \name Construction
/// @{
private:
friend class string_tokeniser<S, D, B, V, T, P>;
/// Conversion constructor
const_iterator(underlying_iterator_type first, underlying_iterator_type last, delimiter_type const& delimiter)
: m_find0(first)
, m_find1(first)
, m_next(first)
, m_end(last)
# if defined(STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION)
, m_delimiter(&delimiter)
# else /* ? STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
, m_delimiter(delimiter)
# endif /* STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
, m_cchDelimiter(comparator_type::length(delimiter))
{
if(m_end != m_find0)
{
increment_();
}
}
public:
/// Default constructor
const_iterator()
: m_find0(NULL)
, m_find1(NULL)
, m_next(NULL)
, m_end(NULL)
, m_delimiter(delimiter_ref_type())
, m_cchDelimiter(0)
{}
/// Copy constructor
///
/// \param rhs The iterator whose current search position will be copied
const_iterator(class_type const& rhs)
: m_find0(rhs.m_find0)
, m_find1(rhs.m_find1)
, m_next(rhs.m_next)
, m_end(rhs.m_end)
, m_delimiter(rhs.m_delimiter)
, m_cchDelimiter(comparator_type::length(get_delim_ref_(rhs.m_delimiter)))
{}
/// Copy-assignment operator
///
/// \param rhs The iterator whose current search position will be copied
class_type const& operator =(class_type const& rhs)
{
m_find0 = rhs.m_find0;
m_find1 = rhs.m_find1;
m_next = rhs.m_next;
m_end = rhs.m_end;
m_delimiter = rhs.m_delimiter;
m_cchDelimiter = rhs.m_cchDelimiter;
return *this;
}
/// @}
/// \name Forward Iterator Methods
/// @{
public:
/// Dereference operator
//
// This has to be V, rather than value_type, because Visual C++ thinks that S is the value_type!!
V operator *() const
{
return traits_type::create(m_find0, m_find1);
}
/// Pre-increment operator
class_type& operator ++()
{
increment_();
return *this;
}
/// Post-increment operator
const class_type operator ++(int)
{
class_type ret(*this);
operator ++();
return ret;
}
/// Evaluates whether \c this and \c rhs are equivalent
ss_bool_t equal(class_type const& rhs) const
{
STLSOFT_MESSAGE_ASSERT("Comparing iterators from different tokenisers", m_end == rhs.m_end);
return m_find0 == rhs.m_find0;
}
/// Evaluates whether \c this and \c rhs are equivalent
ss_bool_t operator == (class_type const& rhs) const
{
return equal(rhs);
}
/// Evaluates whether \c this and \c rhs are not equivalent
ss_bool_t operator != (class_type const& rhs) const
{
return !equal(rhs);
}
/// @}
/// \name Implementation
/// @{
private:
static delimiter_type const& get_delim_ref_(delimiter_ref_type const& delim)
{
# if defined(STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION)
return *delim;
# else /* ? STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
return delim;
# endif /* STLSOFT_STRING_TOKENISER_CF_REQUIRE_DELIMITER_INDIRECTION */
}
void increment_()
{
STLSOFT_MESSAGE_ASSERT("Attempting to increment an invalid iterator", m_find0 != m_end);
// This is a two-phase algorithm:
//
// 1. If skipping blanks, then do that. Otherwise, locate the the start-of-item
// iterator (m_find0) to the previously identified start of the next item (m_next)
// 2. Starting from m_find0, determine the end-of-item (m_find1)
// TODO: Make this into a overload-selector, to avoid the "conditional expression is constant" warning
if(blanks_policy_type::value)
{
// 1. Skip blanks until at start of next item
for(m_find0 = m_next; m_find0 != m_end; )
{
if(comparator_type::not_equal(get_delim_ref_(m_delimiter), m_find0))
{
break;
}
else
{
m_find0 += static_cast<ss_ptrdiff_t>(m_cchDelimiter);
}
}
}
else
{
m_find0 = m_next;
}
// 2. Determine the end-of-item (m_find1), starting from m_find0
for(m_find1 = m_find0; ; )
{
if(m_find1 == m_end)
{
// End of sequence. Item will be [m_find0, m_end (== m_find1))
m_next = m_find1;
break;
}
else if(comparator_type::not_equal(get_delim_ref_(m_delimiter), m_find1))
{
// current item does not hold a delimiter, so advance one position
++m_find1;
}
else
{
// Determine the start of the next potential element, ready
// for the next call of increment_()
m_next = m_find1 + static_cast<ss_ptrdiff_t>(m_cchDelimiter);
break;
}
}
}
/// @}
/// \name Members
/// @{
private:
underlying_iterator_type m_find0; // the start of the current item
underlying_iterator_type m_find1; // the end of the current item
underlying_iterator_type m_next; // the start of the next valid (non-null) item
underlying_iterator_type m_end; // end point of controlled sequence
delimiter_ref_type m_delimiter; // The delimiter
ss_size_t m_cchDelimiter;
/// @}
};
/// Begins the iteration
///
/// \return An iterator representing the start of the sequence
const_iterator begin() const
{
STLSOFT_ASSERT(is_valid());
return const_iterator(traits_type::begin(m_str), traits_type::end(m_str), m_delimiter);
}
/// Ends the iteration
///
/// \return An iterator representing the end of the sequence
const_iterator end() const
{
STLSOFT_ASSERT(is_valid());
return const_iterator(traits_type::end(m_str), traits_type::end(m_str), m_delimiter);
}
/// @}
/// \name Attributes
/// @{
public:
/// Indicates whether the search sequence is empty
ss_bool_t empty() const
{
STLSOFT_ASSERT(is_valid());
return begin() == end();
}
/// @}
/// \name Invariant
/// @{
private:
ss_bool_t is_valid() const
{
return true;
}
/// @}
/// \name Members
/// @{
private:
string_type const m_str;
delimiter_type const m_delimiter;
/// @}
/// \name Not to be implemented
/// @{
private:
class_type const& operator =(class_type const&);
/// @}
};
/* /////////////////////////////////////////////////////////////////////////
* Operators
*/
#if 0
/** \brief Evaluates whether \c this and \c rhs are equivalent
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k D
, ss_typename_param_k B
, ss_typename_param_k V
, ss_typename_param_k T
, ss_typename_param_k P
>
inline ss_bool_t operator ==( ss_typename_type_k string_tokeniser<S, D, B, V, T, P>::const_iterator const& lhs
, ss_typename_type_k string_tokeniser<S, D, B, V, T, P>::const_iterator const& rhs)
{
return lhs.equal(rhs);
}
/** \brief Evaluates whether \c this and \c rhs are not equivalent
*
* \ingroup group__library__string
*/
template< ss_typename_param_k S
, ss_typename_param_k D
, ss_typename_param_k B
, ss_typename_param_k V
, ss_typename_param_k T
, ss_typename_param_k P
>
inline ss_bool_t operator !=( ss_typename_type_k string_tokeniser<S, D, B, V, T, P>::const_iterator const& lhs
, ss_typename_type_k string_tokeniser<S, D, B, V, T, P>::const_iterator const& rhs)
{
return !lhs.equal(rhs);
}
#endif /* 0 */
/* /////////////////////////////////////////////////////////////////////////
* Unit-testing
*/
#ifdef STLSOFT_UNITTEST
# include "./unittest/string_tokeniser_unittest_.h"
#endif /* STLSOFT_UNITTEST */
/* ////////////////////////////////////////////////////////////////////// */
#if defined(STLSOFT_COMPILER_IS_DMC) && \
!defined(_STLPORT_VERSION)
template< ss_typename_param_k S
, ss_typename_param_k D
, ss_typename_param_k B
, ss_typename_param_k V
, ss_typename_param_k T
, ss_typename_param_k P
>
inline forward_iterator_tag iterator_category(string_tokeniser<S, D, B, V, T, P>::const_iterator const&)
{
return forward_iterator_tag();
}
template< ss_typename_param_k S
, ss_typename_param_k D
, ss_typename_param_k B
, ss_typename_param_k V
, ss_typename_param_k T
, ss_typename_param_k P
>
inline ss_ptrdiff_t* distance_type(string_tokeniser<S, D, B, V, T, P>::const_iterator const&)
{
return static_cast<ss_ptrdiff_t*>(0);
}
#endif /* compiler */
/* ////////////////////////////////////////////////////////////////////// */
#ifndef _STLSOFT_NO_NAMESPACE
} // namespace stlsoft
#endif /* _STLSOFT_NO_NAMESPACE */
/* ////////////////////////////////////////////////////////////////////// */
#endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TOKENISER */
/* ////////////////////////////////////////////////////////////////////// */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -