-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCString.hpp
80 lines (63 loc) · 1.98 KB
/
CString.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#ifndef LZ_C_STRING_HPP
#define LZ_C_STRING_HPP
#include "detail/BasicIteratorView.hpp"
#include "detail/iterators/CStringIterator.hpp"
namespace lz {
LZ_MODULE_EXPORT_SCOPE_BEGIN
template<class C, bool IsRandomAccess>
class CString final : public detail::BasicIteratorView<detail::CStringIterator<C, IsRandomAccess>> {
public:
using iterator = detail::CStringIterator<C, IsRandomAccess>;
using const_iterator = iterator;
using value_type = typename iterator::value_type;
constexpr CString(const C* begin, const C* end) noexcept :
detail::BasicIteratorView<iterator>(iterator(begin), iterator(end)) {
}
constexpr CString() = default;
template<bool RA = IsRandomAccess>
LZ_NODISCARD LZ_CONSTEXPR_CXX_17 detail::EnableIf<!RA, std::size_t> size() const noexcept {
std::size_t size = 0;
for (auto i = this->begin(); i != this->end(); ++i, ++size) {}
return size;
}
template<bool RA = IsRandomAccess>
LZ_NODISCARD LZ_CONSTEXPR_CXX_14 detail::EnableIf<RA, std::size_t> size() const noexcept {
return static_cast<std::size_t>(this->_end - this->_begin);
}
LZ_NODISCARD constexpr explicit operator bool() const noexcept {
return static_cast<bool>(this->begin());
}
};
/**
* @addtogroup ItFns
* @{
*/
/**
* Creates a c-string iterator with knowing its length
*
* @param begin Beginning of the c string
* @param end Ending of the c string
* @return CString object containing a random access iterator
*/
template<class C>
LZ_NODISCARD constexpr CString<C, true> cString(const C* begin, const C* end) noexcept {
return { begin, end };
}
/**
* Creates a c-string iterator without knowing its length
*
* @param s The c string
* @return CString object containing a forward iterator
*/
template<class C>
LZ_NODISCARD constexpr CString<C, false> cString(const C* s) noexcept {
return { s, nullptr };
}
// End of group
/**
* @}
*/
LZ_MODULE_EXPORT_SCOPE_END
} // namespace lz
#endif