Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
StringUtils.hxx
Go to the documentation of this file.
1/// \file ROOT/StringUtils.hxx
2/// \author Jonas Rembser <jonas.rembser@cern.ch>
3/// \date 2021-08-09
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_StringUtils
14#define ROOT_StringUtils
15
16#include <string_view>
17
18#include <string>
19#include <vector>
20#include <numeric>
21#include <iterator>
22#include <utility>
23
24namespace ROOT {
25
26std::vector<std::string> Split(std::string_view str, std::string_view delims, bool skipEmpty = false);
27
28/// Given a string `str`, returns a pair of string views into it: the first containing the substring preceding the
29/// first instance of `splitter` and the second containing the substring following it. Note that:
30/// 1. the first instance of `splitter` will not appear in either string;
31/// 2. if `splitter` does not appear, or appears as the last character, the second string view will be empty;
32/// 3. if `splitter` appears as the first character, the first string view will be empty;
33/// 4. if `str` is empty so will both views be.
34/// IMPORTANT: The lifetime of the returned string views is the same as `str`.
35std::pair<std::string_view, std::string_view> SplitAt(std::string_view str, char splitter);
36
37/**
38 * \brief Concatenate a list of strings with a separator
39 * \tparam StringCollection_t Any container of strings (vector, initializer_list, ...)
40 * \param[in] sep Separator inbetween the strings.
41 * \param[in] begin Beginning of the strings container
42 * \param[in] end Past-the-end iterator of the strings container
43 * \return the sep-delimited concatenation of strings
44 */
45template <typename InputIt_t>
46std::string Join(const std::string &sep, InputIt_t begin, InputIt_t end)
47{
48 if (begin == end)
49 return "";
50
51 return std::accumulate(std::next(begin), end, *begin, [&sep](auto const &a, auto const &b) { return a + sep + b; });
52}
53
54/**
55 * \brief Concatenate a list of strings with a separator
56 * \tparam StringCollection_t Any container of strings (vector, initializer_list, ...)
57 * \param[in] sep Separator inbetween the strings.
58 * \param[in] strings container of strings
59 * \return the sep-delimited concatenation of strings
60 */
61template <class StringCollection_t>
62std::string Join(const std::string &sep, StringCollection_t &&strings)
63{
64 return Join(sep, std::begin(strings), std::end(strings));
65}
66
67std::string Round(double value, double error, unsigned int cutoff = 1, std::string_view delim = "#pm");
68
69inline bool StartsWith(std::string_view string, std::string_view prefix)
70{
71 return string.size() >= prefix.size() && string.substr(0, prefix.size()) == prefix;
72}
73
74inline bool EndsWith(std::string_view string, std::string_view suffix)
75{
76 return string.size() >= suffix.size() && string.substr(string.size() - suffix.size(), suffix.size()) == suffix;
77}
78
79} // namespace ROOT
80
81#endif
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
bool EndsWith(std::string_view string, std::string_view suffix)
std::string Join(const std::string &sep, InputIt_t begin, InputIt_t end)
Concatenate a list of strings with a separator.
bool StartsWith(std::string_view string, std::string_view prefix)
std::string Round(double value, double error, unsigned int cutoff=1, std::string_view delim="#pm")
Convert (round) a value and its uncertainty to string using one or two significant digits of the erro...
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
std::pair< std::string_view, std::string_view > SplitAt(std::string_view str, char splitter)
Given a string str, returns a pair of string views into it: the first containing the substring preced...