Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:08

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 // G4String
0027 //
0028 // Class description:
0029 //
0030 // Common string type for Geant4
0031 //
0032 // Provides a `std::string` compliant implementation of basic
0033 // strings. It currently inherits from `std::string`, but this should
0034 // not be assumed other than that it will implement the same interface
0035 // as `std::string` as of the minimum C++ standard required by Geant4
0036 // (currently C++17).
0037 //
0038 // It can be passed to any function accepting `std::string` or `std::string_view` 
0039 // arguments. Whilst it currently implements a conversion operator to `const char*`,
0040 // this should be considered deprecated for use outside of Geant4. Passing to
0041 // non-Geant4 functions taking `const char*` arguments should use the
0042 // `std::string::c_str` member function to explicitly convert.
0043 // 
0044 // See `std::string` for primary interfaces, `G4StrUtil` for additional query/manipulation functions
0045 //
0046 // Author: G.Cosmo, 11 November 1999
0047 //---------------------------------------------------------------------
0048 
0049 #ifndef G4String_hh
0050 #define G4String_hh 1
0051 
0052 #include "G4Types.hh"
0053 
0054 #include <algorithm>
0055 #include <cstdio>
0056 #include <cstring>
0057 #include <iostream>
0058 #include <string>
0059 #include <string_view>
0060 
0061 class G4String : public std::string
0062 {
0063  public:
0064   /// @brief
0065   /// @deprecated Will be removed in future release
0066   enum caseCompare
0067   {
0068     exact,
0069     ignoreCase
0070   };
0071 
0072   /// @brief
0073   /// @deprecated Will be removed in future release
0074   enum stripType
0075   {
0076     leading,
0077     trailing,
0078     both
0079   };
0080 
0081   using std::string::string;
0082   using std::string::operator=;
0083 
0084   inline G4String() = default;
0085   inline G4String(const std::string&);
0086   inline G4String(const G4String&);
0087   inline G4String(std::string&&);
0088   inline G4String(G4String&&);
0089   inline G4String& operator=(const G4String&);
0090   inline G4String& operator=(G4String&&);
0091 
0092   /// @brief Implicitly convert to `const char*`
0093   /// @deprecated Will be removed in future releases for `std::string` compliance
0094   ///   If passing `G4String` to functions requiring `const char*`, use `std::string::c_str`
0095   ///   to explicitly convert. `G4String` also implicitly converts to `std::string_view`
0096   ///   to match the `std::string` interface.
0097   inline operator const char*() const;
0098 
0099   /// @brief Override of subscript operator for `int` to suppress C2666 errors with MSVC
0100   /// @deprecated Will be removed at the same time as `operator const char*` that requires it
0101   ///
0102   /// This override is required because of G4String's provision of an implicit conversion
0103   /// operator to `const char*`. Together with the subscript operator and C++'s built-in
0104   /// `operator[](const char*, int) operator, use of G4String::operator[] will trigger
0105   /// [MSVC error C2666](https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c2666?view=msvc-170)
0106   /// This is a known issue with mixing implicit conversion to `const char*` and subscript
0107   /// operators. Provision of the override with `int` argument is thus a workaround
0108   /// until the conversion operator is removed.
0109   inline reference operator[](int);
0110 
0111   /// @overload  
0112   inline const_reference operator[](int) const;
0113 
0114   /// @brief Deprecated function
0115   /// @deprecated Use `std::string::compare` or `G4StrUtil::icompare` instead
0116   [[deprecated("Use std::string::compare, or G4StrUtil::icompare for case-insensitive comparison")]]
0117   inline G4int compareTo(std::string_view, caseCompare mode = exact) const;
0118 
0119   /// @brief Deprecated function
0120   /// @deprecated Use `std::getline` plus `G4StrUtil::lstrip` instead
0121   [[deprecated("Use std::getline instead, plus G4StrUtil::lstrip if required")]]
0122   inline std::istream& readLine(std::istream&, G4bool skipWhite = true);
0123   
0124   /// @brief Deprecated function
0125   /// @deprecated Use `std::string::erase` instead
0126   [[deprecated("Use std::string::erase instead")]]
0127   inline G4String& remove(size_type);
0128 
0129   /// @brief Deprecated function
0130   /// @deprecated Use `G4StrUtil::contains` instead
0131   [[deprecated("Use G4StrUtil::contains instead")]]
0132   inline G4bool contains(const std::string&) const;
0133 
0134   /// @brief Deprecated function
0135   /// @deprecated Use `G4StrUtil::contains` instead 
0136   [[deprecated("Use G4StrUtil::contains instead")]]
0137   inline G4bool contains(char) const;
0138 
0139   /// @brief Deprecated function
0140   /// @deprecated Use `G4StrUtil` functions instead
0141   [[deprecated("Use G4StrUtil::{lstrip,rstrip,strip}_copy instead")]]
0142   [[nodiscard]] inline G4String strip(stripType strip_Type = trailing, char ch = ' ');
0143 
0144   /// @brief Deprecated function
0145   /// @deprecated Use `G4StrUtil` functions instead
0146   [[deprecated("Use G4StrUtil::to_lower/to_lower_copy instead")]]
0147   inline void toLower();
0148 
0149   /// @brief Deprecated function
0150   /// @deprecated Use `G4StrUtil` functions instead
0151   [[deprecated("Use G4StrUtil::to_upper/to_upper_copy instead")]]
0152   inline void toUpper();
0153 };
0154 
0155 /// @brief Query and manipulation functions for G4String
0156 //
0157 /// Additional free functions that are not part of the `std::string` interface as
0158 /// of the minimum C++ standard supported by Geant4 (currently C++17).
0159 ///
0160 /// @see `G4String`
0161 namespace G4StrUtil
0162 {
0163   /// @brief Convert string to lowercase
0164   /// @param[in, out] str the string to lowercase
0165   inline void to_lower(G4String& str);
0166 
0167   /// @brief Return lowercased copy of string
0168   /// @param[in] str the string to lowercase
0169   /// @return lowercased copy of `str`
0170   inline G4String to_lower_copy(G4String str);
0171 
0172   /// @brief Convert string to uppercase
0173   /// @param[in, out] str the string to uppercase
0174   inline void to_upper(G4String& str);
0175 
0176   /// @brief Return uppercase copy of string
0177   /// @param[in] str the string to upper case
0178   /// @return uppercased copy of `str`
0179   inline G4String to_upper_copy(G4String str);
0180 
0181   /// @brief Remove leading characters from string
0182   /// @param[in,out] str string to strip
0183   /// @param[in] ch character to remove
0184   /// @post `str` has any leading sequence of `ch` removed
0185   void lstrip(G4String& str, char ch = ' ');
0186 
0187   /// @brief Remove trailing characters from string
0188   /// @param[in,out] str string to strip
0189   /// @param[in] ch character to remove
0190   /// @post `str` has any trailing sequence of `ch` removed
0191   void rstrip(G4String& str, char ch = ' ');
0192 
0193   /// @brief Remove leading and trailing characters from string
0194   /// @param[in,out] str string to strip
0195   /// @param[in] ch character to remove
0196   /// @post `str` has any leading and trailing sequence of `ch` removed
0197   void strip(G4String& str, char ch = ' ');
0198 
0199   /// @brief Return copy of string with leading characters removed
0200   /// @param[in] str string to copy and strip
0201   /// @param[in] ch character to remove
0202   /// @return copy of `str` with any leading sequence of `ch` removed
0203   G4String lstrip_copy(G4String str, char ch = ' ');
0204 
0205   /// @brief Return copy of string with trailing characters removed
0206   /// @param[in] str string to copy and strip
0207   /// @param[in] ch character to remove
0208   /// @return copy of `str` with any trailing sequence of `ch` removed
0209   G4String rstrip_copy(G4String str, char ch = ' ');
0210 
0211   /// @brief Return copy of string with leading and trailing characters removed
0212   /// @param[in] str string to copy and strip
0213   /// @param[in] ch character to remove
0214   /// @return copy of `str` with any leading and trailing sequence of `ch` removed
0215   G4String strip_copy(G4String str, char ch = ' ');
0216 
0217   /// @brief Check if a string contains a given substring
0218   /// @param[in] str string to be checked
0219   /// @param[in] ss substring to check for
0220   /// @retval true if `str` contains `ss`
0221   /// @retval false otherwise
0222   inline G4bool contains(const G4String& str, std::string_view ss);
0223 
0224   /// @overload
0225   inline G4bool contains(const G4String& str, char ss);
0226 
0227   /// @overload
0228   inline G4bool contains(const G4String& str, const char* ss);
0229 
0230   /// @overload
0231   /// @note this overload is required to resolve ambiguity between the
0232   ///   signatures taking `std::string_view` and `const char*` substring
0233   ///   arguments. G4String currently provides implicit conversion to
0234   ///   `const char*`, which makes the calls ambiguous due to `std::string`'s
0235   ///   implicit conversion to `std::string_view`
0236   inline G4bool contains(const G4String& str, const G4String& ss);
0237 
0238   /// @brief Case insensitive comparison of two strings
0239   ///
0240   /// Converts both input arguments to lower case and returns the
0241   /// result of `std::string::compare` with these values.
0242   ///
0243   /// @param[in] lhs the first string in the comparison
0244   /// @param[in] rhs the second string in the comparison
0245   /// @return negative(positive) `G4int` if lowercased `lhs` appears
0246   ///   before(after) lowercased `rhs` in lexicographical order, zero if both
0247   ///   compare equivalent after lowercasing.
0248   inline G4int icompare(std::string_view lhs, std::string_view rhs);
0249 
0250   /// @brief Return true if a string starts with a given prefix
0251   /// @param[in] str string to be checked
0252   /// @param[in] ss prefix to check for
0253   /// @retval true if `str` starts with `ss`
0254   /// @retval false otherwise
0255   inline bool starts_with(const G4String& str, std::string_view ss);
0256 
0257   /// @overload
0258   inline bool starts_with(const G4String& str, G4String::value_type ss);
0259 
0260   /// @overload
0261   inline bool starts_with(const G4String& str, const char* ss);
0262 
0263   /// @overload
0264   /// @note this overload is required to resolve ambiguity between the
0265   ///   signatures taking `std::string_view` and `const char*` substring
0266   ///   arguments. G4String currently provides implicit conversion to
0267   ///   `const char*`, which makes the calls ambiguous due to `std::string`'s
0268   ///   implicit conversion to `std::string_view`
0269   inline bool starts_with(const G4String& str, const G4String& ss);
0270 
0271   /// @brief Return true if a string ends with a given suffix
0272   /// @param[in] str string to be checked
0273   /// @param[in] ss suffix to check for
0274   /// @retval true if `str` ends with `ss`
0275   /// @retval false otherwise
0276   inline bool ends_with(const G4String& str, std::string_view ss);
0277 
0278   /// @overload
0279   inline bool ends_with(const G4String& str, G4String::value_type ss);
0280 
0281   /// @overload
0282   inline bool ends_with(const G4String& str, const char* ss);
0283 
0284   /// @overload
0285   inline bool ends_with(const G4String& str, const G4String& ss);
0286 
0287   /// @brief Remove specified in-range characters from string
0288   /// 
0289   /// Equivalent to `std::string::erase(index, count)` with erasure only occuring
0290   /// if `index <= size()`. When `index > size()` the string is left unmodified.
0291   ///
0292   /// @deprecated It is strongly recommended to use `std::string::erase` if the
0293   ///   start index is already checked, or otherwise known, to be in range. Otherwise,
0294   ///   implement the index-size comparison instead of using this function.
0295   ///
0296   /// @param[in,out] str string to erase characters from
0297   /// @param[in] index position to start removal from
0298   /// @param[in] count number of characters to remove
0299   /// @post `str` is unchanged if `index > str.size()`, otherwise `str` has
0300   ///   `min(count, str.size() - index)` characters removed starting at `index`
0301   inline void safe_erase(G4String& str, G4String::size_type index = 0,
0302                          G4String::size_type count = G4String::npos);
0303 
0304   /// @brief Read characters into a G4String from an input stream until end-of-line
0305   ///
0306   /// @deprecated It is strongly recommended to use `std::getline` instead of this
0307   ///   function, plus `G4StrUtil::lstrip` if leading whitespace removal is required. 
0308   /// 
0309   /// @param[in] is input stream to read from
0310   /// @param[in,out] str string to read into
0311   /// @param[in] skipWhite if true, discard leading whitespace from `is`
0312   /// @return `is`
0313   inline std::istream& readline(std::istream& is, G4String& str, G4bool skipWhite = true);
0314 }  // namespace G4StrUtil
0315 
0316 #include "G4String.icc"
0317 
0318 #endif