Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename InputIterator, typename OutputIterator>
- auto to_slug(InputIterator first, InputIterator last, OutputIterator out, std::locale const& locale)
- -> std::pair<OutputIterator, bool>
- {
- using char_t = decltype(*first);
- constexpr auto hyphen = char_t {'-'};
- auto const next = [&](InputIterator it) {
- return std::find_if(it, last, [&](char_t ch) {
- return std::isalnum(ch, locale);
- });
- };
- auto it = next(first);
- if (it == last) {
- return std::make_pair(out, false);
- }
- bool const is_digit = std::isdigit(*it, locale);
- out++ = std::tolower(*it++, locale);
- while (it != last) {
- if (std::isalnum(*it, locale)) {
- out++ = std::tolower(*it++, locale);
- } else if ((it = next(++it)) != last) {
- out++ = hyphen;
- }
- }
- return std::make_pair(out, !is_digit);
- }
- template <typename InputIterator, typename OutputIterator>
- auto to_slug(InputIterator first, InputIterator last, OutputIterator out) {
- return to_slug(first, last, out, std::locale::classic());
- }
- void main() {
- to_slug(std::istream_iterator<char>(std::cin)
- , std::istream_iterator<char>()
- , std::ostream_iterator<char>(std::cout));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement