simple-tof-analysis
 All Classes Namespaces Functions Variables Groups Pages
StringUtils.h
1 #ifndef STRINGUTIL_H_
2 #define STRINGUTIL_H_
3 
4 
5 #include <iostream>
6 #include <sstream>
7 #include <stdio.h>
8 #include <string>
9 
10 #include <TObjArray.h>
11 #include <TObjString.h>
12 #include <TString.h>
13 
14 
15 using std::vector;
16 using std::string;
17 
25 namespace STR_UTILS
26 {
27 
28 
29 
30 static struct IsLonger{
31  //longer TString must appear before in a std::sort ordering
32  bool operator()(TString s1, TString s2){ return ( s1.Length() > s2.Length() ); }
33 } CompLengthDecr;
34 
35 static struct IsShorter{
36  //longer TString must appear before in a std::sort ordering
37  bool operator()(TString s1, TString s2){ return ( s1.Length() < s2.Length() ); }
38 } CompLengthIncr;
39 
40 
41 inline vector<TString> Tokenize(TString line,TString delim){
42 
43  vector<TString> list;
44  if(line.IsNull() || line.IsWhitespace()) return list;
45 
46  const char *delimiters = delim.Data();//":";
47 
48  TObjArray* Strings = line.Tokenize(delimiters);
49 
50  if(Strings->GetEntriesFast()) {
51  TIter iString(Strings);
52  TObjString* os=0;
53  Int_t j=0;
54  while ((os=(TObjString*)iString())) {
55  j++;
56 
57  //cout<<os->GetString().Data()<<endl;
58 
59  list.push_back(os->GetString());
60  }
61  delete os;
62  }
63 
64  delete Strings;
65 
66  return list;
67 
68 }
69 
70 
71 inline vector<string> TokenizeStr(string line,string delim){
72 
73 
74  std::vector<TString> TS = Tokenize(TString(line),TString(delim));
75 
76  std::vector<string> list;
77 
78  for(int i=0; i< TS.size(); ++i){
79  //list.push_back( string(TS[i].Data()) );
80  list.push_back( string(TS[i]) );
81  }
82 
83  return list;
84 }
85 
86 
87 
88 inline int Duplicates( std::vector<TString> list){
89 
90  int N=0;
91 
92  for(int i1=0; i1< list.size()-1; ++i1){
93  for(int i2=i1+1; i2< list.size();++i2){
94  if(list.at(i1)==list.at(i2)) N++;
95  }
96  }
97 
98  return N;
99 }
100 
101 
102 inline bool StringIsPresent(std::vector<TString> &list, TString &s){
103 
104  for(int i=0;i<list.size();++i){
105  if( list.at(i)==s ) return 1;
106  }
107 
108  return 0;
109 }
110 
111 
112 template <typename T>
113  string ToString ( T Number )
114  {
115  std::ostringstream ss;
116  ss << Number;
117  return ss.str();
118  }
119 
120 
121 
122 template <typename N>
123  N ToNumber ( const string &Text )
124  {
125  std::istringstream ss(Text);
126  N result;
127  return ss >> result ? result : 0;
128  }
129 
130 
131 
132 
133 inline TString Name(TString str1,TString sep,TString str2="") {
134 
135  TString name=str1;
136 
137  name+=sep; name+=str2;
138 
139  return name;
140 
141 }
142 
143 inline TString Name(TString str1,TString sep1,TString str2,TString sep2, TString str3) {
144 
145  TString name=Name(str1,sep1,str2);
146 
147  name+=sep2; name+=str3;
148 
149  return name;
150 
151 }
152 
153 inline TString Name(TString str1,TString sep1,TString str2,TString sep2, TString str3, TString sep3, TString str4) {
154 
155  TString name=Name(str1,sep1,str2,sep2,str3);
156 
157  name+=sep3; name+=str4;
158 
159 
160  return name;
161 
162 }
163 
164 inline TString SplitLineLabel(TString top, TString bottom){
165 
166  TString label = "";
167 
168  if(bottom.IsWhitespace()) return top;
169 
170  label = Name("#splitline{",top,"}{",bottom,"}");
171 
172  return label;
173 }
174 
175 inline TString Title(TString tit1,TString sep,TString tit2) {
176 
177  TString title=tit1;
178 
179  title+=sep; title+=tit2;
180 
181  return title;
182 
183 }
184 
185 inline TString TitleXYZ(TString tit,TString titx,TString tity="",TString titz="") {
186 
187 
188  TString title=tit;
189 
190  title+=";"; title+=titx;
191 
192  if(!tity.IsWhitespace() ){
193  title+=";"; title+=tity;
194  }
195 
196  if(!titz.IsWhitespace() ){
197  title+=";"; title+=titz;
198  }
199 
200 
201  return title;
202 }
203 
204 
205 inline TString AppendToTitleXYZ(TString tit,TString sep, TString tit2) {
206 
219  TString str="";
220 
221  std::vector<TString> vec=STR_UTILS::Tokenize(tit,";");
222 
223  int n=vec.size();
224 
225  if(n==0) return tit2;
226 
227  if(n>=1){
228  str=STR_UTILS::Name(vec.at(0),sep,tit2);
229  }
230 
231 
232  if(n>=2){
233  str=STR_UTILS::TitleXYZ(str,vec.at(1));
234  }
235 
236  if(n>=3){
237  str=STR_UTILS::TitleXYZ(str,vec.at(2));
238  }
239 
240  if(n>=4){
241  str=STR_UTILS::TitleXYZ(str,vec.at(3));
242  }
243 
244  return str;
245 }
246 
247 inline TString PrependToTitleXYZ(TString tit,TString sep, TString tit2) {
248 
262  TString str="";
263 
264  std::vector<TString> vec=STR_UTILS::Tokenize(tit,";");
265 
266  int n=vec.size();
267 
268  if(n==0) return tit2;
269 
270  if(n>=1){
271  str=STR_UTILS::Name(tit2,sep,vec.at(0));
272  }
273 
274 
275  if(n>=2){
276  str=STR_UTILS::TitleXYZ(str,vec.at(1));
277  }
278 
279  if(n>=3){
280  str=STR_UTILS::TitleXYZ(str,vec.at(2));
281  }
282 
283  if(n>=4){
284  str=STR_UTILS::TitleXYZ(str,vec.at(3));
285  }
286 
287  return str;
288 }
289 
290 
291 
292 }
293 
294 
295 namespace StringUtils = STR_UTILS;
296 
297 #endif
TString PrependToTitleXYZ(TString tit, TString sep, TString tit2)
Definition: StringUtils.h:247
TString AppendToTitleXYZ(TString tit, TString sep, TString tit2)
Definition: StringUtils.h:205