summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/LspCpp/third_party/uri/src/detail/algorithm.hpp
blob: 6db27e4870c214f8308c472175323cb107ac409c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2016 Glyn Matthews.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef NETWORK_DETAIL_RANGE_INC
#define NETWORK_DETAIL_RANGE_INC

#include <algorithm>
#include <iterator>
#include <utility>
#include <string>
#include <cctype>
#include <locale>

namespace network {
namespace detail {
template <class Rng, class Pred>
inline void for_each(Rng &rng, Pred &&pred) {
  std::for_each(std::begin(rng), std::end(rng), pred);
}

template <class Rng, class Iter, class Pred>
inline void transform(Rng &rng, Iter out, Pred &&pred) {
  std::transform(std::begin(rng), std::end(rng), out, pred);
}

template <class Rng>
inline typename Rng::difference_type distance(Rng &rng) {
  return std::distance(std::begin(rng), std::end(rng));
}

template <class Rng1, class Rng2>
inline bool equal(const Rng1 &rng1, const Rng2 &rng2) {
  return std::equal(std::begin(rng1), std::end(rng1), std::begin(rng2));
}

template <class Rng, class Pred>
inline void remove_erase_if(Rng &rng, Pred &&pred) {
  auto first = std::begin(rng), last = std::end(rng);
  auto it = std::remove_if(first, last, pred);
  rng.erase(it, last);
}

inline std::string trim_front(const std::string &str) {
  auto first = std::begin(str), last = std::end(str);
  auto it = std::find_if(
      first, last, [](char ch) { return !std::isspace(ch, std::locale()); });
  return std::string(it, last);
}

inline std::string trim_back(const std::string &str) {
  using reverse_iterator = std::reverse_iterator<std::string::const_iterator>;

  auto first = reverse_iterator(std::end(str)),
       last = reverse_iterator(std::begin(str));
  auto it = std::find_if(
      first, last, [](char ch) { return !std::isspace(ch, std::locale()); });
  std::string result(it, last);
  std::reverse(std::begin(result), std::end(result));
  return result;
}

inline std::string trim_copy(const std::string &str) {
  return trim_back(trim_front(str));
}
}  // namespace detail
}  // namespace network

#endif  // NETWORK_DETAIL_RANGE_INC