ATLAS Offline Software
Public Member Functions | Public Attributes | List of all members
StringParse Class Reference

Utility object for parsing a string into tokens and returning them as a variety of types. More...

#include <StringParse.h>

Inheritance diagram for StringParse:
Collaboration diagram for StringParse:

Public Member Functions

 StringParse (const std::string &input)
 Constructor, taking a string of whitespace-separated tokens. More...
 
template<typename T >
piece (size_t num) const
 Templated function to get the num'th token as any numeric type. More...
 
template<>
std::string piece (size_t num) const
 Function to get the num'th token as a string. More...
 

Public Attributes

elements
 STL member. More...
 

Detailed Description

Utility object for parsing a string into tokens and returning them as a variety of types.

This code is used to parse a string, tokenising space-separated parts into component 'pieces'.

Methods can then be called to

  1. Return the nth component as a string (piece method)
  2. Return the nth component as an integer (intpiece and longpiece methods) if conversion is possible (returns -1 if it is off the end and 0 if it cannot be converted)
  3. Return the nth component as a double (numpiece method) if conversion is possible (returns -1.1 if it is off the end and 0 if it cannot be converted)

Note that the 'num' index used to retrieve the pieces starts at 1, not the C-conventional 0.

Author
Ian Hinchliffe, April 2000
Yun-Ha.Shin, June 2006
Andy Buckley, September 2012
Andrii Verbytskyi, Jul 2023
Andrii Verbytskyi, May 2024

Definition at line 33 of file StringParse.h.

Constructor & Destructor Documentation

◆ StringParse()

StringParse::StringParse ( const std::string &  input)
inline

Constructor, taking a string of whitespace-separated tokens.

Definition at line 37 of file StringParse.h.

37  {
38  std::istringstream instring(input);
39  std::string token;
40  while (instring >> token) this->push_back(token);
41  }

Member Function Documentation

◆ piece() [1/2]

template<typename T >
T StringParse::piece ( size_t  num) const
inline

Templated function to get the num'th token as any numeric type.

https://en.cppreference.com/w/cpp/utility/from_chars

Definition at line 44 of file StringParse.h.

44  {
45  if (num > this->size()) return {-1};
46  std::string token = this->at(num-1);
47  T result{-1};
49  auto [ptr, ec] = std::from_chars(token.data(), token.data() + token.size(), result);
50  if (ec == std::errc() && ptr == token.data() + token.size()) return result;
51  if (ec == std::errc::invalid_argument || ptr != token.data() + token.size() ) return {-1}; //std::cout << "This number cannot be parsed.\n";
52  if (ec == std::errc::result_out_of_range) return {-1}; //std::cout << "This number is out of range of the return type.\n";
53  return {-1};
54  }

◆ piece() [2/2]

template<>
std::string StringParse::piece ( size_t  num) const

Function to get the num'th token as a string.

Definition at line 57 of file StringParse.h.

57  {
58  return num > this->size() ? std::string{} : this->at(num-1);
59 }

Member Data Documentation

◆ elements

T std::vector< T >::elements
inherited

STL member.


The documentation for this class was generated from the following file:
get_generator_info.result
result
Definition: get_generator_info.py:21
dbg::ptr
void * ptr(T *p)
Definition: SGImplSvc.cxx:74
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35