OpenMS
Loading...
Searching...
No Matches
StringView.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>
14
15#include <algorithm> // for "min"
16#include <string>
17#include <cstring>
18#include <vector>
19
20class QString;
21
22namespace OpenMS
23{
24
29 class OPENMS_DLLAPI StringView
30 {
31 public:
32
33 // create view on string
34 StringView() = default;
35
36 // construct from other view
37 StringView(const StringView&) = default;
38
39 // copy assignment
40 StringView& operator=(const StringView&) = default;
41
42 // create view on string
43 StringView(const std::string& s) : begin_(s.data()), size_(s.size())
44 {
45 }
46
48 bool operator<(const StringView other) const
49 {
50 if (size_ < other.size_) return true;
51
52 if (size_ > other.size_) return false;
53
54 // same size
55 // same sequence, if both Views point to the same start
56 if (begin_ == other.begin_) return false;
57
58 return strncmp(begin_, other.begin_, size_) < 0;
59 }
60
61 bool operator==(const StringView other) const
62 {
63 if (size_ != other.size_) return false;
64
65 //same size
66 // same sequence, if both Views point to the same start
67 if (begin_ == other.begin_) return true;
68
69 return strncmp(begin_, other.begin_, size_) == 0;
70 }
71
73 inline StringView substr(Size start, Size length) const
74 {
75 if (!size_) return *this;
76
77 StringView sv(*this);
78 sv.begin_ = begin_ + start;
79 sv.size_ = std::min(length, sv.size_ - start);
80 return sv;
81 }
82
84 inline Size size() const
85 {
86 return size_;
87 }
88
90 inline String getString() const
91 {
92 if (!size_) return String();
93 return String(begin_, begin_ + size_);
94 }
95
96 private:
97 const char* begin_;
99 };
100
101} // namespace OpenMS
102
StringView provides a non-owning view on an existing string.
Definition StringView.h:30
StringView(const StringView &)=default
StringView substr(Size start, Size length) const
create view that references a substring of the original string
Definition StringView.h:73
const char * begin_
Definition StringView.h:97
StringView()=default
bool operator<(const StringView other) const
less operator
Definition StringView.h:48
StringView & operator=(const StringView &)=default
StringView(const std::string &s)
Definition StringView.h:43
Size size() const
size of view
Definition StringView.h:84
String getString() const
create String object from view
Definition StringView.h:90
bool operator==(const StringView other) const
Definition StringView.h:61
Size size_
Definition StringView.h:98
A more convenient string class.
Definition String.h:34
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