BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
cstr.h
Go to the documentation of this file.
1
98#pragma once
99#include <stdio.h>
100#include <b8/type.h>
101#include <cstddef>
102#include <cstring>
103#include <string>
104#include <charconv>
105
106extern size_t cstr_strlen( const char* sz_ );
107extern int cstr_strcmp(const char *s1, const char *s2);
108template <unsigned int TBYTES>
109class cstr{
110 char _buff[ TBYTES ];
111
112 void _set( u32 idx_ , const char* sz ){
113 u32 nn=idx_;
114 if( sz ){
115 for( ; nn<=TBYTES-2 ; ++nn,++sz){
116 if( 0 == *sz ) break;
117 _buff[ nn ] = *sz;
118 }
119 }
120 _buff[ nn ] = 0x00;
121 }
122
123public:
124 const char* c_str() const {
125 return _buff;
126 }
127
128 const char* data() const {
129 return _buff;
130 }
131
132 void clear() {
133 _buff[0] = 0x00;
134 }
135
136 void push_back( char cc ){
137 char buff[2]={cc,0x00};
138 _set(size(),buff);
139 }
140
141 void pop_back(){
142 if(empty()) return;
143 const size_t idx = size()-1;
144 _buff[ idx ] = 0x00;
145 }
146
147 cstr& operator = (const char* sz) {
148 _set( 0, sz );
149 return *this;
150 }
151
152 cstr& operator = (const std::string& str) {
153 _set( 0, str.c_str() );
154 return *this;
155 }
156
157 cstr& operator += (char ch) {
158 char sz[2];
159 sz[0]=ch;
160 sz[1]=0x00;
161 _set(size(),sz);
162 return *this;
163 }
164
165 cstr& operator += (const char* sz) {
166 if( nullptr == sz ) return *this;
167 _set(size(),sz);
168 return *this;
169 }
170
171 cstr& operator += (const cstr& str ) {
172 _set(size(),str.c_str());
173 return *this;
174 }
175
176 cstr operator+(const cstr& s) const {
177 cstr ret = *this;
178 ret += s;
179 return ret;
180 }
181
182 bool operator == ( const cstr& s ) const {
183 return cstr_strcmp( this->c_str(), s.c_str()) == 0;
184 }
185
186 char at( u32 pos ) const {
187 if( pos < size()){
188 return _buff[pos];
189 }
190 return 0x00;
191 }
192
193 size_t size() const{
194 return cstr_strlen( _buff );
195 }
196
197 bool empty() const{
198 return 0 == size() ? true : false;
199 }
200 cstr( const char* sz ){ _set(0, sz ); }
201 cstr(){ _set(0,""); }
202};
203
204using str8 = cstr<8>;
205using str16 = cstr<16>;
206using str32 = cstr<32>;
207using str64 = cstr<64>;
208
209struct cmp_str {
210 bool operator()(const str8& a, const str8& b) const {
211 return cstr_strcmp(a.c_str(), b.c_str()) < 0;
212 }
213 bool operator()(const str16& a, const str16& b) const {
214 return cstr_strcmp(a.c_str(), b.c_str()) < 0;
215 }
216 bool operator()(const str32& a, const str32& b) const {
217 return cstr_strcmp(a.c_str(), b.c_str()) < 0;
218 }
219 bool operator()(const str64& a, const str64& b) const {
220 return cstr_strcmp(a.c_str(), b.c_str()) < 0;
221 }
222};
223
224inline std::string tostr( const str8 & x_ ){ return x_.c_str(); }
225inline std::string tostr( const str16& x_ ){ return x_.c_str(); }
226inline std::string tostr( const str32& x_ ){ return x_.c_str(); }
227inline std::string tostr( const str64& x_ ){ return x_.c_str(); }
228
229template<typename String, typename T>
230std::from_chars_result from_chars(const String& str, T& value) {
231 return std::from_chars(str.data(), str.data() + str.size(), value);
232}
Definition cstr.h:109
Definition cstr.h:209