libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_string_ref.hpp
1#ifndef GIMBAL_STRING_REF_HPP
2#define GIMBAL_STRING_REF_HPP
3
4#include <cstdint>
5
8
9namespace gbl {
10
11class StringRef {
12private:
13 GblStringRef* pRef_ = nullptr;
14
15public:
16
17 StringRef() noexcept = default;
18
19 StringRef(const char* pStr, std::size_t len=0) noexcept:
20 pRef_(GblStringRef_create(pStr, len)) { }
21
22 StringRef(const StringRef& rhs) noexcept:
23 pRef_(GblStringRef_ref(rhs.pRef_)) { }
24
25 StringRef(StringRef&& rhs) noexcept:
26 pRef_(rhs.pRef_)
27 {
28 rhs.pRef_ = nullptr;
29 }
30
31 StringRef fromGblRef(GblStringRef* pRef) noexcept {
32 StringRef ref;
33 ref.pRef_ = pRef;
34 return ref;
35 }
36
37 GblStringRef* toGblRef() noexcept {
38 GblStringRef* pTemp = pRef_;
39 pRef_ = nullptr;
40 return pTemp;
41 }
42
43 ~StringRef() noexcept {
45 }
46
47 StringRef& operator=(const char* pCStr) noexcept {
49 GblStringRef_create(pCStr);
50 return *this;
51 }
52
53 StringRef& operator=(const StringRef& rhs) noexcept {
55 GblStringRef_ref(rhs.pRef_);
56 return *this;
57 }
58
59 StringRef& operator=(StringRef&& rhs) noexcept {
61 pRef_ = rhs.pRef_;
62 rhs.pRef_ = nullptr;
63 return *this;
64 }
65
66 operator const char*() const noexcept {
67 return pRef_;
68 }
69
70 char operator[](std::size_t index) const {
71 if(index >= length())
72 throw std::out_of_range {
73 "Attempt to index StringRef out-of-range!"
74 };
75
76 return pRef_[index];
77 }
78
79 StringView view(std::size_t offset=0, std::size_t len=0) const {
80 if(!len)
81 len = length() - offset;
82
83 if(offset + len > length())
84 throw std::out_of_range {
85 "Attempt to create out-of-range StringView from StringRef"
86 };
87
88
89 return GblStringView {
90 .pData = pRef_ + offset,
91 .nullTerminated = (offset + len == length()),
92 .length = len
93 };
94 }
95
96 std::size_t refCount() const noexcept {
97 return GblStringRef_refCount(pRef_);
98 }
99
100 std::size_t length() const noexcept {
101 return GblStringRef_length(pRef_);
102 }
103
104 bool valid() const noexcept {
105 return GblStringRef_valid(pRef_);
106 }
107
108 bool empty() const noexcept {
109 return GblStringRef_empty(pRef_);
110 }
111
112 bool blank() const noexcept {
113 return GblStringRef_blank(pRef_);
114 }
115
116 // comparisons
117 // ostream
118 // hash
119 // swap
120 // better creation? snprintf()-y stuff?
121};
122
123}
124
125#endif // GIMBAL_STRING_REF_HPP
size_t GblStringRef_length(const GblStringRef *pSelf)
Returns the cached length of the given GblStringRef.
GblBool GblStringRef_blank(const GblStringRef *pSelf)
Returns whether the given GblStringRef is blank, containing only NULL or spacing characters.
GblBool GblStringRef_valid(const GblStringRef *pSelf)
Returns whether the given GblStringRef is valid (not NULL)
GblBool GblStringRef_empty(const GblStringRef *pSelf)
Returns whether the given GblStringRef is empty, with nothing but a NULL terminator.
GblRefCount GblStringRef_refCount(const GblStringRef *pSelf)
Returns the number of active references remaining to the given GblStringRef.
GblRefCount GblStringRef_unref(const GblStringRef *pRef)
Releases a reference to pRef, freeing the allocation if it was the last, returning the new refCount.
GblStringRef * GblStringRef_ref(const GblStringRef *pRef)
Returns a new reference to pRef, incrementing its internal reference count rather than actually copyi...
const char GblStringRef
Reference-counted, const char*-compatible string type.
size_t nullTerminated
Reserved bit for maintaining whether the string is NULL terminated or not.
const char * pData
Start address of the string being viewed.
size_t length
Length (bytes) of the substring being viewed.
OO C++ binding object around GblStringView.