TF++, Miscellaneous classes and modules in C++:

◆ rangelist()

template<class T >
tfxx::RangeList<T> tfxx::string::rangelist ( const std::string &  s)

read rangelist from string

Reads a comma separated list of range strings an passes tham to the range reading function to build a list of ranges.

The input lists in the string are ranges separated by comma: 4-7,9,13-26

A step size can be specified using the '+' symbol. '5+3-12' means: take every third staring at 5 and ending at 12

Definition at line 98 of file rangestring.h.

References gen_split(), and TFXX_assert.

99  {
100  typedef tfxx::Range<T> Trange;
101  typedef tfxx::RangeList<T> Tlist;
102  Tlist retval;
103  std::list<std::string> slist;
104  tfxx::string::gen_split(slist, s, ",", true);
105  std::list<std::string>::const_iterator i=slist.begin();
106  while (i!=slist.end())
107  {
108  std::vector<std::string> svec;
109  tfxx::string::gen_split(svec, *i, "+", true);
110  if (svec.size()>1)
111  {
112  std::vector<std::string> svec2;
113  tfxx::string::gen_split(svec2, svec[1], "-", true);
114  TFXX_assert(svec2.size()>1, "range not properly defined");
115  typename Trange::Tvalue start(0), step(0), end(0);
116  { std::istringstream iss(svec[0]); iss >> start; }
117  { std::istringstream iss(svec2[0]); iss >> step; }
118  { std::istringstream iss(svec2[1]); iss >> end; }
119  /*
120  std::cerr << "start=" << svec[0]
121  << " step=" << svec2[0]
122  << " end=" << svec2[1] << std::endl;
123  std::cerr << "start=" << start
124  << " step=" << step
125  << " end=" << end << std::endl;
126  */
127  TFXX_assert(step>0, "range step size must be positive");
128  for (typename Trange::Tvalue j=start; j<=end; j=j+step)
129  {
130  retval.append(Trange(j,j));
131  }
132  }
133  else
134  {
135  // no '+' symbol, go ahead with simple range
136  retval.append(range<typename Tlist::Tvalue>(*i));
137  }
138  ++i;
139  }
140  return retval;
141  } // tfxx::RangeList<T> rangelist(const std::string& s)
#define TFXX_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: error.h:175
A class to deal with numerical ranges.
Definition: range.h:50
void gen_split(C &v, const std::string &s, const std::string &delimiter=" ", const bool &dropdelimiter=false)
Definition: stringfunc.h:112
Here is the call graph for this function: