OpenMS  2.8.0
StringView.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-2021.
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>
40 
41 #include <algorithm> // for "min"
42 #include <string>
43 #include <cstring>
44 #include <vector>
45 
46 class QString;
47 
48 namespace OpenMS
49 {
50 
55  class OPENMS_DLLAPI StringView
56  {
57  public:
58 
59  // create view on string
60  StringView() = default;
61 
62  // construct from other view
63  StringView(const StringView&) = default;
64 
65  // copy assignment
66  StringView& operator=(const StringView&) = default;
67 
68  // create view on string
69  StringView(const std::string& s) : begin_(s.data()), size_(s.size())
70  {
71  }
72 
74  bool operator<(const StringView other) const
75  {
76  if (size_ < other.size_) return true;
77 
78  if (size_ > other.size_) return false;
79 
80  // same size
81  // same sequence, if both Views point to the same start
82  if (begin_ == other.begin_) return false;
83 
84  return strncmp(begin_, other.begin_, size_) < 0;
85  }
86 
87  bool operator==(const StringView other) const
88  {
89  if (size_ != other.size_) return false;
90 
91  //same size
92  // same sequence, if both Views point to the same start
93  if (begin_ == other.begin_) return true;
94 
95  return strncmp(begin_, other.begin_, size_) == 0;
96  }
97 
99  inline StringView substr(Size start, Size length) const
100  {
101  if (!size_) return *this;
102 
103  StringView sv(*this);
104  sv.begin_ = begin_ + start;
105  sv.size_ = std::min(length, sv.size_ - start);
106  return sv;
107  }
108 
110  inline Size size() const
111  {
112  return size_;
113  }
114 
116  inline String getString() const
117  {
118  if (!size_) return String();
119  return String(begin_, begin_ + size_);
120  }
121 
122  private:
123  const char* begin_;
125  };
126 
127 } // namespace OpenMS
128 
StringView provides a non-owning view on an existing string.
Definition: StringView.h:56
StringView(const StringView &)=default
StringView substr(Size start, Size length) const
create view that references a substring of the original string
Definition: StringView.h:99
const char * begin_
Definition: StringView.h:123
StringView()=default
bool operator<(const StringView other) const
less operator
Definition: StringView.h:74
StringView & operator=(const StringView &)=default
StringView(const std::string &s)
Definition: StringView.h:69
Size size() const
size of view
Definition: StringView.h:110
String getString() const
create String object from view
Definition: StringView.h:116
bool operator==(const StringView other) const
Definition: StringView.h:87
Size size_
Definition: StringView.h:124
A more convenient string class.
Definition: String.h:60
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:127
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47