libcruft-util/string.hpp

115 lines
3.1 KiB
C++
Raw Normal View History

2011-08-12 00:25:59 +10:00
/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-08-12 00:25:59 +10:00
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-08-12 00:25:59 +10:00
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2011-08-12 00:25:59 +10:00
*
2016-03-17 18:13:19 +11:00
* Copyright 2011-2016 Danny Robson <danny@nerdcruft.net>
2011-08-12 00:25:59 +10:00
*/
#ifndef __UTIL_STRING_HPP
#define __UTIL_STRING_HPP
#include "view.hpp"
2016-03-17 18:13:19 +11:00
namespace util {
std::string to_utf8 (const wchar_t*);
std::string to_utf8 (const std::wstring&);
}
2011-08-12 00:25:59 +10:00
bool
strbegins(const char *restrict str,
const char *restrict prefix);
2016-03-17 18:13:19 +11:00
namespace util {
2016-03-18 11:08:12 +11:00
template <typename Iterator>
2016-03-17 18:13:19 +11:00
struct tokeniser {
public:
2016-03-18 11:08:12 +11:00
using value_type = typename std::iterator_traits<Iterator>::value_type;
using range_type = view<Iterator>;
2016-03-17 18:13:19 +11:00
2016-03-18 11:08:12 +11:00
tokeniser (Iterator first, Iterator last, value_type separator);
2016-03-17 18:13:19 +11:00
2017-09-15 15:22:29 +10:00
template <typename ContainerT>
tokeniser (ContainerT &container, typename ContainerT::value_type _separator):
tokeniser (
std::begin (container),
std::end (container),
_separator
)
{ ; }
struct iterator : public std::iterator<
std::forward_iterator_tag,
range_type,
std::size_t
> {
2016-03-17 18:13:19 +11:00
public:
iterator operator++ (int);
2016-03-18 11:08:12 +11:00
iterator& operator++ (void)&;
2016-03-17 18:13:19 +11:00
range_type operator* (void) const;
bool operator== (const iterator&) const;
2016-03-18 11:08:12 +11:00
bool operator!= (const iterator&) const;
2016-03-17 18:13:19 +11:00
private:
2016-03-18 11:08:12 +11:00
iterator (range_type range, value_type separator);
const value_type m_separator;
2016-03-17 18:13:19 +11:00
range_type m_range;
2016-03-18 11:08:12 +11:00
Iterator m_end;
friend tokeniser;
2016-03-17 18:13:19 +11:00
};
iterator cbegin (void) const;
iterator begin (void) const;
iterator cend (void) const;
iterator end (void) const;
private:
2016-03-18 11:08:12 +11:00
const range_type m_range;
2016-03-17 18:13:19 +11:00
const value_type m_separator;
};
2016-03-18 11:08:12 +11:00
2017-09-15 15:22:29 +10:00
///////////////////////////////////////////////////////////////////////////
template <typename CharT, std::size_t LengthV>
auto
make_tokeniser (CharT (&data)[LengthV], CharT separator)
{
return tokeniser { std::begin (data), std::end (data), separator };
}
//-------------------------------------------------------------------------
tokeniser<std::string::const_iterator>
make_tokeniser (const std::string&, std::string::value_type);
//-------------------------------------------------------------------------
tokeniser<std::string::const_iterator>
make_tokeniser (std::string&&, std::string::value_type) = delete;
//-------------------------------------------------------------------------
tokeniser<const char*>
make_tokeniser (const char*, char);
2016-03-18 11:08:12 +11:00
2016-03-17 18:13:19 +11:00
}
2011-08-12 00:25:59 +10:00
#endif // __UTIL_STRING_HPP