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