byteman  1.3 (Build #225)
Bitstream relocation and manipulation tool
str.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 Kristiyan Manev (University of Manchester)
3  *
4  * Licensed under the Apache License, Version 2.0(the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 
17 #ifndef STR_H
18 #define STR_H
19 
20 #include<algorithm> //replace
21 #include<string>
22 
23 #include "str/iff.h"
24 #include "str/parse.h"
25 
26 /**************************************************************************/
31 namespace str{
33  inline std::string findStringAndGetAllAfter(std::string checkedString, std::string searchString)
34  {
35  size_t loc = checkedString.find(searchString);
36  if(std::string::npos == loc)
37  return std::string("");
38  return checkedString.substr(loc + searchString.size());
39  }
41  inline std::string replace(std::string str, char oldChar, char newChar)
42  {
43  replace(str.begin(), str.end(), oldChar, newChar);
44  return str;
45  }
47  inline std::string removeSpaces(std::string str)
48  {
49  str.erase(remove_if(str.begin(), str.end(), ::isspace), str.end());
50  return str;
51  }
53  inline std::string stringToUpper(std::string str)
54  {
55  transform(str.begin(), str.end(), str.begin(), ::toupper);
56  return str;
57  }
59  inline std::string stringToLower(std::string str)
60  {
61  transform(str.begin(), str.end(), str.begin(), ::tolower);
62  return str;
63  }
65  inline std::string removeExternalQuotes(std::string str)
66  {
67  if(str.empty())
68  return str;
69  if(str.front() == '"')
70  str.erase(str.begin());
71  if(str.empty())
72  return str;
73  if(str.back() == '"')
74  str.erase(str.end()-1);
75  return str;
76  }
77  inline void findAndReplace(std::string &str, std::string toFind, std::string toReplaceWith)
78  {
79  size_t pos = str.find(toFind);
80  while( pos != std::string::npos) {
81  str.replace(pos, toFind.size(), toReplaceWith);
82  pos = str.find(toFind, pos + toReplaceWith.size());
83  }
84  }
85 }
86 #endif //STR_H
Definition: iff.h:29
std::string stringToLower(std::string str)
Replaces all uppercase characters in str with lowercase and returns the resulting string.
Definition: str.h:59
std::string stringToUpper(std::string str)
Replaces all lowercase characters in str with uppercase and returns the resulting string.
Definition: str.h:53
std::string replace(std::string str, char oldChar, char newChar)
Replaces all instances of oldChar in string str with newChar and returns the resulting string.
Definition: str.h:41
std::string removeSpaces(std::string str)
Removes all space chars of str returns the resulting string.
Definition: str.h:47
void findAndReplace(std::string &str, std::string toFind, std::string toReplaceWith)
Definition: str.h:77
std::string removeExternalQuotes(std::string str)
Removes double quotes from start and end of string str and returns the resulting string.
Definition: str.h:65
std::string findStringAndGetAllAfter(std::string checkedString, std::string searchString)
Finds string searchString inside checkedString and returns all to the right inside checkedString....
Definition: str.h:33