string_view.h
Go to the documentation of this file.
1
2/************************************************************************************
3* *
4* Copyright (c) 2014 - 2018 Axel Menzel <info@rttr.org> *
5* *
6* This file is part of RTTR (Run Time Type Reflection) *
7* License: MIT License *
8* *
9* Permission is hereby granted, free of charge, to any person obtaining *
10* a copy of this software and associated documentation files (the "Software"), *
11* to deal in the Software without restriction, including without limitation *
12* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
13* and/or sell copies of the Software, and to permit persons to whom the *
14* Software is furnished to do so, subject to the following conditions: *
15* *
16* The above copyright notice and this permission notice shall be included in *
17* all copies or substantial portions of the Software. *
18* *
19* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
20* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
21* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
22* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
23* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
24* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
25* SOFTWARE. *
26* *
27*************************************************************************************/
28
29#ifndef RTTR_STRING_VIEW_H_
30#define RTTR_STRING_VIEW_H_
31
32#include "rttr/detail/base/core_prerequisites.h"
33
34#include <string>
35#include <ostream>
36
37namespace rttr
38{
39
47template<typename CharT, typename Traits = std::char_traits<CharT> >
49{
50 public:
51 using traits_type = Traits;
52 using value_type = CharT;
53 using pointer = CharT*;
54 using const_pointer = const CharT*;
55 using reference = CharT&;
56 using const_reference = const CharT&;
59 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
61 using size_type = std::size_t;
62 using difference_type = std::ptrdiff_t;
63 RTTR_STATIC_CONSTEXPR size_type npos = size_type(-1);
64
68 RTTR_CONSTEXPR basic_string_view() RTTR_NOEXCEPT;
69
73 RTTR_CONSTEXPR basic_string_view(const basic_string_view& other) RTTR_NOEXCEPT;
74
81 RTTR_CONSTEXPR basic_string_view(const CharT* str);
82
89 RTTR_CONSTEXPR basic_string_view(const CharT* str, size_type len);
90
95 template<typename Allocator>
96 basic_string_view(const std::basic_string<CharT, Traits, Allocator>& str) RTTR_NOEXCEPT;
97
101 basic_string_view& operator=(const basic_string_view& other) RTTR_NOEXCEPT;
102 RTTR_CXX14_CONSTEXPR void swap(basic_string_view& s) RTTR_NOEXCEPT;
103
109 RTTR_CONSTEXPR const_iterator begin() const RTTR_NOEXCEPT;
110
116 RTTR_CONSTEXPR const_iterator cbegin() const RTTR_NOEXCEPT;
117
124 RTTR_CONSTEXPR const_iterator end() const RTTR_NOEXCEPT;
125
132 RTTR_CONSTEXPR const_iterator cend() const RTTR_NOEXCEPT;
133
140 const_reverse_iterator rbegin() const RTTR_NOEXCEPT;
141
148 const_reverse_iterator crbegin() const RTTR_NOEXCEPT;
149
157 const_reverse_iterator rend() const RTTR_NOEXCEPT;
158
166 const_reverse_iterator crend() const RTTR_NOEXCEPT;
167
173 RTTR_CONSTEXPR size_type size() const RTTR_NOEXCEPT;
174
180 RTTR_CONSTEXPR size_type length() const RTTR_NOEXCEPT;
181
187 RTTR_CONSTEXPR size_type max_size() const RTTR_NOEXCEPT;
188
194 RTTR_CONSTEXPR bool empty() const RTTR_NOEXCEPT;
195
202 RTTR_CONSTEXPR const_reference operator[](size_type pos) const RTTR_NOEXCEPT;
203
209 RTTR_CONSTEXPR const_reference front() const;
210
216 RTTR_CONSTEXPR const_reference back() const;
217
227 RTTR_CONSTEXPR const_pointer data() const RTTR_NOEXCEPT;
228
233 RTTR_CXX14_CONSTEXPR void remove_prefix(size_type n) RTTR_NOEXCEPT;
234
239 RTTR_CXX14_CONSTEXPR void remove_suffix(size_type n) RTTR_NOEXCEPT;
240
241
247 template<typename Allocator>
248 explicit operator std::basic_string<CharT, Traits, Allocator>() const;
249
255 template<typename Allocator = std::allocator<CharT> >
256 std::basic_string<CharT, Traits> to_string(const Allocator& a = Allocator()) const;
257
266 RTTR_CXX14_CONSTEXPR int compare(basic_string_view v) const RTTR_NOEXCEPT;
267
275 RTTR_CXX14_CONSTEXPR int compare(const CharT* c) const RTTR_NOEXCEPT;
276
277 private:
278 const value_type* m_data;
279 size_type m_size;
280};
281
285// friend functions
286// MSVC 2013 cannot handle type deduction correctly, thats why we have to declare and
287// define all possibility compares explicitly
288
296template<typename CharT, typename Traits>
297RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator==(basic_string_view<CharT, Traits> lhs,
298 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
299
300template<typename CharT, typename Traits>
301RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator==(const char* lhs,
302 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
303
304template<typename CharT, typename Traits>
305RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator==(basic_string_view<CharT, Traits> lhs,
306 const char* rhs) RTTR_NOEXCEPT;
307
308template<typename CharT, typename Traits>
309RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator==(const std::basic_string<CharT, Traits>& lhs,
310 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
311
312template<typename CharT, typename Traits>
313RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator==(basic_string_view<CharT, Traits> lhs,
314 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
315
317
325template<typename CharT, typename Traits>
326RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator!=(basic_string_view<CharT, Traits> lhs,
327 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
328
329template<typename CharT, typename Traits>
330RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator!=(const char* lhs,
331 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
332
333template<typename CharT, typename Traits>
334RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator!=(basic_string_view<CharT, Traits> lhs,
335 const char* rhs) RTTR_NOEXCEPT;
336
337template<typename CharT, typename Traits>
338RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator!=(const std::basic_string<CharT, Traits>& lhs,
339 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
340
341template<typename CharT, typename Traits>
342RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator!=(basic_string_view<CharT, Traits> lhs,
343 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
344
346
354template<typename CharT, typename Traits>
355RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<=(basic_string_view<CharT, Traits> lhs,
356 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
357
358template<typename CharT, typename Traits>
359RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<=(const char* lhs,
360 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
361
362template<typename CharT, typename Traits>
363RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<=(basic_string_view<CharT, Traits> lhs,
364 const char* rhs) RTTR_NOEXCEPT;
365
366template<typename CharT, typename Traits>
367RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<=(const std::basic_string<CharT, Traits>& lhs,
368 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
369
370template<typename CharT, typename Traits>
371RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<=(basic_string_view<CharT, Traits> lhs,
372 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
373
375
383template<typename CharT, typename Traits>
384RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>=(basic_string_view<CharT, Traits> lhs,
385 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
386
387template<typename CharT, typename Traits>
388RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>=(const char* lhs,
389 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
390
391template<typename CharT, typename Traits>
392RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>=(basic_string_view<CharT, Traits> lhs,
393 const char* rhs) RTTR_NOEXCEPT;
394
395template<typename CharT, typename Traits>
396RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>=(const std::basic_string<CharT, Traits>& lhs,
397 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
398
399template<typename CharT, typename Traits>
400RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>=(basic_string_view<CharT, Traits> lhs,
401 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
402
404
412template<typename CharT, typename Traits>
413RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>(basic_string_view<CharT, Traits> lhs,
414 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
415
416template<typename CharT, typename Traits>
417RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>(const char* lhs,
418 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
419
420template<typename CharT, typename Traits>
421RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>(basic_string_view<CharT, Traits> lhs,
422 const char* rhs) RTTR_NOEXCEPT;
423
424template<typename CharT, typename Traits>
425RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>(const std::basic_string<CharT, Traits>& lhs,
426 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
427
428template<typename CharT, typename Traits>
429RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator>(basic_string_view<CharT, Traits> lhs,
430 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
431
433
441template<typename CharT, typename Traits>
442RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<(basic_string_view<CharT, Traits> lhs,
443 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
444
445template<typename CharT, typename Traits>
446RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<(const char* lhs,
447 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
448
449template<typename CharT, typename Traits>
450RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<(basic_string_view<CharT, Traits> lhs,
451 const char* rhs) RTTR_NOEXCEPT;
452
453template<typename CharT, typename Traits>
454RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<(const std::basic_string<CharT, Traits>& lhs,
455 basic_string_view<CharT, Traits> rhs) RTTR_NOEXCEPT;
456
457template<typename CharT, typename Traits>
458RTTR_INLINE RTTR_CXX14_CONSTEXPR bool operator<(basic_string_view<CharT, Traits> lhs,
459 const std::basic_string<CharT, Traits>& rhs) RTTR_NOEXCEPT;
460
462
463template<typename CharT, typename Traits>
464RTTR_INLINE std::basic_string<CharT, Traits> operator+(basic_string_view<CharT, Traits> lhs,
465 const std::basic_string<CharT, Traits>& rhs);
466
467template<typename CharT, typename Traits>
468RTTR_INLINE std::basic_string<CharT, Traits> operator+(const std::basic_string<CharT, Traits>& lhs,
469 basic_string_view<CharT, Traits> rhs);
470
471template<typename CharT, typename Traits>
472RTTR_INLINE std::basic_string<CharT, Traits> operator+(basic_string_view<CharT, Traits> lhs,
473 std::basic_string<CharT, Traits>&& rhs);
474
475template<typename CharT, typename Traits>
476RTTR_INLINE std::basic_string<CharT, Traits> operator+(std::basic_string<CharT, Traits>&& lhs,
477 basic_string_view<CharT, Traits> rhs);
478
480
486template <typename CharT, typename Traits>
487std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
488 basic_string_view<CharT, Traits> v);
489
494
495
496} // end namespace rttr
497
498#include "rttr/detail/impl/string_view_impl.h"
499
500#endif // RTTR_STRING_VIEW_H_
The class template basic_string_view describes an non-owning reference to a constant contiguous seque...
Definition string_view.h:49
constexpr const_reference back() const
constexpr void remove_suffix(size_type n) noexcept
constexpr const_iterator cbegin() const noexcept
const_pointer const_iterator
Definition string_view.h:57
constexpr int compare(basic_string_view v) const noexcept
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition string_view.h:59
const_reverse_iterator crbegin() const noexcept
constexpr const_iterator end() const noexcept
constexpr basic_string_view() noexcept
Default constructor.
std::size_t size_type
Definition string_view.h:61
CharT value_type
Definition string_view.h:52
constexpr const_iterator begin() const noexcept
CharT * pointer
Definition string_view.h:53
constexpr size_type length() const noexcept
const_iterator iterator
Definition string_view.h:58
const_reverse_iterator reverse_iterator
Definition string_view.h:60
const CharT & const_reference
Definition string_view.h:56
std::basic_string< char, std::char_traits< char > > to_string(const Allocator &a=Allocator()) const
constexpr size_type max_size() const noexcept
const_reverse_iterator rend() const noexcept
constexpr const_iterator cend() const noexcept
const_reverse_iterator crend() const noexcept
Traits traits_type
Definition string_view.h:51
const_reverse_iterator rbegin() const noexcept
std::ptrdiff_t difference_type
Definition string_view.h:62
constexpr const_pointer data() const noexcept
constexpr const_reference front() const
CharT & reference
Definition string_view.h:55
constexpr bool empty() const noexcept
const CharT * const_pointer
Definition string_view.h:54
static size_type npos
Definition string_view.h:63
constexpr size_type size() const noexcept
constexpr void swap(basic_string_view &s) noexcept
constexpr void remove_prefix(size_type n) noexcept
Definition access_levels.h:34
basic_string_view< char > string_view
A class to hold a reference to a continuous sequence of char objects.
Definition string_view.h:493