OpenMS
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
12#include <OpenMS/OpenMSConfig.h>
13
14#include <string>
15#include <cstring>
16#include <vector>
17
18namespace OpenMS
19{
20 class DataValue;
21 template <typename FloatingPointType>
22 struct PrecisionWrapper;
30 class String :
31 public std::string
32 {
33public:
34
36 OPENMS_DLLAPI static const String EMPTY;
37
42 typedef iterator Iterator;
44 typedef const_iterator ConstIterator;
46 typedef reverse_iterator ReverseIterator;
48 typedef const_reverse_iterator ConstReverseIterator;
50 typedef size_type SizeType;
51
54
56
61 OPENMS_DLLAPI String();
63 OPENMS_DLLAPI String(const String&) = default;
65 OPENMS_DLLAPI String(String&&) = default;
67 OPENMS_DLLAPI String(const std::string& s);
69 OPENMS_DLLAPI String(const std::string_view& sv);
71 OPENMS_DLLAPI String(const char* s);
73 OPENMS_DLLAPI String(const char c);
75 OPENMS_DLLAPI String(const char* s, SizeType length);
77 OPENMS_DLLAPI String(size_t len, char c);
79 template <class InputIterator>
80 String(InputIterator first, InputIterator last) :
81 std::string(first, last)
82 {
83 }
84
86 OPENMS_DLLAPI String(int i);
88 OPENMS_DLLAPI String(unsigned int i);
90 OPENMS_DLLAPI String(short int i);
92 OPENMS_DLLAPI String(short unsigned int i);
94 OPENMS_DLLAPI String(long int i);
96 OPENMS_DLLAPI String(long unsigned int i);
98 OPENMS_DLLAPI String(long long unsigned int i);
100 OPENMS_DLLAPI String(long long signed int i);
102 OPENMS_DLLAPI String(float f, bool full_precision = true);
104 OPENMS_DLLAPI String(double d, bool full_precision = true);
106 OPENMS_DLLAPI String(long double ld, bool full_precision = true);
108 OPENMS_DLLAPI String(const DataValue& d, bool full_precision = true);
109
111
116 OPENMS_DLLAPI bool hasPrefix(const String& string) const;
117
119 OPENMS_DLLAPI bool hasSuffix(const String& string) const;
120
122 OPENMS_DLLAPI bool hasSubstring(const String& string) const;
123
125 OPENMS_DLLAPI bool has(Byte byte) const;
127
129 OPENMS_DLLAPI String& operator=(const String&) = default;
131 OPENMS_DLLAPI String& operator=(String&&) & = default;
132
141 OPENMS_DLLAPI String prefix(SizeType length) const;
142
148 OPENMS_DLLAPI String suffix(SizeType length) const;
149
156 OPENMS_DLLAPI String prefix(Int length) const;
157
164 OPENMS_DLLAPI String suffix(Int length) const;
165
171 OPENMS_DLLAPI String prefix(char delim) const;
172
178 OPENMS_DLLAPI String suffix(char delim) const;
179
192 OPENMS_DLLAPI String substr(size_t pos = 0, size_t n = npos) const;
193
201 OPENMS_DLLAPI String chop(Size n) const;
202
204
205
213 OPENMS_DLLAPI String& reverse();
214
216 OPENMS_DLLAPI String& trim();
217
225 OPENMS_DLLAPI bool isQuoted(char q = '"');
226
234 OPENMS_DLLAPI String& quote(char q = '"', QuotingMethod method = ESCAPE);
235
245 OPENMS_DLLAPI String& unquote(char q = '"', QuotingMethod method = ESCAPE);
246
248 OPENMS_DLLAPI String& simplify();
249
251 OPENMS_DLLAPI String& fillLeft(char c, UInt size);
252
254 OPENMS_DLLAPI String& fillRight(char c, UInt size);
255
257 OPENMS_DLLAPI String& toUpper();
258
260 OPENMS_DLLAPI String& toLower();
261
263 OPENMS_DLLAPI String& firstToUpper();
264
266 OPENMS_DLLAPI String& substitute(char from, char to);
267
269 OPENMS_DLLAPI String& substitute(const String& from, const String& to);
270
272 OPENMS_DLLAPI String& remove(char what);
273
275 OPENMS_DLLAPI String& ensureLastChar(char end);
276
278 OPENMS_DLLAPI String& removeWhitespaces();
280
284
285
294 OPENMS_DLLAPI Int toInt() const;
295
304 OPENMS_DLLAPI Int32 toInt32() const;
305
314 OPENMS_DLLAPI Int64 toInt64() const;
315
321 OPENMS_DLLAPI float toFloat() const;
322
328 OPENMS_DLLAPI double toDouble() const;
329
331
336 OPENMS_DLLAPI String operator+(int i) const;
338 OPENMS_DLLAPI String operator+(unsigned int i) const;
340 OPENMS_DLLAPI String operator+(short int i) const;
342 OPENMS_DLLAPI String operator+(short unsigned int i) const;
344 OPENMS_DLLAPI String operator+(long int i) const;
346 OPENMS_DLLAPI String operator+(long unsigned int i) const;
348 OPENMS_DLLAPI String operator+(long long unsigned int i) const;
350 OPENMS_DLLAPI String operator+(float f) const;
352 OPENMS_DLLAPI String operator+(double d) const;
354 OPENMS_DLLAPI String operator+(long double ld) const;
356 OPENMS_DLLAPI String operator+(char c) const;
358 OPENMS_DLLAPI String operator+(const char* s) const;
360 OPENMS_DLLAPI String operator+(const String& s) const;
362 OPENMS_DLLAPI String operator+(const std::string& s) const;
364
369 OPENMS_DLLAPI String& operator+=(int i);
371 OPENMS_DLLAPI String& operator+=(unsigned int i);
373 OPENMS_DLLAPI String& operator+=(short int i);
375 OPENMS_DLLAPI String& operator+=(short unsigned int i);
377 OPENMS_DLLAPI String& operator+=(long int i);
379 OPENMS_DLLAPI String& operator+=(long unsigned int i);
381 OPENMS_DLLAPI String& operator+=(long long unsigned int i);
383 OPENMS_DLLAPI String& operator+=(float f);
385 OPENMS_DLLAPI String& operator+=(double d);
387 OPENMS_DLLAPI String& operator+=(long double d);
389 OPENMS_DLLAPI String& operator+=(char c);
391 OPENMS_DLLAPI String& operator+=(const char* s);
393 OPENMS_DLLAPI String& operator+=(const String& s);
395 OPENMS_DLLAPI String& operator+=(const std::string& s);
397
399 OPENMS_DLLAPI static String random(UInt length);
400
402 OPENMS_DLLAPI static String number(double d, UInt n);
403
409 OPENMS_DLLAPI static String numberLength(double d, UInt n);
410
427 OPENMS_DLLAPI bool split(const char splitter, std::vector<String>& substrings, bool quote_protect = false) const;
428
440 OPENMS_DLLAPI bool split(const String& splitter, std::vector<String>& substrings) const;
441
456 OPENMS_DLLAPI bool split_quoted(const String& splitter, std::vector<String>& substrings,
457 char q = '"', QuotingMethod method = ESCAPE) const;
458
464 template <class StringIterator>
465 void concatenate(StringIterator first, StringIterator last, const String& glue = "")
466 {
467 //empty container
468 if (first == last)
469 {
470 std::string::clear();
471 return;
472 }
473
474 std::string::operator=(*first);
475 for (StringIterator it = ++first; it != last; ++it)
476 {
477 std::string::operator+=(glue + (*it));
478 }
479 }
480 };
481 OPENMS_DLLAPI ::size_t hash_value(OpenMS::String const& s);
482} // namespace OpenMS
483
484namespace std
485{
486 template <> struct hash<OpenMS::String> //hash for String
487 {
488 std::size_t operator()( OpenMS::String const& s) const
489 {
490 return std::hash<string>()(static_cast<string>(s));
491 }
492 };
493} // namespace std
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition DataValue.h:32
A more convenient string class.
Definition String.h:32
String & operator+=(long unsigned int i)
Sum operator for an unsigned integer.
String(long long unsigned int i)
Constructor from an unsigned integer.
String & operator+=(short unsigned int i)
Sum operator for an unsigned integer.
const_reverse_iterator ConstReverseIterator
Const reverse Iterator.
Definition String.h:48
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 & operator+=(short int i)
Sum operator for an integer.
String(float f, bool full_precision=true)
Constructor from float (full_precision controls number of fractional digits, 3 digits when false,...
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 & operator+=(const char *s)
Sum operator for char*.
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 ...
String & operator+=(float f)
Sum operator for float.
reverse_iterator ReverseIterator
Reverse Iterator.
Definition String.h:46
String & unquote(char q='"', QuotingMethod method = ESCAPE)
Reverses changes made by the quote method.
Int64 toInt64() const
Conversion to Int64.
String & operator+=(int i)
Sum operator for an integer.
String & substitute(const String &from, const String &to)
Replaces all occurrences of the string from by the string to.
String prefix(SizeType length) const
returns the prefix of length length
String prefix(char delim) const
returns the prefix up to the first occurrence of char delim (excluding it)
iterator Iterator
Iterator.
Definition String.h:42
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(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 & operator+=(double d)
Sum operator for double.
String(int i)
Constructor from an integer.
String & operator+=(unsigned int i)
Sum operator for an unsigned integer.
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:44
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition String.h:53
@ ESCAPE
Definition String.h:53
@ DOUBLE
Definition String.h:53
@ NONE
Definition String.h:53
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 & remove(char what)
Remove all occurrences of the character what.
Int32 toInt32() const
Conversion to Int32.
String & reverse()
inverts the direction of the string
String & operator+=(long long unsigned int i)
Sum operator for an unsigned 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 & firstToUpper()
Converts the first letter of the string to uppercase.
String(short unsigned int i)
Constructor from an unsigned integer.
String & ensureLastChar(char end)
Makes sure the string ends with the character end.
String(const char *s)
Constructor from char*.
String operator+(double d) const
Sum operator for double.
String()
Default constructor.
String(String &&)=default
Move constructor.
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
bool isQuoted(char q='"')
Checks if the string is wrapped in quotation marks.
String & quote(char q='"', QuotingMethod method = ESCAPE)
Wraps the string in quotation marks.
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+=(const std::string &s)
Sum operator for std::string.
String & operator=(String &&) &=default
Move assignment operator.
String(const String &)=default
Copy constructor.
String operator+(const std::string &s) const
Sum operator for std::string.
String & removeWhitespaces()
removes whitespaces (space, tab, line feed, carriage return)
String & simplify()
merges subsequent whitespaces to one blank character
size_type SizeType
UInt type.
Definition String.h:50
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:80
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 & operator+=(const String &s)
Sum operator for String.
String & fillLeft(char c, UInt size)
Adds c on the left side until the size of the string is size.
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 double d)
Sum operator for long double.
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.
String & toLower()
Converts the string to lowercase.
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:36
String(unsigned int i)
Constructor from an unsigned integer.
float toFloat() const
Conversion to float.
String & toUpper()
Converts the string to uppercase.
String operator+(long long unsigned int i) const
Sum operator for an unsigned integer.
String & operator=(const String &)=default
Assignment operator.
String & fillRight(char c, UInt size)
Adds c on the right side until the size of the string is size.
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
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:465
String & operator+=(long int i)
Sum operator for an integer.
String & operator+=(char c)
Sum operator for char.
int32_t Int32
Signed integer type (32bit)
Definition Types.h:26
int64_t Int64
Signed integer type (64bit)
Definition Types.h:40
uint8_t Byte
Byte type.
Definition Types.h:81
int Int
Signed integer type.
Definition Types.h:72
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
::size_t hash_value(OpenMS::String const &s)
STL namespace.
std::size_t operator()(OpenMS::String const &s) const
Definition String.h:488