String.cc
Go to the documentation of this file.
1 #include <boost/tokenizer.hpp>
2 #include <utl/String.h>
3 #include <utl/ErrorLogger.h>
4 
5 using namespace std;
6 
7 
8 namespace utl {
9 
10  namespace String {
11 
12  vector<string>
13  Split(const string& str, const char* const separators)
14  {
15  typedef boost::tokenizer<boost::char_separator<char>> Tok;
16  const Tok tok(str, boost::char_separator<char>(separators));
17  vector<string> vec;
18  copy(tok.begin(), tok.end(), back_insert_iterator<vector<string>>(vec));
19  return vec;
20  }
21 
22 
32  string
33  OfAlreadySortedIds(const vector<int>& ids)
34  {
35  auto it = ids.cbegin();
36  const auto end = ids.cend();
37  if (it == end)
38  return "";
39  int first = *it;
40  int last = 0;
41  int prev = first;
42  ostringstream oss;
43  while (++it != end) {
44  const int curr = *it;
45  if (prev+1 == curr)
46  last = curr;
47  else {
48  oss << first;
49  if (last)
50  oss << (first+1 == last ? ' ' : '-') << last;
51  oss << ' ';
52  first = curr;
53  last = 0;
54  }
55  prev = curr;
56  }
57  oss << first;
58  if (last)
59  oss << (first+1 == last ? ' ' : '-') << last;
60  return oss.str();
61  }
62 
63 
64  string
65  OfSortedIds(vector<int> ids)
66  {
67  sort(ids.begin(), ids.end());
68  return OfAlreadySortedIds(ids);
69  }
70 
71 
72  string
73  OfSortedIds(const set<int>& ids)
74  {
75  return OfAlreadySortedIds(vector<int>(ids.begin(), ids.end()));
76  }
77 
78  }
79 
80 }
string OfAlreadySortedIds(const vector< int > &ids)
Definition: String.cc:33
string OfSortedIds(vector< int > ids)
Definition: String.cc:65
vector< string > Split(const string &str, const char *const separators)
Definition: String.cc:13

, generated on Tue Sep 26 2023.