g4tools  5.4.0
srep
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_srep
5 #define tools_srep
6 
7 #include <string>
8 #include <vector>
9 
10 #include "forit"
11 
12 namespace tools {
13 
14 inline void replace(std::string& a_string,char a_old,char a_new){
15  tools_sforit(a_string,it) {
16  if((*it)==a_old) *it = a_new;
17  }
18 }
19 
20 inline bool replace(std::string& a_string,const std::string& a_old,const std::string& a_new){
21  // return true : some replacement done.
22  // return false : nothing replaced.
23  if(a_old.empty()) return false;
24  std::string snew;
25  std::string::size_type lold = a_old.length();
26  bool status = false;
27  std::string stmp = a_string;
28  while(true) {
29  std::string::size_type pos = stmp.find(a_old);
30  if(pos==std::string::npos){
31  snew += stmp;
32  break;
33  } else {
34  snew += stmp.substr(0,pos);
35  snew += a_new;
36  stmp = stmp.substr(pos+lold,stmp.length()-(pos+lold));
37  status = true;
38  }
39  }
40  a_string = snew;
41  return status;
42 }
43 
44 inline bool replace_(std::string& a_string,const std::string& a_old,const std::string& a_new) {
45  return replace(a_string,a_old,a_new);
46 }
47 
48 inline bool replace(std::vector<std::string>& a_strings,const std::string& a_old,const std::string& a_new){
49  tools_vforit(std::string,a_strings,it) {
50  if(!replace(*it,a_old,a_new)) return false;
51  }
52  return true;
53 }
54 
55 inline void toxml(std::string& a_string){
56  // > : &lt;
57  // < : &gt;
58  // & : &amp;
59  // " : &quot;
60  // ' : &apos;
61  replace(a_string,"&","&amp;"); //must be first.
62  replace(a_string,"<","&lt;");
63  replace(a_string,">","&gt;");
64  replace(a_string,"\"","&quot;");
65  replace(a_string,"'","&apos;");
66 }
67 
68 inline std::string to_xml(const std::string& a_string){
69  std::string s = a_string;
70  toxml(s);
71  return s;
72 }
73 
74 inline void to_win(std::string& a_string) {
75  replace(a_string,"/cygdrive/c","C:"); // CYGWIN.
76  replace(a_string,"/mnt/c","C:"); // WSL.
77  replace(a_string,'/','\\');
78 }
79 
80 inline void to_win_python(std::string& a_string) {
81  replace(a_string,"/cygdrive/c","c:"); // CYGWIN.
82  replace(a_string,"/mnt/c","c:"); // WSL. (Python wants c: in lowercase).
83  replace(a_string,"C:","c:");
84  replace(a_string,'\\','/');
85 }
86 
87 }
88 
89 #endif
tools::replace
void replace(std::string &a_string, char a_old, char a_new)
Definition: srep:14
tools::to_win
void to_win(std::string &a_string)
Definition: srep:74
tools::to_win_python
void to_win_python(std::string &a_string)
Definition: srep:80
tools::to_xml
std::string to_xml(const std::string &a_string)
Definition: srep:68
tools_vforit
#define tools_vforit(a__T, a__v, a__it)
Definition: forit:13
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::replace_
bool replace_(std::string &a_string, const std::string &a_old, const std::string &a_new)
Definition: srep:44
tools_sforit
#define tools_sforit(a__s, a__it)
Definition: forit:37
forit
tools::toxml
void toxml(std::string &a_string)
Definition: srep:55