musrfit 1.10.0
PStringUtils Class Reference

Lightweight, dependency-free string utilities (pure C++17). More...

#include <PStringUtils.h>

Static Public Member Functions

static std::vector< std::string > Split (const std::string &str, const std::string &delimiters)
 
static bool IsInt (const std::string &str)
 
static bool IsFloat (const std::string &str)
 
static int ToInt (const std::string &str, bool *ok=nullptr)
 
static double ToDouble (const std::string &str, bool *ok=nullptr)
 
static bool IsEqualNoCase (const std::string &a, const std::string &b)
 
static bool ContainsNoCase (const std::string &haystack, const std::string &needle)
 
static bool BeginsWithNoCase (const std::string &str, const std::string &prefix)
 

Detailed Description

Lightweight, dependency-free string utilities (pure C++17).

PStringUtils collects small string helpers used throughout the musrfit suite, in particular for tokenizing and parsing the plain-text MSR file format. The implementation deliberately relies only on the C++ standard library (no ROOT) so that it can be reused freely.

The provided helpers replicate the semantics of the corresponding ROOT TString methods that were previously used:

  • Split replaces TString::Tokenize() (+ TObjArray/TObjString)
  • IsInt replaces TString::IsDigit()
  • IsFloat replaces TString::IsFloat()
  • ToInt replaces TString::Atoi()
  • ToDouble replaces TString::Atof()
  • IsEqualNoCase replaces TString::CompareTo(..., TString::kIgnoreCase)

All methods are static; the class is a pure namespace-like utility.

Definition at line 56 of file PStringUtils.h.

Member Function Documentation

◆ BeginsWithNoCase()

bool PStringUtils::BeginsWithNoCase ( const std::string & str,
const std::string & prefix )
static

Case-insensitive prefix test. Mirrors TString::BeginsWith(..., TString::kIgnoreCase).

Parameters
strstring to be tested
prefixprefix to be searched for
Returns
true if str starts with prefix ignoring case

Case-insensitive prefix test, mirroring TString::BeginsWith(..., TString::kIgnoreCase).

Parameters
strstring to be tested
prefixprefix to be searched for
Returns
true if str starts with prefix ignoring case

Definition at line 265 of file PStringUtils.cpp.

Referenced by PMsrHandler::HandlePlotEntry().

◆ ContainsNoCase()

bool PStringUtils::ContainsNoCase ( const std::string & haystack,
const std::string & needle )
static

Case-insensitive substring search. Mirrors TString::Contains(..., TString::kIgnoreCase).

Parameters
haystackstring to be searched in
needlesubstring to be searched for
Returns
true if needle is contained in haystack ignoring case

Case-insensitive substring search, mirroring TString::Contains(..., TString::kIgnoreCase).

Parameters
haystackstring to be searched in
needlesubstring to be searched for
Returns
true if needle is contained in haystack ignoring case

Definition at line 235 of file PStringUtils.cpp.

Referenced by PMsrHandler::CheckFuncs(), PMsrHandler::CheckMaps(), and PMsrHandler::HandlePlotEntry().

◆ IsEqualNoCase()

bool PStringUtils::IsEqualNoCase ( const std::string & a,
const std::string & b )
static

Case-insensitive full-string equality. Mirrors TString::CompareTo(..., TString::kIgnoreCase) == 0.

Parameters
afirst string
bsecond string
Returns
true if a and b are equal ignoring case

Case-insensitive full-string equality, mirroring TString::CompareTo(..., TString::kIgnoreCase) == 0.

Parameters
afirst string
bsecond string
Returns
true if a and b are equal ignoring case

Definition at line 212 of file PStringUtils.cpp.

Referenced by PMsrHandler::HandleFitParameterEntry(), PMsrHandler::HandleFourierEntry(), PMsrHandler::HandleGlobalEntry(), PMsrHandler::HandleRunEntry(), PMusrCanvas::IsScaleN0AndBkg(), and PRunSingleHisto::IsScaleN0AndBkg().

◆ IsFloat()

bool PStringUtils::IsFloat ( const std::string & str)
static

Returns true if the string is a complete integer or floating point literal (optionally signed, with decimal point and/or exponent). Mirrors TString::IsFloat() for the relevant cases.

Parameters
strstring to be checked
Returns
true if str is a valid number

Returns true if the string is a complete integer or floating point literal (optionally signed, with decimal point and/or exponent). Mirrors the semantics of TString::IsFloat() for the relevant cases.

Parameters
strstring to be checked
Returns
true if str is a valid number

Definition at line 111 of file PStringUtils.cpp.

Referenced by PMsrHandler::HandleGlobalEntry(), and PMsrHandler::HandleRunEntry().

◆ IsInt()

bool PStringUtils::IsInt ( const std::string & str)
static

Returns true if the string is a (possibly signed) integer literal, i.e. a non-empty sequence of decimal digits with an optional single leading sign (+/-). Slightly more permissive than TString::IsDigit(), which rejects a sign, so that e.g. "-5" is also recognised.

Parameters
strstring to be checked
Returns
true if str is a (possibly signed) integer

Returns true if the string is an integer literal, i.e. a non-empty sequence of decimal digits with an optional single leading sign (+/-). This is slightly more permissive than TString::IsDigit(), which rejects a sign, so that negative/positive integers such as "-5" or "+42" are also recognised.

Parameters
strstring to be checked
Returns
true if str is a (possibly signed) integer

Definition at line 77 of file PStringUtils.cpp.

Referenced by PMsrHandler::FillParameterInUse(), PMsrHandler::GetNoOfFitParameters(), PMsrHandler::HandleRunEntry(), PStartupHandler::OnCharacters(), and PMsrHandler::WriteMsrLogFile().

◆ Split()

std::vector< std::string > PStringUtils::Split ( const std::string & str,
const std::string & delimiters )
static

Splits a string into tokens on any character contained in delimiters, skipping empty tokens. Mirrors TString::Tokenize().

Parameters
strinput string to be tokenized
delimitersset of delimiter characters
Returns
vector of tokens (without the delimiters)

Splits a string into tokens on any character contained in delimiters, skipping empty tokens. Mirrors the semantics of TString::Tokenize().

Parameters
strinput string to be tokenized
delimitersset of delimiter characters
Returns
vector of tokens (without the delimiters)

Definition at line 48 of file PStringUtils.cpp.

Referenced by PFitter::CheckCommands(), PMsrHandler::CheckFuncs(), PMsrHandler::CheckMaps(), PFitter::ExecuteFitRange(), PFitter::ExecuteFix(), PFitter::ExecutePrintLevel(), PFitter::ExecuteRelease(), PRunDataHandler::FileExistsCheck(), PRunDataHandler::FileExistsCheck(), PRunDataHandler::FileExistsCheck(), PRunDataHandler::FileNameFromTemplate(), PMsrHandler::FillParameterInUse(), PMsrHandler::GetNoOfFitParameters(), PFitter::GetParFromFun(), PFitter::GetPhaseParams(), PMsrHandler::HandleFitParameterEntry(), PMsrHandler::HandleFourierEntry(), PMsrHandler::HandleGlobalEntry(), PMsrHandler::HandlePlotEntry(), PMsrHandler::HandleRunEntry(), PMusrCanvas::IsScaleN0AndBkg(), PRunSingleHisto::IsScaleN0AndBkg(), PTheory::MakeCleanAndTidyPolynom(), PTheory::MakeCleanAndTidyTheoryBlock(), PTheory::MakeCleanAndTidyUserFcn(), msr2msr_param(), msr2msr_run(), msr2msr_theory(), musrFT_filter_histo(), PStartupHandler::OnCharacters(), PMsrHandler::ParseFourierPhaseParIterVector(), PMsrHandler::ParseFourierPhaseParVector(), PMsrHandler::ParseFourierPhaseValueVector(), PTheory::PTheory(), PRunDataHandler::ReadAsciiFile(), PRunDataHandler::ReadDatFile(), PRunDataHandler::ReadDBFile(), PRunDataHandler::ReadMduAsciiFile(), PRunDataHandler::ReadRootFile(), PRunDataHandler::ReadWkmFile(), PRunAsymmetry::SetFitRangeBin(), PRunAsymmetryBNMR::SetFitRangeBin(), PRunAsymmetryRRF::SetFitRangeBin(), PRunMuMinus::SetFitRangeBin(), PRunSingleHisto::SetFitRangeBin(), PRunSingleHistoRRF::SetFitRangeBin(), and PMsrHandler::WriteMsrLogFile().

◆ ToDouble()

double PStringUtils::ToDouble ( const std::string & str,
bool * ok = nullptr )
static

Converts the leading part of the string to a double. Mirrors TString::Atof(). Returns 0.0 if no conversion is possible.

If ok is non-null it is set to true when a value was parsed and to false on error (no number, or value out of range), allowing callers to distinguish a legitimate 0.0 from a failed conversion.

Parameters
strstring to be converted
okoptional out-parameter signalling conversion success
Returns
converted double value (0.0 on error)

Converts the leading part of the string to a double, mirroring TString::Atof(). Returns 0.0 if no conversion is possible.

Like ToInt(), conversion errors can be reported through the optional ok out-parameter: it is set to true when a value was parsed and to false otherwise (no number present, or the value is out of range). This lets callers distinguish a legitimate 0.0 from a failed conversion. strtod() (rather than std::from_chars) is used so that the accepted input set stays identical to TString::Atof() (leading whitespace is skipped, a leading '+' is honoured, trailing characters are ignored). A null ok preserves the historic fire-and-forget behaviour.

Parameters
strstring to be converted
okoptional out-parameter signalling conversion success
Returns
converted double value (0.0 on error)

Definition at line 189 of file PStringUtils.cpp.

Referenced by PMsrHandler::HandleFitParameterEntry(), PMsrHandler::HandleFourierEntry(), PMsrHandler::HandleGlobalEntry(), PMsrHandler::HandlePlotEntry(), PMsrHandler::HandleRunEntry(), and PMsrHandler::ParseFourierPhaseValueVector().

◆ ToInt()

int PStringUtils::ToInt ( const std::string & str,
bool * ok = nullptr )
static

Converts the leading part of the string to an int (base 10). Mirrors TString::Atoi(). Returns 0 if no conversion is possible.

If ok is non-null it is set to true when a valid integer was parsed and to false on error (no digits, or value out of int range). This allows callers to distinguish a legitimate 0 from a failed conversion, which the bare return value cannot express.

Parameters
strstring to be converted
okoptional out-parameter signalling conversion success
Returns
converted integer value (0 on error)

Converts the leading part of the string to an int (base 10), mirroring TString::Atoi(). Returns 0 if no conversion is possible.

Uses std::from_chars so that conversion errors can be reported through the optional ok out-parameter: it is set to true when a valid integer was parsed (leading whitespace is skipped) and to false otherwise (no digits present, or the value is out of int range). Trailing non-numeric characters are ignored, as with TString::Atoi(). A null ok preserves the historic fire-and-forget behaviour.

Parameters
strstring to be converted
okoptional out-parameter signalling conversion success
Returns
converted integer value (0 on error)

Definition at line 152 of file PStringUtils.cpp.

Referenced by PMsrHandler::FillParameterInUse(), PMsrHandler::GetNoOfFitParameters(), PMsrHandler::HandleFitParameterEntry(), PMsrHandler::HandleFourierEntry(), PMsrHandler::HandleGlobalEntry(), PMsrHandler::HandlePlotEntry(), PMsrHandler::HandleRunEntry(), PStartupHandler::OnCharacters(), PMsrHandler::ParseFourierPhaseParIterVector(), PMsrHandler::ParseFourierPhaseParVector(), and PMsrHandler::WriteMsrLogFile().


The documentation for this class was generated from the following files: