OpenMS  2.6.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-2020.
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 <algorithm> // for "min"
41 #include <string>
42 #include <cstring>
43 #include <vector>
44 
45 class QString;
46 
47 namespace OpenMS
48 {
49  class DataValue;
50  template <typename FloatingPointType>
51  struct PrecisionWrapper;
59  class String :
60  public std::string
61  {
62 public:
63 
65  OPENMS_DLLAPI static const String EMPTY;
66 
70  typedef iterator Iterator;
73  typedef const_iterator ConstIterator;
75  typedef reverse_iterator ReverseIterator;
77  typedef const_reverse_iterator ConstReverseIterator;
79  typedef size_type SizeType;
80 
83 
85 
89  OPENMS_DLLAPI String();
92  OPENMS_DLLAPI String(const String&) = default;
94  OPENMS_DLLAPI String(String&&) = default;
96  OPENMS_DLLAPI String(const std::string& s);
98  OPENMS_DLLAPI String(const QString& s);
100  OPENMS_DLLAPI String(const char* s);
102  OPENMS_DLLAPI String(const char c);
104  OPENMS_DLLAPI String(const char* s, SizeType length);
106  OPENMS_DLLAPI String(size_t len, char c);
108  template <class InputIterator>
109  String(InputIterator first, InputIterator last) :
110  std::string(first, last)
111  {
112  }
113 
115  OPENMS_DLLAPI String(int i);
117  OPENMS_DLLAPI String(unsigned int i);
119  OPENMS_DLLAPI String(short int i);
121  OPENMS_DLLAPI String(short unsigned int i);
123  OPENMS_DLLAPI String(long int i);
125  OPENMS_DLLAPI String(long unsigned int i);
127  OPENMS_DLLAPI String(long long unsigned int i);
129  OPENMS_DLLAPI String(long long signed int i);
131  OPENMS_DLLAPI String(float f, bool full_precision = true);
133  OPENMS_DLLAPI String(double d, bool full_precision = true);
135  OPENMS_DLLAPI String(long double ld, bool full_precision = true);
137  OPENMS_DLLAPI String(const DataValue& d, bool full_precision = true);
138 
140 
144  OPENMS_DLLAPI bool hasPrefix(const String& string) const;
146 
148  OPENMS_DLLAPI bool hasSuffix(const String& string) const;
149 
151  OPENMS_DLLAPI bool hasSubstring(const String& string) const;
152 
154  OPENMS_DLLAPI bool has(Byte byte) const;
156 
158  OPENMS_DLLAPI String& operator=(const String&) = default;
160  OPENMS_DLLAPI String& operator=(String&&) & = default;
161 
170  OPENMS_DLLAPI String prefix(SizeType length) const;
171 
177  OPENMS_DLLAPI String suffix(SizeType length) const;
178 
185  OPENMS_DLLAPI String prefix(Int length) const;
186 
193  OPENMS_DLLAPI String suffix(Int length) const;
194 
200  OPENMS_DLLAPI String prefix(char delim) const;
201 
207  OPENMS_DLLAPI String suffix(char delim) const;
208 
221  OPENMS_DLLAPI String substr(size_t pos = 0, size_t n = npos) const;
222 
230  OPENMS_DLLAPI String chop(Size n) const;
231 
233 
234 
241  OPENMS_DLLAPI String& reverse();
243 
245  OPENMS_DLLAPI String& trim();
246 
254  OPENMS_DLLAPI String& quote(char q = '"', QuotingMethod method = ESCAPE);
255 
265  OPENMS_DLLAPI String& unquote(char q = '"', QuotingMethod method = ESCAPE);
266 
268  OPENMS_DLLAPI String& simplify();
269 
271  OPENMS_DLLAPI String& fillLeft(char c, UInt size);
272 
274  OPENMS_DLLAPI String& fillRight(char c, UInt size);
275 
277  OPENMS_DLLAPI String& toUpper();
278 
280  OPENMS_DLLAPI String& toLower();
281 
283  OPENMS_DLLAPI String& firstToUpper();
284 
286  OPENMS_DLLAPI String& substitute(char from, char to);
287 
289  OPENMS_DLLAPI String& substitute(const String& from, const String& to);
290 
292  OPENMS_DLLAPI String& remove(char what);
293 
295  OPENMS_DLLAPI String& ensureLastChar(char end);
296 
298  OPENMS_DLLAPI String& removeWhitespaces();
300 
304 
313  OPENMS_DLLAPI Int toInt() const;
314 
320  OPENMS_DLLAPI float toFloat() const;
321 
327  OPENMS_DLLAPI double toDouble() const;
328 
330  OPENMS_DLLAPI QString toQString() const;
331 
333 
337  OPENMS_DLLAPI String operator+(int i) const;
340  OPENMS_DLLAPI String operator+(unsigned int i) const;
342  OPENMS_DLLAPI String operator+(short int i) const;
344  OPENMS_DLLAPI String operator+(short unsigned int i) const;
346  OPENMS_DLLAPI String operator+(long int i) const;
348  OPENMS_DLLAPI String operator+(long unsigned int i) const;
350  OPENMS_DLLAPI String operator+(long long unsigned int i) const;
352  OPENMS_DLLAPI String operator+(float f) const;
354  OPENMS_DLLAPI String operator+(double d) const;
356  OPENMS_DLLAPI String operator+(long double ld) const;
358  OPENMS_DLLAPI String operator+(char c) const;
360  OPENMS_DLLAPI String operator+(const char* s) const;
362  OPENMS_DLLAPI String operator+(const String& s) const;
364  OPENMS_DLLAPI String operator+(const std::string& s) const;
366 
370  OPENMS_DLLAPI String& operator+=(int i);
373  OPENMS_DLLAPI String& operator+=(unsigned int i);
375  OPENMS_DLLAPI String& operator+=(short int i);
377  OPENMS_DLLAPI String& operator+=(short unsigned int i);
379  OPENMS_DLLAPI String& operator+=(long int i);
381  OPENMS_DLLAPI String& operator+=(long unsigned int i);
383  OPENMS_DLLAPI String& operator+=(long long unsigned int i);
385  OPENMS_DLLAPI String& operator+=(float f);
387  OPENMS_DLLAPI String& operator+=(double d);
389  OPENMS_DLLAPI String& operator+=(long double d);
391  OPENMS_DLLAPI String& operator+=(char c);
393  OPENMS_DLLAPI String& operator+=(const char* s);
395  OPENMS_DLLAPI String& operator+=(const String& s);
397  OPENMS_DLLAPI String& operator+=(const std::string& s);
399 
401  OPENMS_DLLAPI static String random(UInt length);
402 
404  OPENMS_DLLAPI static String number(double d, UInt n);
410  OPENMS_DLLAPI static String numberLength(double d, UInt n);
411 
412 
429  OPENMS_DLLAPI bool split(const char splitter, std::vector<String>& substrings, bool quote_protect = false) const;
430 
442  OPENMS_DLLAPI bool split(const String& splitter, std::vector<String>& substrings) const;
443 
458  OPENMS_DLLAPI bool split_quoted(const String& splitter, std::vector<String>& substrings,
459  char q = '"', QuotingMethod method = ESCAPE) const;
460 
466  template <class StringIterator>
467  void concatenate(StringIterator first, StringIterator last, const String& glue = "")
468  {
469  //empty container
470  if (first == last)
471  {
472  std::string::clear();
473  return;
474  }
475 
476  std::string::operator=(* first);
477  for (StringIterator it = ++first; it != last; ++it)
478  {
479  std::string::operator+=(glue + (*it));
480  }
481  }
482 
483  };
484 
489  class OPENMS_DLLAPI StringView
490  {
491  public:
492 
493  // create view on string
494  StringView() : begin_(), size_(0)
495  {
496  }
497 
498  // create view on string
499  StringView(const std::string& s) : begin_(s.data()), size_(s.size())
500  {
501  }
502 
503  // construct from other view
504  StringView(const StringView& s) : begin_(s.begin_), size_(s.size_)
505  {
506  }
507 
509  bool operator<(const StringView other) const
510  {
511  if (size_ < other.size_) return true;
512 
513  if (size_ > other.size_) return false;
514 
515  // same size
516  // same sequence, if both Views point to the same start
517  if (begin_ == other.begin_) return false;
518 
519  return strncmp(begin_, other.begin_, size_) < 0;
520  }
521 
522  bool operator==(const StringView other) const
523  {
524  if (size_ != other.size_) return false;
525 
526  //same size
527  // same sequence, if both Views point to the same start
528  if (begin_ == other.begin_) return true;
529 
530  return strncmp(begin_, other.begin_, size_) == 0;
531  }
532 
534  inline StringView substr(Size start, Size length) const
535  {
536  if (!size_) return *this;
537 
538  StringView sv(*this);
539  sv.begin_ = begin_ + start;
540  sv.size_ = std::min(length, sv.size_ - start);
541  return sv;
542  }
543 
545  inline Size size() const
546  {
547  return size_;
548  }
549 
551  inline String getString() const
552  {
553  if (!size_) return String();
554  return String(begin_, begin_ + size_);
555  }
556 
557  private:
558  const char* begin_;
560  };
561 
562  OPENMS_DLLAPI ::size_t hash_value(OpenMS::String const& s);
563 } // namespace OpenMS
564 
565 namespace std
566 {
567  template <> struct hash<OpenMS::String> //hash for String
568  {
569  std::size_t operator()( OpenMS::String const& s) const
570  {
571  return std::hash<string>()(static_cast<string>(s));
572  }
573  };
574 } // namespace std
OpenMS::StringView::operator==
bool operator==(const StringView other) const
Definition: String.h:522
OpenMS::String::SizeType
size_type SizeType
UInt type.
Definition: String.h:79
OpenMS::hash_value
std::size_t hash_value(const DPosition< N, T > &b)
Definition: HashGrid.h:458
OpenMS::StringView::StringView
StringView(const std::string &s)
Definition: String.h:499
OpenMS::String< TSize >::QuotingMethod
QuotingMethod
How to handle embedded quotes when quoting strings.
Definition: String.h:82
OpenMS::String::fillLeft
String & fillLeft(char c, UInt size)
Adds c on the left side until the size of the string is size.
OpenMS::String::random
static String random(UInt length)
returns a random string of the given length. It consists of [0-9a-zA-Z]
OpenMS::StringView::getString
String getString() const
create String object from view
Definition: String.h:551
Types.h
OpenMS::String::ConstReverseIterator
const_reverse_iterator ConstReverseIterator
Const reverse Iterator.
Definition: String.h:77
OpenMS::String::substitute
String & substitute(char from, char to)
Replaces all occurrences of the character from by the character to.
OpenMS::String::ReverseIterator
reverse_iterator ReverseIterator
Reverse Iterator.
Definition: String.h:75
OpenMS::String::toDouble
double toDouble() const
Conversion to double.
OpenMS::String::NONE
Definition: String.h:82
OpenMS::String
A more convenient string class.
Definition: String.h:59
OpenMS::String::trim
String & trim()
removes whitespaces (space, tab, line feed, carriage return) at the beginning and the end of the stri...
OpenMS::String::removeWhitespaces
String & removeWhitespaces()
removes whitespaces (space, tab, line feed, carriage return)
OpenMS::Size
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
OpenMS::Constants::c
const double c
OpenMS::String::prefix
String prefix(SizeType length) const
returns the prefix of length length
OpenMS::String::DOUBLE
Definition: String.h:82
OpenMS::String::operator+=
String & operator+=(int i)
Sum operator for an integer.
OpenMS::String::hasPrefix
bool hasPrefix(const String &string) const
true if String begins with string, false otherwise
OpenMS::String::hasSubstring
bool hasSubstring(const String &string) const
true if String contains the string, false otherwise
OpenMS::String::operator=
String & operator=(const String &)=default
Assignment operator.
OpenMS::String::simplify
String & simplify()
merges subsequent whitespaces to one blank character
OpenMS::String::firstToUpper
String & firstToUpper()
Converts the first letter of the string to uppercase.
OpenMS::String::chop
String chop(Size n) const
Returns a substring where n characters were removed from the end of the string.
OpenMS::Byte
OPENMS_BYTE_TYPE Byte
Byte type.
Definition: Types.h:111
OpenMS::StringView::operator<
bool operator<(const StringView other) const
less operator
Definition: String.h:509
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::String::number
static String number(double d, UInt n)
returns a string for d with exactly n decimal places
OpenMS::DataValue
Class to hold strings, numeric values, lists of strings and lists of numeric values.
Definition: DataValue.h:56
OpenMS::StringView::begin_
const char * begin_
Definition: String.h:558
int
std::hash< OpenMS::String >::operator()
std::size_t operator()(OpenMS::String const &s) const
Definition: String.h:569
OpenMS::String::has
bool has(Byte byte) const
true if String contains the byte, false otherwise
OpenMS::StringView
StringView provides a non-owning view on an existing string.
Definition: String.h:489
OpenMS::StringView::substr
StringView substr(Size start, Size length) const
create view that references a substring of the original string
Definition: String.h:534
OpenMS::String::split
bool split(const char splitter, std::vector< String > &substrings, bool quote_protect=false) const
Splits a string into substrings using splitter as delimiter.
OpenMS::String::toQString
QString toQString() const
Conversion to Qt QString.
OpenMS::String::EMPTY
static const String EMPTY
Empty string for comparisons.
Definition: String.h:65
OpenMS::String::hasSuffix
bool hasSuffix(const String &string) const
true if String ends with string, false otherwise
OpenMS::String::unquote
String & unquote(char q='"', QuotingMethod method = ESCAPE)
Reverses changes made by the quote method.
OpenMS::String::toUpper
String & toUpper()
Converts the string to uppercase.
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::String::ESCAPE
Definition: String.h:82
OpenMS::String::quote
String & quote(char q='"', QuotingMethod method = ESCAPE)
Wraps the string in quotation marks.
OpenMS::String::fillRight
String & fillRight(char c, UInt size)
Adds c on the right side until the size of the string is size.
OpenMS::StringView::StringView
StringView(const StringView &s)
Definition: String.h:504
OpenMS::StringView::size_
Size size_
Definition: String.h:559
OpenMS::String::String
String()
Default constructor.
OpenMS::String::Iterator
iterator Iterator
Iterator.
Definition: String.h:71
OpenMS::String::substr
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...
OpenMS::String::reverse
String & reverse()
inverts the direction of the string
OpenMS::String::remove
String & remove(char what)
Remove all occurrences of the character what.
OpenMS::String::concatenate
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:467
OpenMS::String::ensureLastChar
String & ensureLastChar(char end)
Makes sure the string ends with the character end.
OpenMS::StringView::size
Size size() const
size of view
Definition: String.h:545
OpenMS::StringView::StringView
StringView()
Definition: String.h:494
OpenMS::String::numberLength
static String numberLength(double d, UInt n)
Returns a string with at maximum n characters for d.
OpenMS::String::suffix
String suffix(SizeType length) const
returns the suffix of length length
OpenMS::String::String
String(InputIterator first, InputIterator last)
Constructor from a char range.
Definition: String.h:109
OpenMS::String::ConstIterator
const_iterator ConstIterator
Const Iterator.
Definition: String.h:73
OpenMS::String::toInt
Int toInt() const
Conversion to int.
OpenMS::String::toFloat
float toFloat() const
Conversion to float.
OpenMS::String::split_quoted
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...
OpenMS::String::operator+
String operator+(int i) const
Sum operator for an integer.
OpenMS::String::toLower
String & toLower()
Converts the string to lowercase.