summary refs log tree commit diff stats
path: root/src/util.h
blob: 1eb230369917b06d1e4e6ed3ab634c1c2055eede (plain) (blame)
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
#ifndef UTIL_H_E9110D4C
#define UTIL_H_E9110D4C

#include <string>
#include <iterator>

template <typename Container, typename Predicate>
void erase_if(Container& items, const Predicate& predicate)
{
  for (auto it = std::begin(items); it != std::end(items);)
  {
    if (predicate(*it))
    {
      it = items.erase(it);
    }
    else
    {
      ++it;
    }
  }
};

template <class OutputIterator>
void splitStr(
  std::string input,
  std::string delimiter,
  OutputIterator out) {
  while (!input.empty()) {
    int divider = input.find(delimiter);
    if (divider == std::string::npos) {
      *out = input;
      out++;

      input = "";
    } else {
      *out = input.substr(0, divider);
      out++;

      input = input.substr(divider+delimiter.length());
    }
  }
}

template <class Container>
Container splitStr(
  std::string input,
  std::string delimiter) {
  Container result;

  splitStr(input, delimiter, std::back_inserter(result));

  return result;
}

#endif /* end of include guard: UTIL_H_E9110D4C */