MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
strutil.cpp
Go to the documentation of this file.
1 /*******************************************************************
2  * File: StringUtil.cpp
3  * Description:
4  * Author: Subhasis Ray
5  * E-mail: ray.subhasis@gmail.com
6  * Created: 2007-09-25 12:12:10
7  ********************************************************************/
8 #include <string>
9 #include <iostream>
10 #include <vector>
11 #include "strutil.h"
12 
13 using namespace std;
14 
15 namespace moose
16 {
17 
18 // Adapted from code available on oopweb.com
19 void tokenize( const string& str, const string& delimiters, vector< string >& tokens )
20 {
21  // Token boundaries
22  string::size_type begin = str.find_first_not_of( delimiters, 0 );
23  string::size_type end = str.find_first_of( delimiters, begin );
24 
25  while ( string::npos != begin || string::npos != end )
26  {
27  // Found a token, add it to the vector.
28  tokens.push_back( str.substr( begin, end - begin ) );
29 
30  // Update boundaries
31  begin = str.find_first_not_of( delimiters, end );
32  end = str.find_first_of( delimiters, begin );
33  }
34 }
35 
36 string& clean_type_name(string& arg)
37 {
38  for (size_t pos = arg.find(' '); pos != string::npos; pos = arg.find(' '))
39  {
40  arg.replace(pos, 1, 1, '_');
41  }
42  for (size_t pos = arg.find('<'); pos != string::npos; pos = arg.find('<'))
43  {
44  arg.replace(pos, 1, 1, '_');
45  }
46  for (size_t pos = arg.find('>'); pos != string::npos; pos = arg.find('>'))
47  {
48  arg.replace(pos, 1, 1, '_');
49  }
50  return arg;
51 }
52 
53 std::string trim(const std::string myString, const string& delimiters)
54 {
55  if (myString.length() == 0 )
56  {
57  return myString;
58  }
59 
60  string::size_type end = myString.find_last_not_of(delimiters);
61  string::size_type begin = myString.find_first_not_of(delimiters);
62 
63  if(begin != string::npos)
64  {
65  return std::string(myString, begin, end-begin+1);
66  }
67 
68  return "";
69 }
70 
71 std::string fix(const std::string userPath, const string& delimiters)
72 {
73  string trimmedPath = trim(userPath, delimiters);
74 
75  string fixedPath;
76  char prev = 0;
77 
78  // In this loop, we check if there are more than one '/' together. If yes,
79  // then accept only first one and reject other.
80  for(unsigned int i = 0; i < trimmedPath.size(); ++i)
81  {
82  const char c = trimmedPath[i];
83  if(c != '/' || c != prev)
84  fixedPath.push_back(c);
85  prev = c;
86  }
87  return fixedPath;
88 }
89 
90 int testTrim()
91 {
92 
93  std::string testStrings [] =
94  {
95  " space at beginning",
96  "space at end ",
97  " space at both sides ",
98  "\ttab at beginning",
99  "tab at end\t",
100  "\ttab at both sides\t",
101  "\nnewline at beginning",
102  "newline at end\n",
103  "\nnewline at both sides\n",
104  "\n\tnewline and tab at beginning",
105  "space and tab at end \t",
106  " \rtab and return at both sides \r"
107  };
108 
109  std::string results[] =
110  {
111  "space at beginning",
112  "space at end",
113  "space at both sides",
114  "tab at beginning",
115  "tab at end",
116  "tab at both sides",
117  "newline at beginning",
118  "newline at end",
119  "newline at both sides",
120  "newline and tab at beginning",
121  "space and tab at end",
122  "tab and return at both sides"
123  };
124 
125  bool success = true;
126 
127  for (unsigned int i = 0; i < sizeof(testStrings)/sizeof(*testStrings); ++i )
128  {
129  std::string trimmed = trim(testStrings[i]);
130  success = (results[i].compare(trimmed)==0);
131 
132  cout << "'" << trimmed << "'" << (success ?" SUCCESS":"FAILED") << endl;
133  }
134  return success?1:0;
135 }
136 
137 
138 bool endswith(const string & full, const string & ending)
139 {
140  if (full.length() < ending.length())
141  {
142  return false;
143  }
144  return (0 == full.compare(full.length() - ending.length(), ending.length(), ending));
145 }
146 
147 /* Compare two strings. */
148 int strncasecmp( const string& a, const string& b, size_t n)
149 {
150  for( size_t i = 0; i < std::min(n, b.size()); ++i )
151  if( tolower(a[i]) != tolower(b[i]) )
152  return tolower(a[i]) - tolower(b[i]);
153 
154  if( b.size() < n )
155  return a.size() - b.size();
156 
157  return 0;
158 }
159 
160 // This function is modification of this solution:
161 // https://stackoverflow.com/a/440240/1805129
162 string random_string( const unsigned len )
163 {
164  static const char alphanum[] =
165  "0123456789"
166  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
167  "abcdefghijklmnopqrstuvwxyz";
168 
169  string s(len, '_' );
170  for (unsigned i = 0; i < len; ++i)
171  {
172  s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
173  }
174 
175  return s;
176 }
177 
178 }
bool endswith(const string &full, const string &ending)
Definition: strutil.cpp:138
int testTrim()
Definition: strutil.cpp:90
int strncasecmp(const string &a, const string &b, size_t n)
Compares the two strings a and b for first n characters, ignoring the case of the characters...
Definition: strutil.cpp:148
std::string fix(const std::string userPath, const string &delimiters)
Definition: strutil.cpp:71
string & clean_type_name(string &arg)
Definition: strutil.cpp:36
void tokenize(const string &str, const string &delimiters, vector< string > &tokens)
Definition: strutil.cpp:19
std::string trim(const std::string myString, const string &delimiters)
Definition: strutil.cpp:53
string random_string(const unsigned len)
Definition: strutil.cpp:162