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