GENIEGenerator
Loading...
Searching...
No Matches
StringUtils.cxx
Go to the documentation of this file.
1//____________________________________________________________________________
2/*
3 Copyright (c) 2003-2025, 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
15using std::ostringstream;
16
17//____________________________________________________________________________
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//____________________________________________________________________________
36vector<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//____________________________________________________________________________
79string 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//____________________________________________________________________________
92string 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//____________________________________________________________________________
98string 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//____________________________________________________________________________
void ReplaceStringInPlace(string &subject, const string &search, const string &replace)
string ToUpper(string input)
string TrimSpaces(string input)
string ToLower(string input)
string FilterString(string filt, string input)
vector< string > Split(string input, string delim)
string RemoveSuccessiveSpaces(string input)
string IntAsString(int i)