GENIEGenerator
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringUtils.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*
3  Copyright (c) 2003-2024, The GENIE Collaboration
4  For the full text of the license visit http://copyright.genie-mc.org
5 
6  Costas Andreopoulos <c.andreopoulos \at cern.ch>
7  University of Liverpool
8 */
9 //____________________________________________________________________________
10 
11 #include <sstream>
12 
14 
15 using std::ostringstream;
16 
17 //____________________________________________________________________________
18 string genie::utils::str::TrimSpaces(string input)
19 {
20  if( input.find_first_not_of(" \n") != 0)
21  input.erase( 0, input.find_first_not_of(" \n") );
22 
23  if( input.find_last_not_of(" \n") != input.length() )
24  input.erase( input.find_last_not_of(" \n")+1, input.length() );
25 
26  return RemoveSuccessiveSpaces(input);
27 }
28 //____________________________________________________________________________
30 {
31  ostringstream os;
32  os << i;
33  return os.str();
34 }
35 //____________________________________________________________________________
36 vector<string> genie::utils::str::Split(string input, string delimiter)
37 {
38 // split a string of 'delimiter' separated values and return each string
39 // part as a vector<string> element.
40 
41  vector<string> string_parts;
42 
43  while(input.find_first_of(delimiter) < input.length()) {
44 
45  string_parts.push_back( input.substr(0, input.find_first_of(delimiter)) );
46  input = input.substr(input.find_first_of(delimiter)+1, input.length());
47  }
48  string_parts.push_back(input);
49  return string_parts;
50 }
51 //____________________________________________________________________________
53 {
54 // this method trims white space that may be found within an expression...
55 // eg. "stop pressing the spacebar" -> "stop pressing the spacebar"
56 
57  string trimmed = input;
58 
59  string::size_type pos = 0;
60 
61  while( (pos = trimmed.find_first_of(" ", pos)) != string::npos ) {
62  if( trimmed[++pos] == ' ' ) trimmed.erase(pos--,1);
63  }
64  return FilterString("\n",trimmed);
65 }
66 //____________________________________________________________________________
68  string& subject, const string& search, const string& replace)
69 {
70 // Searches the string "input" for "search" and replaces this with "replace"
71  size_t pos = 0;
72  while ((pos = subject.find(search, pos)) != std::string::npos) {
73  subject.replace(pos, search.length(), replace);
74  // move to end of replaced string
75  pos += replace.length();
76  }
77 }
78 //____________________________________________________________________________
79 string genie::utils::str::FilterString(string filt_elements, string input)
80 {
81 // filter out from 'input' all characters that can be found in 'filt_elements'
82 
83  string filtered_string = input;
84  string::size_type pos = 0;
85 
86  while( (pos = filtered_string.find_first_of(
87  filt_elements, pos)) != string::npos )
88  filtered_string.erase(pos,1);
89  return filtered_string;
90 }
91 //____________________________________________________________________________
92 string genie::utils::str::ToUpper(string input)
93 {
94  for(unsigned int i=0; i<input.size(); i++) input[i] = toupper(input[i]);
95  return input;
96 }
97 //____________________________________________________________________________
98 string genie::utils::str::ToLower(string input)
99 {
100  for(unsigned int i=0; i<input.size(); i++) input[i] = tolower(input[i]);
101  return input;
102 }
103 //____________________________________________________________________________
string ToLower(string input)
Definition: StringUtils.cxx:98
string IntAsString(int i)
Definition: StringUtils.cxx:29
string RemoveSuccessiveSpaces(string input)
Definition: StringUtils.cxx:52
string TrimSpaces(string input)
Definition: StringUtils.cxx:18
string FilterString(string filt, string input)
Definition: StringUtils.cxx:79
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
string ToUpper(string input)
Definition: StringUtils.cxx:92
void ReplaceStringInPlace(string &subject, const string &search, const string &replace)
Definition: StringUtils.cxx:67