cppcore
A small, portable C++ core library
Loading...
Searching...
No Matches
TStringView.h
Go to the documentation of this file.
1/*-----------------------------------------------------------------------------------------------
2The MIT License (MIT)
3
4Copyright (c) 2014-2026 Kim Kulling
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of
7this software and associated documentation files (the "Software"), to deal in
8the Software without restriction, including without limitation the rights to
9use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10the Software, and to permit persons to whom the Software is furnished to do so,
11subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22-----------------------------------------------------------------------------------------------*/
23#pragma once
24
26
27namespace cppcore {
28
29//-------------------------------------------------------------------------------------------------
34//-------------------------------------------------------------------------------------------------
35template <class T>
37public:
38 using const_iterator = const T*;
39
41 TStringView() = default;
42
46 TStringView(const T *ptr, size_t len);
47
49 ~TStringView() = default;
50
53 size_t size() const;
54
58 const T *data(size_t offset = 0) const;
59
62 bool isEmpty() const;
63
66 const_iterator begin() const;
67
70 const_iterator end() const;
71
72private:
73 const T *mPtr{nullptr};
74 size_t mLen{0l};
75};
76
77template <class T>
78inline TStringView<T>::TStringView(const T *ptr, size_t len) :
79 mPtr(ptr), mLen(len) {
80 // empty
81}
82
83template <class T>
84inline size_t TStringView<T>::size() const {
85 return mLen;
86}
87
88template <class T>
89inline const T *TStringView<T>::data(size_t offset) const {
90 if (offset > mLen) {
91 return nullptr;
92 }
93 return mPtr + offset;
94}
95
96template <class T>
97inline bool TStringView<T>::isEmpty() const {
98 return mLen == 0;
99}
100
101template <class T>
103 if (isEmpty()) {
104 return end();
105 }
106 return mPtr;
107}
108
109template <class T>
111 return mPtr + mLen;
112}
113
114} // namespace cppcore
const_iterator end() const
Will return the end entry.
bool isEmpty() const
Will return true, if there is no string to view.
Definition TStringView.h:97
TStringView()=default
The default constructor.
~TStringView()=default
The class destructor.
const T * data(size_t offset=0) const
Will return the data with an offset, if given.
Definition TStringView.h:89
size_t size() const
Will return the size of the view.
Definition TStringView.h:84
const T * const_iterator
Definition TStringView.h:38
const_iterator begin() const
Will return the first entry.