Geant4-11
G4String.hh
Go to the documentation of this file.
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
27//
28// Class description:
29//
30// Common string type for Geant4
31//
32// Provides a `std::string` compliant implementation of basic
33// strings. It currently inherits from `std::string`, but this should
34// not be assumed other than that it will implement the same interface
35// as `std::string` as of the minimum C++ standard required by Geant4
36// (currently C++17).
37//
38// It can be passed to any function accepting `std::string` or `std::string_view`
39// arguments. Whilst it currently implements a conversion operator to `const char*`,
40// this should be considered deprecated for use outside of Geant4. Passing to
41// non-Geant4 functions taking `const char*` arguments should use the
42// `std::string::c_str` member function to explicitly convert.
43//
44// See `std::string` for primary interfaces, `G4StrUtil` for additional query/manipulation functions
45//
46// Author: G.Cosmo, 11 November 1999
47//---------------------------------------------------------------------
48
49#ifndef G4String_hh
50#define G4String_hh 1
51
52#include <algorithm>
53#include <cstring>
54#include <iostream>
55#include <stdio.h>
56#include <string>
57#include <string_view>
58
59#include "G4Types.hh"
60
61class G4String : public std::string
62{
63 public:
67 {
70 };
71
75 {
78 both
79 };
80
81 using std::string::string;
82 using std::string::operator=;
83
84 inline G4String() = default;
85 inline G4String(const std::string&);
86 inline G4String(const G4String&);
87 inline G4String(std::string&&);
88 inline G4String(G4String&&);
89 inline G4String& operator=(const G4String&);
91
97 inline operator const char*() const;
98
109 inline reference operator[](int);
110
112 inline const_reference operator[](int) const;
113
116 [[deprecated("Use std::string::compare, or G4StrUtil::icompare for case-insensitive comparison")]]
117 inline G4int compareTo(std::string_view, caseCompare mode = exact) const;
118
121 [[deprecated("Use std::getline instead, plus G4StrUtil::lstrip if required")]]
122 inline std::istream& readLine(std::istream&, G4bool skipWhite = true);
123
126 [[deprecated("Use std::string::erase instead")]]
127 inline G4String& remove(size_type);
128
131 [[deprecated("Use G4StrUtil::contains instead")]]
132 inline G4bool contains(const std::string&) const;
133
136 [[deprecated("Use G4StrUtil::contains instead")]]
137 inline G4bool contains(char) const;
138
141 [[deprecated("Use G4StrUtil::{lstrip,rstrip,strip}_copy instead")]]
142 [[nodiscard]] inline G4String strip(stripType strip_Type = trailing, char c = ' ');
143
146 [[deprecated("Use G4StrUtil::to_lower/to_lower_copy instead")]]
147 inline void toLower();
148
151 [[deprecated("Use G4StrUtil::to_upper/to_upper_copy instead")]]
152 inline void toUpper();
153};
154
156//
161namespace G4StrUtil
162{
165 inline void to_lower(G4String& str);
166
171
174 inline void to_upper(G4String& str);
175
180
185 void lstrip(G4String& str, char c = ' ');
186
191 void rstrip(G4String& str, char c = ' ');
192
197 void strip(G4String& str, char c = ' ');
198
203 G4String lstrip_copy(G4String str, char c = ' ');
204
209 G4String rstrip_copy(G4String str, char c = ' ');
210
215 G4String strip_copy(G4String str, char c = ' ');
216
222 inline G4bool contains(const G4String& str, std::string_view ss);
223
225 inline G4bool contains(const G4String& str, char ss);
226
228 inline G4bool contains(const G4String& str, const char* ss);
229
236 inline G4bool contains(const G4String& str, const G4String& ss);
237
248 inline G4int icompare(std::string_view lhs, std::string_view rhs);
249
255 inline bool starts_with(const G4String& str, std::string_view ss);
256
258 inline bool starts_with(const G4String& str, G4String::value_type ss);
259
261 inline bool starts_with(const G4String& str, const char* ss);
262
269 inline bool starts_with(const G4String& str, const G4String& ss);
270
276 inline bool ends_with(const G4String& str, std::string_view ss);
277
279 inline bool ends_with(const G4String& str, G4String::value_type ss);
280
282 inline bool ends_with(const G4String& str, const char* ss);
283
285 inline bool ends_with(const G4String& str, const G4String& ss);
286
301 inline void safe_erase(G4String& str, G4String::size_type index = 0,
302 G4String::size_type count = G4String::npos);
303
313 inline std::istream& readline(std::istream& is, G4String& str, G4bool skipWhite = true);
314} // namespace G4StrUtil
315
316#include "G4String.icc"
317
318#endif
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
G4int compareTo(std::string_view, caseCompare mode=exact) const
Deprecated function.
G4String(G4String &&)
const_reference operator[](int) const
@ ignoreCase
Definition: G4String.hh:69
@ trailing
Definition: G4String.hh:77
@ leading
Definition: G4String.hh:76
G4bool contains(char) const
Deprecated function.
G4String(std::string &&)
G4String & operator=(const G4String &)
G4bool contains(const std::string &) const
Deprecated function.
void toUpper()
Deprecated function.
G4String(const G4String &)
void toLower()
Deprecated function.
G4String strip(stripType strip_Type=trailing, char c=' ')
Deprecated function.
std::istream & readLine(std::istream &, G4bool skipWhite=true)
Deprecated function.
G4String(const std::string &)
G4String & remove(size_type)
Deprecated function.
G4String & operator=(G4String &&)
G4String()=default
reference operator[](int)
Override of subscript operator for int to suppress C2666 errors with MSVC.
Query and manipulation functions for G4String.
Definition: G4String.hh:162
G4String to_lower_copy(G4String str)
Return lowercased copy of string.
G4String strip_copy(G4String str, char c=' ')
Return copy of string with leading and trailing characters removed.
G4int icompare(std::string_view lhs, std::string_view rhs)
Case insensitive comparison of two strings.
void lstrip(G4String &str, char c=' ')
Remove leading characters from string.
G4String lstrip_copy(G4String str, char c=' ')
Return copy of string with leading characters removed.
void to_upper(G4String &str)
Convert string to uppercase.
void safe_erase(G4String &str, G4String::size_type index=0, G4String::size_type count=G4String::npos)
Remove specified in-range characters from string.
G4String rstrip_copy(G4String str, char c=' ')
Return copy of string with trailing characters removed.
void strip(G4String &str, char c=' ')
Remove leading and trailing characters from string.
G4bool contains(const G4String &str, std::string_view ss)
Check if a string contains a given substring.
G4String to_upper_copy(G4String str)
Return uppercase copy of string.
void to_lower(G4String &str)
Convert string to lowercase.
void rstrip(G4String &str, char c=' ')
Remove trailing characters from string.
bool ends_with(const G4String &str, std::string_view ss)
Return true if a string ends with a given suffix.
std::istream & readline(std::istream &is, G4String &str, G4bool skipWhite=true)
Read characters into a G4String from an input stream until end-of-line.
bool starts_with(const G4String &str, std::string_view ss)
Return true if a string starts with a given prefix.