21#ifndef G_STRING_TOKEN_H
22#define G_STRING_TOKEN_H
33 template <
typename T>
class StringTokenT ;
34 using StringToken = StringTokenT<std::string> ;
35 using StringTokenView = StringTokenT<std::string_view> ;
56 using char_type =
typename T::value_type ;
58 StringTokenT(
const T & s ,
const char_type * ws , std::size_t wsn ) noexcept ;
68 StringTokenT(
const T & s , std::string_view ws ) noexcept ;
72 bool valid()
const noexcept ;
75 explicit operator bool()
const noexcept ;
78 const char_type *
data()
const noexcept ;
81 std::size_t
size()
const noexcept ;
84 std::size_t
pos()
const noexcept ;
87 T
operator()()
const noexcept(std::is_same<T,std::string_view>::value) ;
99 StringTokenT( T && s ,
const char_type * , std::size_t ) = delete ;
107 static constexpr std::size_t npos = T::npos ;
109 const char_type * m_ws ;
112 std::size_t m_endpos ;
117 namespace StringTokenImp
119 template <
typename T>
inline T substr(
const T & s ,
120 std::size_t pos , std::size_t len )
noexcept
122 try {
return s.substr( pos , len ) ; }
catch(...) {
return {} ; }
124 template <>
inline std::string_view substr<std::string_view>(
const std::string_view & s ,
125 std::size_t pos , std::size_t len )
noexcept
127 return sv_substr_noexcept( s , pos , len ) ;
137 m_pos(s.empty()?npos:s.find_first_not_of(m_ws,0U,m_wsn)) ,
138 m_endpos(s.find_first_of(m_ws,m_pos,m_wsn))
140 G_ASSERT( !(s.empty()) || ( m_pos == npos && m_endpos == npos ) ) ;
141 G_ASSERT( !(!s.empty() && wsn==0U) || ( m_pos == 0U && m_endpos == npos ) ) ;
142 G_ASSERT( !(m_pos == npos) || m_endpos == npos ) ;
150 m_pos(s.empty()?npos:s.find_first_not_of(m_ws,0U,m_wsn)) ,
151 m_endpos(s.find_first_of(m_ws,m_pos,m_wsn))
159 return m_s.data() + (m_pos==npos?0U:m_pos) ;
165 return (m_endpos==npos?m_s.size():m_endpos) - m_pos ;
171 return m_pos == npos ? 0U : m_pos ;
177 return m_pos != npos ;
183 return m_pos != npos ;
189 using string_type = T ;
190 return m_pos == npos ? string_type{} : StringTokenImp::substr( m_s , m_pos , size() ) ;
196 m_pos = m_s.find_first_not_of( m_ws , m_endpos , m_wsn ) ;
197 m_endpos = m_s.find_first_of( m_ws , m_pos , m_wsn ) ;
198 G_ASSERT( !(m_s.empty()) || ( m_pos == npos && m_endpos == npos ) ) ;
199 G_ASSERT( !(!m_s.empty() && m_wsn==0U) || ( m_pos == npos && m_endpos == npos ) ) ;
200 G_ASSERT( !(m_pos == npos) || m_endpos == npos ) ;
A zero-copy string token iterator where the token separators are runs of whitespace characters,...
StringTokenT< T > & next() noexcept
Moves to the next token.
T operator()() const noexcept(std::is_same< T, std::string_view >::value)
Returns the current token substring or T() if not valid().
const char_type * data() const noexcept
Returns the current token pointer.
bool valid() const noexcept
Returns true if a valid token position.
std::size_t size() const noexcept
Returns the current token size.
std::size_t pos() const noexcept
Returns the offset of data().
StringTokenT< T > & operator++() noexcept
Moves to the next token.
StringTokenT(const T &s, const char_type *ws, std::size_t wsn) noexcept
Constructor.
A class like c++17's std::string_view.