REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestStringHelper.h
1
2#ifndef RestTools_REST_StringHelper
3#define RestTools_REST_StringHelper
4#pragma once
5
6#include <TF1.h>
7#include <TFormula.h>
8#include <TVector2.h>
9#include <TVector3.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13#include <algorithm>
14#include <cstdio>
15#include <cstring>
16#include <fstream>
17#include <iomanip>
18#include <iostream>
19#include <set>
20#include <sstream>
21
22#include "TRestStringOutput.h"
23
26
27Int_t GetChar(std::string hint = "Press a KEY to continue ...");
28Int_t isANumber(std::string in);
29Int_t isAExpression(const std::string& in);
30std::string CropWithPrecision(std::string in, Int_t precision);
31std::string ReplaceMathematicalExpressions(std::string buffer, Int_t precision = 0,
32 std::string errorMessage = "");
33std::string EvaluateExpression(std::string exp);
34Float_t StringToFloat(std::string in);
35Double_t StringToDouble(std::string in);
36Int_t StringToInteger(std::string in);
37std::string IntegerToString(Int_t n, std::string format = "%d");
38std::vector<int> IntegerToBinary(int number, size_t dimension = 0);
39std::string DoubleToString(Double_t d, std::string format = "%8.6e");
40Bool_t StringToBool(std::string booleanString);
41Long64_t StringToLong(std::string in);
42TVector3 StringTo3DVector(std::string in);
43TVector2 StringTo2DVector(std::string in);
44std::vector<std::string> Split(std::string in, std::string separator, bool allowBlankString = false,
45 bool removeWhiteSpaces = false, int startPos = -1);
46std::vector<double> StringToElements(std::string in, std::string separator);
47std::vector<double> StringToElements(std::string in, std::string headChar, std::string separator,
48 std::string tailChar);
49std::string RemoveDelimiters(std::string in);
50std::string RemoveWhiteSpaces(std::string in);
51std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition = 0,
52 Int_t N = 0);
53std::string EscapeSpecialLetters(std::string in);
54std::string ToDateTimeString(time_t time);
55time_t StringToTimeStamp(std::string time);
56TRestStringOutput::REST_Verbose_Level StringToVerboseLevel(std::string in);
57ULong64_t ToHash(std::string str);
58constexpr ULong64_t ToHash(const char* str, ULong64_t last_value = 0xCBF29CE484222325ull) {
59 return *str ? ToHash(str + 1, (*str ^ last_value) * 0x100000001B3ull) : last_value;
60}
61Int_t Count(std::string s, std::string sbstring);
62Int_t FindNthStringPosition(const std::string& in, size_t pos, const std::string& strToFind, size_t nth);
63Bool_t MatchString(std::string str, std::string matcher);
64Int_t DiffString(const std::string& source, const std::string& target);
65template <class T>
66std::string ToString(T source, int length = -1, char fill = ' ') {
67 std::stringstream ss1;
68 ss1 << source;
69 std::string s = ss1.str();
70 if (length < 0) {
71 return s;
72 } else if (s.size() < (unsigned int)length) {
73 return s + std::string(length - s.size(), fill);
74 } else {
75 return s.substr(0, length);
76 }
77}
78
79template <class T>
80std::string StringWithPrecision(T& value, int precision) {
81 std::stringstream ss;
82 ss.precision(precision);
83 ss << value;
84
85 return ss.str();
86}
87
88template <class T1, class T2>
89inline std::vector<T2> Vector_cast(std::vector<T1> vecstring) {
90 std::vector<T2> result;
91 for (T1 s : vecstring) {
92 result.push_back((T2)s);
93 }
94 return result;
95}
96
97std::string ToUpper(std::string in);
98std::string ToLower(std::string in);
99
100std::string FirstToUpper(std::string s);
101
102std::string RightTrim(std::string s, const char* t = " \t\n\r\f\v");
103std::string LeftTrim(std::string s, const char* t = " \t\n\r\f\v");
104std::string Trim(std::string s, const char* t = " \t\n\r\f\v");
105std::string TrimAndUpper(std::string s);
106std::string TrimAndLower(std::string s);
107
108std::string DataMemberNameToParameterName(std::string name);
109std::string ParameterNameToDataMemberName(std::string name);
110
111TF1* CreateTF1FromString(std::string func, double init, double end);
112
113}; // namespace REST_StringHelper
114using namespace REST_StringHelper;
115
116#ifdef WIN32
117#include <process.h>
118inline void setenv(const char* __name, const char* __value, int __replace) {
119 _putenv(((std::string)__name + "=" + (std::string)__value).c_str());
120}
121inline int getpid() { return _getpid(); }
122inline int fileno(FILE* fp) { return _fileno(fp); }
123
124inline void usleep(int usec) {
125 if (usec >= 1000) {
126 _sleep(usec / 1000);
127 } else {
128 _sleep(1); // sleep minimum 1ms on windows
129 }
130}
131inline void sleep(int sec) { _sleep(1000 * sec); }
132#define __PRETTY_FUNCTION__ __FUNCTION__
133#define M_PI 3.14159265358979323846
134#endif
135
136#endif
REST_Verbose_Level
Enumerate of verbose level, containing five levels.
String helper classes. Declared static to be able to have direct access to the methods.
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
std::string IntegerToString(Int_t n, std::string format="%d")
Gets a string from an integer.
std::string FirstToUpper(std::string s)
Convert the first character of a string to upper case.
time_t StringToTimeStamp(std::string time)
A method to convert a date/time formatted string to a timestamp.
Float_t StringToFloat(std::string in)
Gets a float from a string.
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
Bool_t MatchString(std::string str, std::string matcher)
This method matches a string with certain matcher. Returns true if matched. Supports wildcard charact...
std::string RightTrim(std::string s, const char *t=" \t\n\r\f\v")
Removes all white spaces found at the end of the string (https://stackoverflow.com/questions/216823/w...
Double_t StringToDouble(std::string in)
Gets a double from a string.
TF1 * CreateTF1FromString(std::string func, double init, double end)
Reads a function with parameter options from string and returns it as TF1*.
std::string ToUpper(std::string in)
Convert string to its upper case. Alternative of TString::ToUpper.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
Int_t isAExpression(const std::string &in)
Returns 1 only if valid mathematical expression keywords (or numbers) are found in the string in....
std::string DoubleToString(Double_t d, std::string format="%8.6e")
Gets a string from a double.
std::string TrimAndUpper(std::string s)
It trims and uppers the string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.
std::string LeftTrim(std::string s, const char *t=" \t\n\r\f\v")
Removes all white spaces found at the beginning of the string (https://stackoverflow....
std::string EvaluateExpression(std::string exp)
Evaluates a complex numerical expression and returns the resulting value using TFormula.
std::vector< int > IntegerToBinary(int number, size_t dimension=0)
It returns an integer vector with the binary digits decomposed.
std::string TrimAndLower(std::string s)
It trims and lowers the string.
Int_t Count(std::string s, std::string sbstring)
Counts the number of occurences of substring inside the input string in.
Int_t DiffString(const std::string &source, const std::string &target)
Returns the number of different characters between two strings.
std::string Trim(std::string s, const char *t=" \t\n\r\f\v")
Removes all white spaces found at the beginning and the end of the string (https://stackoverflow....
std::string ParameterNameToDataMemberName(std::string name)
Convert parameter name to datamember name, following REST parameter naming convention.
std::string RemoveDelimiters(std::string in)
Returns the input string removing any delimiters ({[]})
std::string ToLower(std::string in)
Convert string to its lower case. Alternative of TString::ToLower.
std::string DataMemberNameToParameterName(std::string name)
Convert data member name to parameter name, following REST parameter naming convention.
Int_t isANumber(std::string in)
Returns 1 only if a valid number is found in the string in. If not it returns 0.
TVector3 StringTo3DVector(std::string in)
Gets a 3D-vector from a string. Format should be : (X,Y,Z).
Int_t FindNthStringPosition(const std::string &in, size_t pos, const std::string &strToFind, size_t nth)
Returns the position of the nth occurence of the string strToFind inside the string in.
std::string RemoveWhiteSpaces(std::string in)
Returns the input string removing all white spaces.
std::string CropWithPrecision(std::string in, Int_t precision)
It crops a floating number given inside the string in with the given precision. I....
std::vector< double > StringToElements(std::string in, std::string separator)
Convert the input string into a vector of double elements.
std::string ToDateTimeString(time_t time)
Format time_t into string.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.
std::string ReplaceMathematicalExpressions(std::string buffer, Int_t precision=0, std::string errorMessage="")
Evaluates and replaces valid mathematical expressions found in the input string buffer.