Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // G4String inline methods implementation 27 // 28 // Author: G.Cosmo, 11 November 1999 29 //--------------------------------------------------------------------- 30 31 inline G4String::G4String(const std::string& str) 32 : std::string(str) 33 {} 34 35 inline G4String::G4String(const G4String& str) = default; 36 37 inline G4String::G4String(std::string&& str) 38 : std::string(std::move(str)) 39 {} 40 41 inline G4String::G4String(G4String&& str) 42 : std::string(std::move(str)) 43 {} 44 45 inline G4String& G4String::operator=(const G4String& str) 46 { 47 if(&str == this) 48 { 49 return *this; 50 } 51 std::string::operator=(str); 52 return *this; 53 } 54 55 inline G4String& G4String::operator=(G4String&& str) 56 { 57 std::string::operator=(std::move(str)); 58 return *this; 59 } 60 61 inline G4String::operator const char*() const { return c_str(); } 62 63 inline G4String::reference G4String::operator[](int pos) 64 { 65 return std::string::operator[](pos); 66 } 67 68 inline G4String::const_reference G4String::operator[](int pos) const 69 { 70 return std::string::operator[](pos); 71 } 72 73 inline G4int G4String::compareTo(std::string_view str, caseCompare mode) const 74 { 75 if(mode == exact) 76 { 77 return compare(str); 78 } 79 80 return G4StrUtil::icompare(*this, str); 81 } 82 83 inline std::istream& G4String::readLine(std::istream& strm, G4bool skipWhite) 84 { 85 return G4StrUtil::readline(strm, *this, skipWhite); 86 } 87 88 inline G4String& G4String::remove(size_type n) 89 { 90 G4StrUtil::safe_erase(*this, n); 91 return *this; 92 } 93 94 inline G4bool G4String::contains(const std::string& str) const 95 { 96 // Use of const char* required to resolve ambiguity with std::string_view 97 return G4StrUtil::contains(*this, str.c_str()); 98 } 99 100 inline G4bool G4String::contains(char ch) const 101 { 102 return G4StrUtil::contains(*this, ch); 103 } 104 105 inline G4String G4String::strip(G4String::stripType strip_Type, char ch) 106 { 107 if(empty()) 108 { 109 return ""; 110 } 111 112 G4String retVal = *this; 113 114 switch(strip_Type) 115 { 116 case G4String::leading: 117 G4StrUtil::lstrip(retVal, ch); 118 break; 119 case G4String::trailing: 120 G4StrUtil::rstrip(retVal, ch); 121 break; 122 case G4String::both: 123 G4StrUtil::strip(retVal, ch); 124 break; 125 default: 126 break; 127 } 128 129 return retVal; 130 } 131 132 inline void G4String::toLower() { G4StrUtil::to_lower(*this); } 133 134 inline void G4String::toUpper() { G4StrUtil::to_upper(*this); } 135 136 namespace G4StrUtil 137 { 138 inline void to_lower(G4String& str) 139 { 140 std::transform(str.begin(), str.end(), str.begin(), 141 [](unsigned char ch) { return std::tolower(ch); }); 142 } 143 144 inline G4String to_lower_copy(G4String str) 145 { 146 to_lower(str); 147 return str; 148 } 149 150 inline void to_upper(G4String& str) 151 { 152 std::transform(str.begin(), str.end(), str.begin(), 153 [](unsigned char ch) { return std::toupper(ch); }); 154 } 155 156 inline G4String to_upper_copy(G4String str) 157 { 158 to_upper(str); 159 return str; 160 } 161 162 inline void lstrip(G4String& str, char ch) 163 { 164 auto startIndex = str.find_first_not_of(ch); 165 str.erase(0, startIndex); 166 } 167 168 inline void rstrip(G4String& str, char ch) 169 { 170 auto endIndex = str.find_last_not_of(ch); 171 if(endIndex == G4String::npos) 172 { 173 str = ""; 174 } 175 else 176 { 177 str.erase(endIndex + 1); 178 } 179 } 180 181 inline void strip(G4String& str, char ch) 182 { 183 if(str.empty()) 184 { 185 return; 186 } 187 188 lstrip(str, ch); 189 rstrip(str, ch); 190 } 191 192 inline G4String lstrip_copy(G4String str, char ch) 193 { 194 lstrip(str, ch); 195 return str; 196 } 197 198 inline G4String rstrip_copy(G4String str, char ch) 199 { 200 rstrip(str, ch); 201 return str; 202 } 203 204 inline G4String strip_copy(G4String str, char ch) 205 { 206 strip(str, ch); 207 return str; 208 } 209 210 inline G4bool contains(const G4String& str, std::string_view ss) 211 { 212 return str.find(ss) != G4String::npos; 213 } 214 215 inline G4bool contains(const G4String& str, char ss) 216 { 217 return str.find(ss) != G4String::npos; 218 } 219 220 inline G4bool contains(const G4String& str, const char* ss) 221 { 222 return str.find(ss) != G4String::npos; 223 } 224 225 inline G4bool contains(const G4String& str, const G4String& ss) 226 { 227 return str.find(ss) != G4String::npos; 228 } 229 230 inline G4int icompare(std::string_view lhs, std::string_view rhs) 231 { 232 G4String buf1 = G4StrUtil::to_lower_copy(G4String(lhs.data(), lhs.size())); 233 G4String buf2 = G4StrUtil::to_lower_copy(G4String(rhs.data(), rhs.size())); 234 return buf1.compare(buf2); 235 } 236 237 inline bool starts_with(const G4String& str, std::string_view ss) 238 { 239 return str.rfind(ss, 0) == 0; 240 } 241 242 inline bool starts_with(const G4String& str, G4String::value_type ss) 243 { 244 return !str.empty() && (str[0] == ss); 245 } 246 247 inline bool starts_with(const G4String& str, const char* ss) 248 { 249 return str.rfind(ss, 0) == 0; 250 } 251 252 inline bool starts_with(const G4String& str, const G4String& ss) 253 { 254 return str.rfind(ss, 0) == 0; 255 } 256 257 inline bool ends_with(const G4String& str, std::string_view ss) 258 { 259 if(str.length() < ss.length()) 260 { 261 return false; 262 } 263 return str.compare(str.length() - ss.length(), ss.length(), ss) == 0; 264 } 265 266 inline bool ends_with(const G4String& str, G4String::value_type ss) 267 { 268 return !str.empty() && (str.back() == ss); 269 } 270 271 inline bool ends_with(const G4String& str, const char* ss) 272 { 273 std::string_view sv(ss); 274 return ends_with(str, sv); 275 } 276 277 inline bool ends_with(const G4String& str, const G4String& ss) 278 { 279 return ends_with(str, ss.c_str()); 280 } 281 282 inline void safe_erase(G4String& str, G4String::size_type index, 283 G4String::size_type count) 284 { 285 if(index < str.size()) 286 { 287 str.erase(index, count); 288 } 289 } 290 291 inline std::istream& readline(std::istream& is, G4String& str, 292 G4bool skipWhite) 293 { 294 char tmp[1024]; 295 if(skipWhite) 296 { 297 is >> std::ws; 298 } 299 300 is.getline(tmp, 1024); 301 str = tmp; 302 303 return is; 304 } 305 } // namespace G4StrUtil 306