Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // G4String 26 // G4String 27 // 27 // 28 // Class description: 28 // Class description: 29 // 29 // 30 // Common string type for Geant4 30 // Common string type for Geant4 31 // 31 // 32 // Provides a `std::string` compliant implemen 32 // Provides a `std::string` compliant implementation of basic 33 // strings. It currently inherits from `std::s 33 // strings. It currently inherits from `std::string`, but this should 34 // not be assumed other than that it will impl 34 // not be assumed other than that it will implement the same interface 35 // as `std::string` as of the minimum C++ stan 35 // as `std::string` as of the minimum C++ standard required by Geant4 36 // (currently C++17). 36 // (currently C++17). 37 // 37 // 38 // It can be passed to any function accepting 38 // It can be passed to any function accepting `std::string` or `std::string_view` 39 // arguments. Whilst it currently implements a 39 // arguments. Whilst it currently implements a conversion operator to `const char*`, 40 // this should be considered deprecated for us 40 // this should be considered deprecated for use outside of Geant4. Passing to 41 // non-Geant4 functions taking `const char*` a 41 // non-Geant4 functions taking `const char*` arguments should use the 42 // `std::string::c_str` member function to exp 42 // `std::string::c_str` member function to explicitly convert. 43 // 43 // 44 // See `std::string` for primary interfaces, ` 44 // See `std::string` for primary interfaces, `G4StrUtil` for additional query/manipulation functions 45 // 45 // 46 // Author: G.Cosmo, 11 November 1999 46 // Author: G.Cosmo, 11 November 1999 47 //-------------------------------------------- 47 //--------------------------------------------------------------------- 48 48 49 #ifndef G4String_hh 49 #ifndef G4String_hh 50 #define G4String_hh 1 50 #define G4String_hh 1 51 51 52 #include "G4Types.hh" 52 #include "G4Types.hh" 53 53 54 #include <algorithm> 54 #include <algorithm> 55 #include <cstdio> 55 #include <cstdio> 56 #include <cstring> 56 #include <cstring> 57 #include <iostream> 57 #include <iostream> 58 #include <string> 58 #include <string> 59 #include <string_view> 59 #include <string_view> 60 60 61 class G4String : public std::string 61 class G4String : public std::string 62 { 62 { 63 public: 63 public: 64 /// @brief 64 /// @brief 65 /// @deprecated Will be removed in future re 65 /// @deprecated Will be removed in future release 66 enum caseCompare 66 enum caseCompare 67 { 67 { 68 exact, 68 exact, 69 ignoreCase 69 ignoreCase 70 }; 70 }; 71 71 72 /// @brief 72 /// @brief 73 /// @deprecated Will be removed in future re 73 /// @deprecated Will be removed in future release 74 enum stripType 74 enum stripType 75 { 75 { 76 leading, 76 leading, 77 trailing, 77 trailing, 78 both 78 both 79 }; 79 }; 80 80 81 using std::string::string; 81 using std::string::string; 82 using std::string::operator=; 82 using std::string::operator=; 83 83 84 inline G4String() = default; 84 inline G4String() = default; 85 inline G4String(const std::string&); 85 inline G4String(const std::string&); 86 inline G4String(const G4String&); 86 inline G4String(const G4String&); 87 inline G4String(std::string&&); 87 inline G4String(std::string&&); 88 inline G4String(G4String&&); 88 inline G4String(G4String&&); 89 inline G4String& operator=(const G4String&); 89 inline G4String& operator=(const G4String&); 90 inline G4String& operator=(G4String&&); 90 inline G4String& operator=(G4String&&); 91 91 92 /// @brief Implicitly convert to `const char 92 /// @brief Implicitly convert to `const char*` 93 /// @deprecated Will be removed in future re 93 /// @deprecated Will be removed in future releases for `std::string` compliance 94 /// If passing `G4String` to functions req 94 /// If passing `G4String` to functions requiring `const char*`, use `std::string::c_str` 95 /// to explicitly convert. `G4String` also 95 /// to explicitly convert. `G4String` also implicitly converts to `std::string_view` 96 /// to match the `std::string` interface. 96 /// to match the `std::string` interface. 97 inline operator const char*() const; 97 inline operator const char*() const; 98 98 99 /// @brief Override of subscript operator fo 99 /// @brief Override of subscript operator for `int` to suppress C2666 errors with MSVC 100 /// @deprecated Will be removed at the same 100 /// @deprecated Will be removed at the same time as `operator const char*` that requires it 101 /// 101 /// 102 /// This override is required because of G4S 102 /// This override is required because of G4String's provision of an implicit conversion 103 /// operator to `const char*`. Together with 103 /// operator to `const char*`. Together with the subscript operator and C++'s built-in 104 /// `operator[](const char*, int) operator, 104 /// `operator[](const char*, int) operator, use of G4String::operator[] will trigger 105 /// [MSVC error C2666](https://docs.microsof 105 /// [MSVC error C2666](https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2666?view=msvc-170) 106 /// This is a known issue with mixing implic 106 /// This is a known issue with mixing implicit conversion to `const char*` and subscript 107 /// operators. Provision of the override wit 107 /// operators. Provision of the override with `int` argument is thus a workaround 108 /// until the conversion operator is removed 108 /// until the conversion operator is removed. 109 inline reference operator[](int); 109 inline reference operator[](int); 110 110 111 /// @overload 111 /// @overload 112 inline const_reference operator[](int) const 112 inline const_reference operator[](int) const; 113 113 114 /// @brief Deprecated function 114 /// @brief Deprecated function 115 /// @deprecated Use `std::string::compare` o 115 /// @deprecated Use `std::string::compare` or `G4StrUtil::icompare` instead 116 [[deprecated("Use std::string::compare, or G 116 [[deprecated("Use std::string::compare, or G4StrUtil::icompare for case-insensitive comparison")]] 117 inline G4int compareTo(std::string_view, cas 117 inline G4int compareTo(std::string_view, caseCompare mode = exact) const; 118 118 119 /// @brief Deprecated function 119 /// @brief Deprecated function 120 /// @deprecated Use `std::getline` plus `G4S 120 /// @deprecated Use `std::getline` plus `G4StrUtil::lstrip` instead 121 [[deprecated("Use std::getline instead, plus 121 [[deprecated("Use std::getline instead, plus G4StrUtil::lstrip if required")]] 122 inline std::istream& readLine(std::istream&, 122 inline std::istream& readLine(std::istream&, G4bool skipWhite = true); 123 123 124 /// @brief Deprecated function 124 /// @brief Deprecated function 125 /// @deprecated Use `std::string::erase` ins 125 /// @deprecated Use `std::string::erase` instead 126 [[deprecated("Use std::string::erase instead 126 [[deprecated("Use std::string::erase instead")]] 127 inline G4String& remove(size_type); 127 inline G4String& remove(size_type); 128 128 129 /// @brief Deprecated function 129 /// @brief Deprecated function 130 /// @deprecated Use `G4StrUtil::contains` in 130 /// @deprecated Use `G4StrUtil::contains` instead 131 [[deprecated("Use G4StrUtil::contains instea 131 [[deprecated("Use G4StrUtil::contains instead")]] 132 inline G4bool contains(const std::string&) c 132 inline G4bool contains(const std::string&) const; 133 133 134 /// @brief Deprecated function 134 /// @brief Deprecated function 135 /// @deprecated Use `G4StrUtil::contains` in 135 /// @deprecated Use `G4StrUtil::contains` instead 136 [[deprecated("Use G4StrUtil::contains instea 136 [[deprecated("Use G4StrUtil::contains instead")]] 137 inline G4bool contains(char) const; 137 inline G4bool contains(char) const; 138 138 139 /// @brief Deprecated function 139 /// @brief Deprecated function 140 /// @deprecated Use `G4StrUtil` functions in 140 /// @deprecated Use `G4StrUtil` functions instead 141 [[deprecated("Use G4StrUtil::{lstrip,rstrip, 141 [[deprecated("Use G4StrUtil::{lstrip,rstrip,strip}_copy instead")]] 142 [[nodiscard]] inline G4String strip(stripTyp 142 [[nodiscard]] inline G4String strip(stripType strip_Type = trailing, char ch = ' '); 143 143 144 /// @brief Deprecated function 144 /// @brief Deprecated function 145 /// @deprecated Use `G4StrUtil` functions in 145 /// @deprecated Use `G4StrUtil` functions instead 146 [[deprecated("Use G4StrUtil::to_lower/to_low 146 [[deprecated("Use G4StrUtil::to_lower/to_lower_copy instead")]] 147 inline void toLower(); 147 inline void toLower(); 148 148 149 /// @brief Deprecated function 149 /// @brief Deprecated function 150 /// @deprecated Use `G4StrUtil` functions in 150 /// @deprecated Use `G4StrUtil` functions instead 151 [[deprecated("Use G4StrUtil::to_upper/to_upp 151 [[deprecated("Use G4StrUtil::to_upper/to_upper_copy instead")]] 152 inline void toUpper(); 152 inline void toUpper(); 153 }; 153 }; 154 154 155 /// @brief Query and manipulation functions fo 155 /// @brief Query and manipulation functions for G4String 156 // 156 // 157 /// Additional free functions that are not par 157 /// Additional free functions that are not part of the `std::string` interface as 158 /// of the minimum C++ standard supported by G 158 /// of the minimum C++ standard supported by Geant4 (currently C++17). 159 /// 159 /// 160 /// @see `G4String` 160 /// @see `G4String` 161 namespace G4StrUtil 161 namespace G4StrUtil 162 { 162 { 163 /// @brief Convert string to lowercase 163 /// @brief Convert string to lowercase 164 /// @param[in, out] str the string to lowerc 164 /// @param[in, out] str the string to lowercase 165 inline void to_lower(G4String& str); 165 inline void to_lower(G4String& str); 166 166 167 /// @brief Return lowercased copy of string 167 /// @brief Return lowercased copy of string 168 /// @param[in] str the string to lowercase 168 /// @param[in] str the string to lowercase 169 /// @return lowercased copy of `str` 169 /// @return lowercased copy of `str` 170 inline G4String to_lower_copy(G4String str); 170 inline G4String to_lower_copy(G4String str); 171 171 172 /// @brief Convert string to uppercase 172 /// @brief Convert string to uppercase 173 /// @param[in, out] str the string to upperc 173 /// @param[in, out] str the string to uppercase 174 inline void to_upper(G4String& str); 174 inline void to_upper(G4String& str); 175 175 176 /// @brief Return uppercase copy of string 176 /// @brief Return uppercase copy of string 177 /// @param[in] str the string to upper case 177 /// @param[in] str the string to upper case 178 /// @return uppercased copy of `str` 178 /// @return uppercased copy of `str` 179 inline G4String to_upper_copy(G4String str); 179 inline G4String to_upper_copy(G4String str); 180 180 181 /// @brief Remove leading characters from st 181 /// @brief Remove leading characters from string 182 /// @param[in,out] str string to strip 182 /// @param[in,out] str string to strip 183 /// @param[in] ch character to remove 183 /// @param[in] ch character to remove 184 /// @post `str` has any leading sequence of 184 /// @post `str` has any leading sequence of `ch` removed 185 void lstrip(G4String& str, char ch = ' '); 185 void lstrip(G4String& str, char ch = ' '); 186 186 187 /// @brief Remove trailing characters from s 187 /// @brief Remove trailing characters from string 188 /// @param[in,out] str string to strip 188 /// @param[in,out] str string to strip 189 /// @param[in] ch character to remove 189 /// @param[in] ch character to remove 190 /// @post `str` has any trailing sequence of 190 /// @post `str` has any trailing sequence of `ch` removed 191 void rstrip(G4String& str, char ch = ' '); 191 void rstrip(G4String& str, char ch = ' '); 192 192 193 /// @brief Remove leading and trailing chara 193 /// @brief Remove leading and trailing characters from string 194 /// @param[in,out] str string to strip 194 /// @param[in,out] str string to strip 195 /// @param[in] ch character to remove 195 /// @param[in] ch character to remove 196 /// @post `str` has any leading and trailing 196 /// @post `str` has any leading and trailing sequence of `ch` removed 197 void strip(G4String& str, char ch = ' '); 197 void strip(G4String& str, char ch = ' '); 198 198 199 /// @brief Return copy of string with leadin 199 /// @brief Return copy of string with leading characters removed 200 /// @param[in] str string to copy and strip 200 /// @param[in] str string to copy and strip 201 /// @param[in] ch character to remove 201 /// @param[in] ch character to remove 202 /// @return copy of `str` with any leading s 202 /// @return copy of `str` with any leading sequence of `ch` removed 203 G4String lstrip_copy(G4String str, char ch = 203 G4String lstrip_copy(G4String str, char ch = ' '); 204 204 205 /// @brief Return copy of string with traili 205 /// @brief Return copy of string with trailing characters removed 206 /// @param[in] str string to copy and strip 206 /// @param[in] str string to copy and strip 207 /// @param[in] ch character to remove 207 /// @param[in] ch character to remove 208 /// @return copy of `str` with any trailing 208 /// @return copy of `str` with any trailing sequence of `ch` removed 209 G4String rstrip_copy(G4String str, char ch = 209 G4String rstrip_copy(G4String str, char ch = ' '); 210 210 211 /// @brief Return copy of string with leadin 211 /// @brief Return copy of string with leading and trailing characters removed 212 /// @param[in] str string to copy and strip 212 /// @param[in] str string to copy and strip 213 /// @param[in] ch character to remove 213 /// @param[in] ch character to remove 214 /// @return copy of `str` with any leading a 214 /// @return copy of `str` with any leading and trailing sequence of `ch` removed 215 G4String strip_copy(G4String str, char ch = 215 G4String strip_copy(G4String str, char ch = ' '); 216 216 217 /// @brief Check if a string contains a give 217 /// @brief Check if a string contains a given substring 218 /// @param[in] str string to be checked 218 /// @param[in] str string to be checked 219 /// @param[in] ss substring to check for 219 /// @param[in] ss substring to check for 220 /// @retval true if `str` contains `ss` 220 /// @retval true if `str` contains `ss` 221 /// @retval false otherwise 221 /// @retval false otherwise 222 inline G4bool contains(const G4String& str, 222 inline G4bool contains(const G4String& str, std::string_view ss); 223 223 224 /// @overload 224 /// @overload 225 inline G4bool contains(const G4String& str, 225 inline G4bool contains(const G4String& str, char ss); 226 226 227 /// @overload 227 /// @overload 228 inline G4bool contains(const G4String& str, 228 inline G4bool contains(const G4String& str, const char* ss); 229 229 230 /// @overload 230 /// @overload 231 /// @note this overload is required to resol 231 /// @note this overload is required to resolve ambiguity between the 232 /// signatures taking `std::string_view` a 232 /// signatures taking `std::string_view` and `const char*` substring 233 /// arguments. G4String currently provides 233 /// arguments. G4String currently provides implicit conversion to 234 /// `const char*`, which makes the calls a 234 /// `const char*`, which makes the calls ambiguous due to `std::string`'s 235 /// implicit conversion to `std::string_vi 235 /// implicit conversion to `std::string_view` 236 inline G4bool contains(const G4String& str, 236 inline G4bool contains(const G4String& str, const G4String& ss); 237 237 238 /// @brief Case insensitive comparison of tw 238 /// @brief Case insensitive comparison of two strings 239 /// 239 /// 240 /// Converts both input arguments to lower c 240 /// Converts both input arguments to lower case and returns the 241 /// result of `std::string::compare` with th 241 /// result of `std::string::compare` with these values. 242 /// 242 /// 243 /// @param[in] lhs the first string in the c 243 /// @param[in] lhs the first string in the comparison 244 /// @param[in] rhs the second string in the 244 /// @param[in] rhs the second string in the comparison 245 /// @return negative(positive) `G4int` if lo 245 /// @return negative(positive) `G4int` if lowercased `lhs` appears 246 /// before(after) lowercased `rhs` in lexi 246 /// before(after) lowercased `rhs` in lexicographical order, zero if both 247 /// compare equivalent after lowercasing. 247 /// compare equivalent after lowercasing. 248 inline G4int icompare(std::string_view lhs, 248 inline G4int icompare(std::string_view lhs, std::string_view rhs); 249 249 250 /// @brief Return true if a string starts wi 250 /// @brief Return true if a string starts with a given prefix 251 /// @param[in] str string to be checked 251 /// @param[in] str string to be checked 252 /// @param[in] ss prefix to check for 252 /// @param[in] ss prefix to check for 253 /// @retval true if `str` starts with `ss` 253 /// @retval true if `str` starts with `ss` 254 /// @retval false otherwise 254 /// @retval false otherwise 255 inline bool starts_with(const G4String& str, 255 inline bool starts_with(const G4String& str, std::string_view ss); 256 256 257 /// @overload 257 /// @overload 258 inline bool starts_with(const G4String& str, 258 inline bool starts_with(const G4String& str, G4String::value_type ss); 259 259 260 /// @overload 260 /// @overload 261 inline bool starts_with(const G4String& str, 261 inline bool starts_with(const G4String& str, const char* ss); 262 262 263 /// @overload 263 /// @overload 264 /// @note this overload is required to resol 264 /// @note this overload is required to resolve ambiguity between the 265 /// signatures taking `std::string_view` a 265 /// signatures taking `std::string_view` and `const char*` substring 266 /// arguments. G4String currently provides 266 /// arguments. G4String currently provides implicit conversion to 267 /// `const char*`, which makes the calls a 267 /// `const char*`, which makes the calls ambiguous due to `std::string`'s 268 /// implicit conversion to `std::string_vi 268 /// implicit conversion to `std::string_view` 269 inline bool starts_with(const G4String& str, 269 inline bool starts_with(const G4String& str, const G4String& ss); 270 270 271 /// @brief Return true if a string ends with 271 /// @brief Return true if a string ends with a given suffix 272 /// @param[in] str string to be checked 272 /// @param[in] str string to be checked 273 /// @param[in] ss suffix to check for 273 /// @param[in] ss suffix to check for 274 /// @retval true if `str` ends with `ss` 274 /// @retval true if `str` ends with `ss` 275 /// @retval false otherwise 275 /// @retval false otherwise 276 inline bool ends_with(const G4String& str, s 276 inline bool ends_with(const G4String& str, std::string_view ss); 277 277 278 /// @overload 278 /// @overload 279 inline bool ends_with(const G4String& str, G 279 inline bool ends_with(const G4String& str, G4String::value_type ss); 280 280 281 /// @overload 281 /// @overload 282 inline bool ends_with(const G4String& str, c 282 inline bool ends_with(const G4String& str, const char* ss); 283 283 284 /// @overload 284 /// @overload 285 inline bool ends_with(const G4String& str, c 285 inline bool ends_with(const G4String& str, const G4String& ss); 286 286 287 /// @brief Remove specified in-range charact 287 /// @brief Remove specified in-range characters from string 288 /// 288 /// 289 /// Equivalent to `std::string::erase(index, 289 /// Equivalent to `std::string::erase(index, count)` with erasure only occuring 290 /// if `index <= size()`. When `index > size 290 /// if `index <= size()`. When `index > size()` the string is left unmodified. 291 /// 291 /// 292 /// @deprecated It is strongly recommended t 292 /// @deprecated It is strongly recommended to use `std::string::erase` if the 293 /// start index is already checked, or oth 293 /// start index is already checked, or otherwise known, to be in range. Otherwise, 294 /// implement the index-size comparison in 294 /// implement the index-size comparison instead of using this function. 295 /// 295 /// 296 /// @param[in,out] str string to erase chara 296 /// @param[in,out] str string to erase characters from 297 /// @param[in] index position to start remov 297 /// @param[in] index position to start removal from 298 /// @param[in] count number of characters to 298 /// @param[in] count number of characters to remove 299 /// @post `str` is unchanged if `index > str 299 /// @post `str` is unchanged if `index > str.size()`, otherwise `str` has 300 /// `min(count, str.size() - index)` chara 300 /// `min(count, str.size() - index)` characters removed starting at `index` 301 inline void safe_erase(G4String& str, G4Stri 301 inline void safe_erase(G4String& str, G4String::size_type index = 0, 302 G4String::size_type c 302 G4String::size_type count = G4String::npos); 303 303 304 /// @brief Read characters into a G4String f 304 /// @brief Read characters into a G4String from an input stream until end-of-line 305 /// 305 /// 306 /// @deprecated It is strongly recommended t 306 /// @deprecated It is strongly recommended to use `std::getline` instead of this 307 /// function, plus `G4StrUtil::lstrip` if 307 /// function, plus `G4StrUtil::lstrip` if leading whitespace removal is required. 308 /// 308 /// 309 /// @param[in] is input stream to read from 309 /// @param[in] is input stream to read from 310 /// @param[in,out] str string to read into 310 /// @param[in,out] str string to read into 311 /// @param[in] skipWhite if true, discard le 311 /// @param[in] skipWhite if true, discard leading whitespace from `is` 312 /// @return `is` 312 /// @return `is` 313 inline std::istream& readline(std::istream& 313 inline std::istream& readline(std::istream& is, G4String& str, G4bool skipWhite = true); 314 } // namespace G4StrUtil 314 } // namespace G4StrUtil 315 315 316 #include "G4String.icc" 316 #include "G4String.icc" 317 317 318 #endif 318 #endif 319 319