Project
Loading...
Searching...
No Matches
RangeTokenizer.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11#ifndef RANGE_TOKENIZER_H
12#define RANGE_TOKENIZER_H
13
18
19#include <vector>
20#include <string>
21#include <sstream>
22#include <utility> // std::move
23#include <functional> // std::function
24
25namespace o2
26{
27
63 template <typename T>
64 static std::vector<T> tokenize(
65 std::string input, std::function<T(std::string const&)> convert = [](std::string const& token) {T value; std::istringstream(token) >> value; return value; })
66 {
67 std::istringstream stream(input);
68 std::string token;
69 std::vector<T> res;
70 while (std::getline(stream, token, ',')) {
71 if (std::is_integral<T>::value && token.find('-') != token.npos) {
72 // extract range
73 if constexpr (std::is_integral<T>::value) { // c++17 compile time
74 insertRange(res, token, convert);
75 }
76 } else {
77 res.emplace_back(convert(token));
78 }
79 }
80 return std::move(res);
81 }
82
84 template <typename T, typename std::enable_if_t<std::is_integral<T>::value == true, int> = 0>
85 static void insertRange(std::vector<T>& res, std::string token, std::function<T(std::string const&)> convert)
86 {
87 std::istringstream tokenstream(token);
88 std::string bound;
89 T lowerBound, upperBound;
90 if (std::getline(tokenstream, bound, '-')) {
91 lowerBound = convert(bound);
92 if (std::getline(tokenstream, bound, '-')) {
93 upperBound = convert(bound);
94 for (T index = lowerBound; index <= upperBound; index++) {
95 res.emplace_back(index);
96 }
97 }
98 }
99 }
100};
101}; // namespace o2
102
103#endif
uint32_t res
Definition RawData.h:0
GLuint index
Definition glcorearb.h:781
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLuint GLuint stream
Definition glcorearb.h:1806
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Tokenize a string according to delimiter ',' and extract values of type T.
static void insertRange(std::vector< T > &res, std::string token, std::function< T(std::string const &)> convert)
extract a range of an integral type from a token string and add to vector
static std::vector< T > tokenize(std::string input, std::function< T(std::string const &)> convert=[](std::string const &token) {T value;std::istringstream(token) > > value;return value;})
std::vector< uint64_t > convert(gsl::span< const uint64_t > page)