OpenMS  3.0.0
String.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2022.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
38 #include <OpenMS/OpenMSConfig.h>
39 
40 #include <string>
41 #include <cstring>
42 #include <vector>
43 
44 class QString;
45 
46 namespace OpenMS
47 {
48  class DataValue;
49  template <typename FloatingPointType>
50  struct PrecisionWrapper;
58  class String :
59  public std::string
60  {
61 public:
62 
64  OPENMS_DLLAPI static const String EMPTY;
65 
70  typedef iterator Iterator;
72  typedef const_iterator ConstIterator;
74  typedef reverse_iterator ReverseIterator;
76  typedef const_reverse_iterator ConstReverseIterator;
78  typedef size_type SizeType;
79 
82 
84 
89  OPENMS_DLLAPI String();
91  OPENMS_DLLAPI String(const String&) = default;
93  OPENMS_DLLAPI String(String&&) = default;
95  OPENMS_DLLAPI String(const std::string& s);
97  OPENMS_DLLAPI String(const std::string_view& sv);
99  OPENMS_DLLAPI String(const QString& s);
101  OPENMS_DLLAPI String(const char* s);
103  OPENMS_DLLAPI String(const char c);
105  OPENMS_DLLAPI String(const char* s, SizeType length);
107  OPENMS_DLLAPI String(size_t len, char c);
109  template <class InputIterator>
110  String(InputIterator first, InputIterator last) :
111  std::string(first, last)
112  {
113  }
114 
116  OPENMS_DLLAPI String(int i);
118  OPENMS_DLLAPI String(unsigned int i);
120  OPENMS_DLLAPI String(short int i);
122  OPENMS_DLLAPI String(short unsigned int i);
124  OPENMS_DLLAPI String(long int i);
126  OPENMS_DLLAPI String(long unsigned int i);
128  OPENMS_DLLAPI String(long long unsigned int i);
130  OPENMS_DLLAPI String(long long signed int i);
132  OPENMS_DLLAPI String(float f, bool full_precision = true);
134  OPENMS_DLLAPI String(double d, bool full_precision = true);
136  OPENMS_DLLAPI String(long double ld, bool full_precision = true);
138  OPENMS_DLLAPI String(const DataValue& d, bool full_precision = true);
139 
141 
146  OPENMS_DLLAPI bool hasPrefix(const String& string) const;
147 
149  OPENMS_DLLAPI bool hasSuffix(const String& string) const;
150 
152  OPENMS_DLLAPI bool hasSubstring(const String& string) const;
153 
155  OPENMS_DLLAPI bool has(Byte byte) const;
157 
159  OPENMS_DLLAPI String& operator=(const String&) = default;
161  OPENMS_DLLAPI String& operator=(String&&) & = default;
162 
171  OPENMS_DLLAPI String prefix(SizeType length) const;
172 
178  OPENMS_DLLAPI String suffix(SizeType length) const;
179 
186  OPENMS_DLLAPI String prefix(Int length) const;
187 
194  OPENMS_DLLAPI String suffix(Int length) const;
195 
201  OPENMS_DLLAPI String prefix(char delim) const;
202 
208  OPENMS_DLLAPI String suffix(char delim) const;
209 
222  OPENMS_DLLAPI String substr(size_t pos = 0, size_t n = npos) const;
223 
231  OPENMS_DLLAPI String chop(Size n) const;
232 
234 
235 
243  OPENMS_DLLAPI String& reverse();
244 
246  OPENMS_DLLAPI String& trim();
247 
255  OPENMS_DLLAPI String& quote(char q = '"', QuotingMethod method = ESCAPE);
256 
266  OPENMS_DLLAPI String& unquote(char q = '"', QuotingMethod method = ESCAPE);
267 
269  OPENMS_DLLAPI String& simplify();
270 
272  OPENMS_DLLAPI String& fillLeft(char c, UInt size);
273 
275  OPENMS_DLLAPI String& fillRight(char c, UInt size);
276 
278  OPENMS_DLLAPI String& toUpper();
279 
281  OPENMS_DLLAPI String& toLower();
282 
284  OPENMS_DLLAPI String& firstToUpper();
285 
287  OPENMS_DLLAPI String& substitute(char from, char to);
288 
290  OPENMS_DLLAPI String& substitute(const String& from, const String& to);
291 
293  OPENMS_DLLAPI String& remove(char what);
294 
296  OPENMS_DLLAPI String& ensureLastChar(char end);
297 
299  OPENMS_DLLAPI String& removeWhitespaces();
301 
305 
314  OPENMS_DLLAPI Int toInt() const;
315 
321  OPENMS_DLLAPI float toFloat() const;
322 
328  OPENMS_DLLAPI double toDouble() const;
329 
331  OPENMS_DLLAPI QString toQString() const;
332 
334 
339  OPENMS_DLLAPI String operator+(int i) const;
341  OPENMS_DLLAPI String operator+(unsigned int i) const;
343  OPENMS_DLLAPI String operator+(short int i) const;
345  OPENMS_DLLAPI String operator+(short unsigned int i) const;
347  OPENMS_DLLAPI String operator+(long int i) const;
349  OPENMS_DLLAPI String operator+(long unsigned int i) const;
351  OPENMS_DLLAPI String operator+(long long unsigned int i) const;
353  OPENMS_DLLAPI String operator+(float f) const;
355  OPENMS_DLLAPI String operator+(double d) const;
357  OPENMS_DLLAPI String operator+(long double ld) const;
359  OPENMS_DLLAPI String operator+(char c) const;
361  OPENMS_DLLAPI String operator+(const char* s) const;
363  OPENMS_DLLAPI String operator+(const String& s) const;
365  OPENMS_DLLAPI String operator+(const std::string& s) const;
367 
372  OPENMS_DLLAPI String& operator+=(int i);
374  OPENMS_DLLAPI String& operator+=(unsigned int i);
376  OPENMS_DLLAPI String& operator+=(short int i);
378  OPENMS_DLLAPI String& operator+=(short unsigned int i);
380  OPENMS_DLLAPI String& operator+=(long int i);
382  OPENMS_DLLAPI String& operator+=(long unsigned int i);
384  OPENMS_DLLAPI String& operator+=(long long unsigned int i);
386  OPENMS_DLLAPI String& operator+=(float f);
388  OPENMS_DLLAPI String& operator+=(double d);
390  OPENMS_DLLAPI String& operator+=(long double d);
392  OPENMS_DLLAPI String& operator+=(char c);
394  OPENMS_DLLAPI String& operator+=(const char* s);
396  OPENMS_DLLAPI String& operator+=(const String& s);
398  OPENMS_DLLAPI String& operator+=(const std::string& s);
400 
402  OPENMS_DLLAPI static String random(UInt length);
403 
405  OPENMS_DLLAPI static String number(double d, UInt n);
406 
412  OPENMS_DLLAPI static String numberLength(double d, UInt n);
413 
430  OPENMS_DLLAPI bool split(const char splitter, std::vector<String>& substrings, bool quote_protect = false) const;
431 
443  OPENMS_DLLAPI bool split(const String& splitter, std::vector<String>& substrings) const;
444 
459  OPENMS_DLLAPI bool split_quoted(const String& splitter, std::vector<String>& substrings,
460  char q = '"', QuotingMethod method = ESCAPE) const;
461 
467  template <class StringIterator>
468  void concatenate(StringIterator first, StringIterator last, const String& glue = "")
469  {
470  //empty container
471  if (first == last)
472  {
473  std::string::clear();
474  return;
475  }
476 
477  std::string::operator=(*first);
478  for (StringIterator it = ++first; it != last; ++it)
479  {
480  std::string::operator+=(glue + (*it));
481  }
482  }
483  };
484  OPENMS_DLLAPI ::size_t hash_value(OpenMS::String const& s);
485 } // namespace OpenMS
486 
487 namespace std
488 {
489  template <> struct hash<OpenMS::String> //hash for String
490  {
491  std::size_t operator()( OpenMS::String const& s) const
492  {
493  return std::hash<string>()(static_cast<string>(s));
494  }
495  };
496 } // namespace std
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:59
A more convenient string class.
Definition: String.h:60
String(long long unsigned int i)
Constructor from an unsigned integer.
String & substitute(const String &from, const String &to)
Replaces all occurrences of the string from by the string to.
String & operator+=(int i)
Sum operator for an integer.
const_reverse_iterator ConstReverseIterator
Const reverse Iterator.
Definition: String.h:76
String & fillRight(char c, UInt size)
Adds c on the right side until the size of the string is size.
String substr(size_t pos=0, size_t n=npos) const
Wrapper for the STL substr() method. Returns a String object with its contents initialized to a subst...
String(float f, bool full_precision=true)
Constructor from float (full_precision controls number of fractional digits, 3 digits when false,...
String & operator+=(short unsigned int i)
Sum operator for an unsigned integer.
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
bool split_quoted(const String &splitter, std::vector< String > &substrings, char q='"', QuotingMethod method = ESCAPE) const
Splits a string into substrings using splitter (the whole string) as delimiter, but does not split wi...
String & simplify()
merges subsequent whitespaces to one blank character
String(long int i)
Constructor from an integer.
String chop(Size n) const
Returns a substring where n characters were removed from the end of the string.
String(const std::string_view &sv)
Constructor from std::string_view.
bool hasSubstring(const String &string) const
true if String contains the string, false otherwise
String(const DataValue &d, bool full_precision=true)
Constructor from DataValue (full_precision controls number of fractional digits for all double types ...
reverse_iterator ReverseIterator
Reverse Iterator.
Definition: String.h:74
String & remove(char what)
Remove all occurrences of the character what.
String & operator+=(long int i)
Sum operator for an integer.
String & operator+=(const char *s)
Sum operator for char*.
String & operator+=(double d)
Sum operator for double.
String prefix(SizeType length) const
returns the prefix of length length
String & firstToUpper()
Converts the first letter of the string to uppercase.
String prefix(char delim) const
returns the prefix up to the first occurrence of char delim (excluding it)
iterator Iterator
Iterator.
Definition: String.h:70
bool has(Byte byte) const
true if String contains the byte, false otherwise
Int toInt() const
Conversion to int.
String(const char c)
Constructor from a char.
String & operator=(const String &)=default
Assignment operator.
String & operator+=(long unsigned int i)
Sum operator for an unsigned integer.
String & operator+=(unsigned int i)
Sum operator for an unsigned integer.
String(long long signed int i)
Constructor from an unsigned integer.
double toDouble() const
Conversion to double.
String(const std::string &s)
Constructor from std::string.
String & removeWhitespaces()
removes whitespaces (space, tab, line feed, carriage return)
String(int i)
Constructor from an integer.
QString toQString() const
Conversion to Qt QString.
String & reverse()
inverts the direction of the string
String operator+(char c) const
Sum operator for char.
String operator+(int i) const
Sum operator for an integer.
String operator+(long int i) const
Sum operator for an integer.
const_iterator ConstIterator
Const Iterator.
Definition: String.h:72
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:81
@ ESCAPE
Definition: String.h:81
@ DOUBLE
Definition: String.h:81
@ NONE
Definition: String.h:81
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
String(const char *s, SizeType length)
Constructor from char* (only length characters)
String & unquote(char q='"', QuotingMethod method = ESCAPE)
Reverses changes made by the quote method.
String & operator+=(short int i)
Sum operator for an integer.
String operator+(const char *s) const
Sum operator for char*.
String operator+(short unsigned int i) const
Sum operator for an unsigned integer.
String(short unsigned int i)
Constructor from an unsigned integer.
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
String(const char *s)
Constructor from char*.
String operator+(double d) const
Sum operator for double.
String()
Default constructor.
String(String &&)=default
Move constructor.
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
String & toLower()
Converts the string to lowercase.
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
static String numberLength(double d, UInt n)
Returns a string with at maximum n characters for d.
String operator+(float f) const
Sum operator for float.
String & operator+=(char c)
Sum operator for char.
String & fillLeft(char c, UInt size)
Adds c on the left side until the size of the string is size.
String & operator+=(long double d)
Sum operator for long double.
String & operator+=(float f)
Sum operator for float.
String(const String &)=default
Copy constructor.
String operator+(const std::string &s) const
Sum operator for std::string.
String & quote(char q='"', QuotingMethod method = ESCAPE)
Wraps the string in quotation marks.
size_type SizeType
UInt type.
Definition: String.h:78
String prefix(Int length) const
returns the prefix of length length
String operator+(long unsigned int i) const
Sum operator for an unsigned integer.
String suffix(char delim) const
returns the suffix up to the last occurrence of char delim (excluding it)
String(double d, bool full_precision=true)
Constructor from double (full_precision controls number of fractional digits, 3 digits when false,...
String(size_t len, char c)
Constructor from char (repeats the char len times)
String(InputIterator first, InputIterator last)
Constructor from a char range.
Definition: String.h:110
String(long double ld, bool full_precision=true)
Constructor from long double (full_precision controls number of fractional digits,...
String(long unsigned int i)
Constructor from an unsigned integer.
static String random(UInt length)
returns a random string of the given length. It consists of [0-9a-zA-Z]
String & ensureLastChar(char end)
Makes sure the string ends with the character end.
String operator+(long double ld) const
Sum operator for long double.
String suffix(SizeType length) const
returns the suffix of length length
String & operator+=(long long unsigned int i)
Sum operator for an unsigned integer.
String operator+(short int i) const
Sum operator for an integer.
String operator+(const String &s) const
Sum operator for String.
String suffix(Int length) const
returns the suffix of length length
String(short int i)
Constructor from an integer.
bool split(const String &splitter, std::vector< String > &substrings) const
Splits a string into substrings using splitter (the whole string) as delimiter.
String operator+(unsigned int i) const
Sum operator for an unsigned integer.
static String number(double d, UInt n)
returns a string for d with exactly n decimal places
static const String EMPTY
Empty string for comparisons.
Definition: String.h:64
String(unsigned int i)
Constructor from an unsigned integer.
float toFloat() const
Conversion to float.
String & operator=(String &&) &=default
Move assignment operator.
String operator+(long long unsigned int i) const
Sum operator for an unsigned integer.
String & operator+=(const std::string &s)
Sum operator for std::string.
String(const QString &s)
Constructor from Qt QString.
void concatenate(StringIterator first, StringIterator last, const String &glue="")
Concatenates all elements from first to last-1 and inserts glue between the elements.
Definition: String.h:468
String & toUpper()
Converts the string to uppercase.
String & operator+=(const String &s)
Sum operator for String.
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:111
int Int
Signed integer type.
Definition: Types.h:102
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
const double c
Definition: Constants.h:214
FLASHIda C++ to C# (or vice versa) bridge functions The functions here are called in C# to invoke fun...
Definition: FeatureDeconvolution.h:48
std::size_t hash_value(const DPosition< N, T > &b)
Definition: HashGrid.h:458
std::size_t operator()(OpenMS::String const &s) const
Definition: String.h:491