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::string DoubleToString(Double_t d, std::string format = "%4.2lf");
39Bool_t StringToBool(std::string booleanString);
40Long64_t StringToLong(std::string in);
41TVector3 StringTo3DVector(std::string in);
42TVector2 StringTo2DVector(std::string in);
43std::vector<std::string> Split(std::string in, std::string separator, bool allowBlankString = false,
44 bool removeWhiteSpaces = false, int startPos = -1);
45std::vector<double> StringToElements(std::string in, std::string separator);
46std::vector<double> StringToElements(std::string in, std::string headChar, std::string separator,
47 std::string tailChar);
48std::string RemoveWhiteSpaces(std::string in);
49std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition = 0,
50 Int_t N = 0);
51std::string EscapeSpecialLetters(std::string in);
52std::string ToDateTimeString(time_t time);
53time_t StringToTimeStamp(std::string time);
54TRestStringOutput::REST_Verbose_Level StringToVerboseLevel(std::string in);
55ULong64_t ToHash(std::string str);
56constexpr ULong64_t ToHash(const char* str, ULong64_t last_value = 0xCBF29CE484222325ull) {
57 return *str ? ToHash(str + 1, (*str ^ last_value) * 0x100000001B3ull) : last_value;
58}
59Int_t Count(std::string s, std::string sbstring);
60Int_t FindNthStringPosition(const std::string& in, size_t pos, const std::string& strToFind, size_t nth);
61Bool_t MatchString(std::string str, std::string matcher);
62Int_t DiffString(const std::string& source, const std::string& target);
63template <class T>
64std::string ToString(T source, int length = -1, char fill = ' ') {
65 std::stringstream ss1;
66 ss1 << source;
67 std::string s = ss1.str();
68 if (length < 0) {
69 return s;
70 } else if (s.size() < (unsigned int)length) {
71 return s + std::string(length - s.size(), fill);
72 } else {
73 return s.substr(0, length);
74 }
75}
76
77template <class T>
78std::string StringWithPrecision(T& value, int precision) {
79 std::stringstream ss;
80 ss.precision(precision);
81 ss << value;
82
83 return ss.str();
84}
85
86template <class T1, class T2>
87inline std::vector<T2> Vector_cast(std::vector<T1> vecstring) {
88 std::vector<T2> result;
89 for (T1 s : vecstring) {
90 result.push_back((T2)s);
91 }
92 return result;
93}
94
95std::string ToUpper(std::string in);
96std::string ToLower(std::string in);
97
98std::string FirstToUpper(std::string s);
99
100std::string RightTrim(std::string s, const char* t = " \t\n\r\f\v");
101std::string LeftTrim(std::string s, const char* t = " \t\n\r\f\v");
102std::string Trim(std::string s, const char* t = " \t\n\r\f\v");
103std::string TrimAndUpper(std::string s);
104std::string TrimAndLower(std::string s);
105
106std::string DataMemberNameToParameterName(std::string name);
107std::string ParameterNameToDataMemberName(std::string name);
108
109TF1* CreateTF1FromString(std::string func, double init, double end);
110
111}; // namespace REST_StringHelper
112using namespace REST_StringHelper;
113
114#ifdef WIN32
115#include <process.h>
116inline void setenv(const char* __name, const char* __value, int __replace) {
117 _putenv(((std::string)__name + "=" + (std::string)__value).c_str());
118}
119inline int getpid() { return _getpid(); }
120inline int fileno(FILE* fp) { return _fileno(fp); }
121
122inline void usleep(int usec) {
123 if (usec >= 1000) {
124 _sleep(usec / 1000);
125 } else {
126 _sleep(1); // sleep minimum 1ms on windows
127 }
128}
129inline void sleep(int sec) { _sleep(1000 * sec); }
130#define __PRETTY_FUNCTION__ __FUNCTION__
131#define M_PI 3.14159265358979323846
132#endif
133
134#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 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::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 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 DoubleToString(Double_t d, std::string format="%4.2lf")
Gets a string from a double.
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace every 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.