| 1 |
// Copyright 2016 Glyn Matthews. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. |
| 3 |
// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
#ifndef NETWORK_DETAIL_RANGE_INC |
| 7 |
#define NETWORK_DETAIL_RANGE_INC |
| 8 |
|
| 9 |
#include <algorithm> |
| 10 |
#include <iterator> |
| 11 |
#include <utility> |
| 12 |
#include <string> |
| 13 |
#include <cctype> |
| 14 |
#include <locale> |
| 15 |
|
| 16 |
namespace network { |
| 17 |
namespace detail { |
| 18 |
template <class Rng, class Pred> |
| 19 |
inline void for_each(Rng &rng, Pred &&pred) { |
| 20 |
std::for_each(std::begin(rng), std::end(rng), pred); |
| 21 |
} |
| 22 |
|
| 23 |
template <class Rng, class Iter, class Pred> |
| 24 |
inline void transform(Rng &rng, Iter out, Pred &&pred) { |
| 25 |
std::transform(std::begin(rng), std::end(rng), out, pred); |
| 26 |
} |
| 27 |
|
| 28 |
template <class Rng> |
| 29 |
inline typename Rng::difference_type distance(Rng &rng) { |
| 30 |
return std::distance(std::begin(rng), std::end(rng)); |
| 31 |
} |
| 32 |
|
| 33 |
template <class Rng1, class Rng2> |
| 34 |
inline bool equal(const Rng1 &rng1, const Rng2 &rng2) { |
| 35 |
return std::equal(std::begin(rng1), std::end(rng1), std::begin(rng2)); |
| 36 |
} |
| 37 |
|
| 38 |
template <class Rng, class Pred> |
| 39 |
inline void remove_erase_if(Rng &rng, Pred &&pred) { |
| 40 |
auto first = std::begin(rng), last = std::end(rng); |
| 41 |
auto it = std::remove_if(first, last, pred); |
| 42 |
rng.erase(it, last); |
| 43 |
} |
| 44 |
|
| 45 |
inline std::string trim_front(const std::string &str) { |
| 46 |
auto first = std::begin(str), last = std::end(str); |
| 47 |
auto it = std::find_if( |
| 48 |
first, last, [](char ch) { return !std::isspace(ch, std::locale()); }); |
| 49 |
return std::string(it, last); |
| 50 |
} |
| 51 |
|
| 52 |
inline std::string trim_back(const std::string &str) { |
| 53 |
using reverse_iterator = std::reverse_iterator<std::string::const_iterator>; |
| 54 |
|
| 55 |
auto first = reverse_iterator(std::end(str)), |
| 56 |
last = reverse_iterator(std::begin(str)); |
| 57 |
auto it = std::find_if( |
| 58 |
first, last, [](char ch) { return !std::isspace(ch, std::locale()); }); |
| 59 |
std::string result(it, last); |
| 60 |
std::reverse(std::begin(result), std::end(result)); |
| 61 |
return result; |
| 62 |
} |
| 63 |
|
| 64 |
inline std::string trim_copy(const std::string &str) { |
| 65 |
return trim_back(trim_front(str)); |
| 66 |
} |
| 67 |
} // namespace detail |
| 68 |
} // namespace network |
| 69 |
|
| 70 |
#endif // NETWORK_DETAIL_RANGE_INC |