libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal::String Class Reference
Inheritance diagram for gimbal::String:
GblStringBuffer gimbal::StringViewBase< String > gimbal::tags::StringBase gimbal::FlexibleString< ExtraStackSize >

Public Member Functions

const GblStringBuffergetString_ (void) const
 
GblStringBuffergetString_ (void)
 
 String (std::nullptr_t, Size size=sizeof(String))
 
 String (Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (const std::string &stdString, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (const char *pCStr, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (const std::string_view &stringView, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (const String &rhs, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (StringView other, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (String &&rhs, Context *pCtx=nullptr, Size size=sizeof(String))
 
 String (GblStringBuffer &&rhs, Context *pCtx=nullptr, Size size=sizeof(String))
 
auto take (void)
 
void give (std::pair< char *, Size > data)
 
void clear (void)
 
const Stringoperator= (std::string_view view)
 
const Stringoperator= (const String &rhs)
 
const Stringoperator= (StringView string)
 
const Stringoperator= (const std::string &stdString)
 
const Stringoperator= (const char *pCStr)
 
const Stringoperator= (std::nullptr_t)
 
const Stringoperator= (String &&rhs)
 
const Stringoperator= (GblStringBuffer &&rhs)
 
const Stringoperator+= (std::string_view view)
 
const Stringoperator+= (StringView view)
 
void reserve (Size capacity)
 
void resize (Size size)
 
void insert (const_iterator pos, const char *pString, GblSize count=0)
 
void concat (std::string_view view)
 
Stringvasprintf (const char *pFmt, va_list varArgs)
 
Stringsprintf (const char *pFmt,...)
 
template<typename... Args>
StringvarArgs (Args &&... args)
 
- Public Member Functions inherited from gimbal::StringViewBase< String >
 operator const GblStringBuffer * () const
 
const char & getElement_ (size_t index) const
 
char & getElement_ (size_t index)
 
void setElement_ (size_t index, char value)
 
size_t getElementCount_ (void) const
 
Size getStackBytes (void) const
 
constexpr Size getLength (void) const
 
constexpr Size getCapacity (void) const
 
ContextgetContext (void) const
 
constexpr bool isEmpty (void) const
 
constexpr bool isStack (void) const
 
constexpr bool isHeap (void) const
 
const char * getCString (void) const
 
char * getCString (void)
 
std::string toStdString (void) const
 
constexpr std::string_view toStringView (void) const
 

Static Public Member Functions

static std::pair< char *, Size > take (GblStringBuffer *pGblStr)
 

Friends

std::istream & operator>> (std::istream &input, String &s)
 
void swap (String &lhs, String &rhs)
 

Additional Inherited Members

- Public Types inherited from gimbal::StringViewBase< String >
using StringViewType = StringViewBase< String >
 
using Derived = String
 
using Iterable = RandomAccessIterable< StringViewType, size_t, char >
 
using iterator = typename Iterable::iterator
 
using const_iterator = typename Iterable::const_iterator
 
- Data Fields inherited from GblStringBuffer
GblArrayList data
 
- Protected Member Functions inherited from gimbal::StringViewBase< String >
decltype(auto) str_ (void) const
 
decltype(auto) str_ (void)
 

Detailed Description

Definition at line 184 of file gimbal_string.hpp.

Constructor & Destructor Documentation

◆ String() [1/9]

gimbal::String::String ( std::nullptr_t  ,
Size  size = sizeof(String) 
)
inline

Definition at line 201 of file gimbal_string.hpp.

201 :
202 String(static_cast<Context*>(nullptr), size) {}

◆ String() [2/9]

gimbal::String::String ( Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 204 of file gimbal_string.hpp.

204 :
205 String(std::string_view{}, pCtx, size) {}

◆ String() [3/9]

gimbal::String::String ( const std::string &  stdString,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 208 of file gimbal_string.hpp.

208 :
209 String(std::string_view{stdString.c_str()}, pCtx, size) {}

◆ String() [4/9]

gimbal::String::String ( const char *  pCStr,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 216 of file gimbal_string.hpp.

216 :
217 String(pCStr? std::string_view{pCStr} : std::string_view{}, pCtx, size) {}

◆ String() [5/9]

gimbal::String::String ( const std::string_view &  stringView,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 219 of file gimbal_string.hpp.

219 {
220 Exception::checkThrow(GblStringBuffer_construct(this,
221 GBL_STRING_VIEW(stringView.data(), stringView.length()),
222 size,
223 pCtx));
224 }
GBL_RESULT GblStringBuffer_construct(GblStringBuffer *pSelf, const char *pString, size_t length, size_t structSize, GblContext *pCtx)
Constructs the given GblStringBuffer struct with any given initial values (or defaults),...

◆ String() [6/9]

gimbal::String::String ( const String rhs,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 227 of file gimbal_string.hpp.

227 :
228 String(StringView(rhs), pCtx, size) {}

◆ String() [7/9]

gimbal::String::String ( StringView  other,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 230 of file gimbal_string.hpp.

230 :
231 String(other.toStringView(), pCtx? pCtx : other.getContext(), size) {}

◆ String() [8/9]

gimbal::String::String ( String &&  rhs,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 234 of file gimbal_string.hpp.

234 :
235 String(static_cast<GblStringBuffer&&>(rhs), pCtx, size) {}
Mutable string type optimized for building and writing.

◆ String() [9/9]

gimbal::String::String ( GblStringBuffer &&  rhs,
Context pCtx = nullptr,
Size  size = sizeof(String) 
)
inline

Definition at line 237 of file gimbal_string.hpp.

237 :
238 String(pCtx, size)
239 {
240 *this = rhs;
241 }

◆ ~String()

gimbal::String::~String ( void  )
inline

Definition at line 243 of file gimbal_string.hpp.

243 {
244 Exception::checkThrow(GblStringBuffer_destruct(this));
245 }
GBL_RESULT GblStringBuffer_destruct(GblStringBuffer *pSelf)
Destructs the given GblStringBuffer structure, releasing any allocated resources and returning a resu...

Member Function Documentation

◆ getString_() [1/2]

const GblStringBuffer * gimbal::String::getString_ ( void  ) const
inline

Definition at line 190 of file gimbal_string.hpp.

190 {
191 return static_cast<const GblStringBuffer*>(this);
192 }

◆ getString_() [2/2]

GblStringBuffer * gimbal::String::getString_ ( void  )
inline

Definition at line 194 of file gimbal_string.hpp.

194 {
195 return static_cast<GblStringBuffer*>(this);
196 }

◆ take() [1/2]

static std::pair< char *, Size > gimbal::String::take ( GblStringBuffer pGblStr)
inlinestatic

Definition at line 247 of file gimbal_string.hpp.

247 {
248 char* pCStr = nullptr;
249 Size capacity = 0;
250 Exception::checkThrow(GblStringBuffer_release(pGblStr, &pCStr, &capacity));
251 return { pCStr, capacity };
252 }
GBL_RESULT GblStringBuffer_release(GblStringBuffer *pSelf, char **ppStrPtr, size_t *pCapacity)
Takes ownership of the given GblStringBuffer's internal buffer, also returning its capacity and a res...

◆ take() [2/2]

auto gimbal::String::take ( void  )
inline

Definition at line 254 of file gimbal_string.hpp.

254 {
255 return take(this);
256 }

◆ give()

void gimbal::String::give ( std::pair< char *, Size >  data)
inline

Definition at line 258 of file gimbal_string.hpp.

258 {
259 Exception::checkThrow(GblStringBuffer_acquire(this, data.first, data.second));
260 }
GBL_RESULT GblStringBuffer_acquire(GblStringBuffer *pSelf, char *pData, size_t capacity)
Transfers ownership of a given C string to the given buffer, assigning its value and updating its cap...
GblArrayList data
Internal storage buffer.

◆ clear()

void gimbal::String::clear ( void  )
inline

Definition at line 262 of file gimbal_string.hpp.

262 {
263 Exception::checkThrow(GblStringBuffer_clear(this));
264 }
GBL_RESULT GblStringBuffer_clear(GblStringBuffer *pSelf)
Clears the contents of the given GblStringBuffer, resetting it back to an empty state and returning a...

◆ operator=() [1/8]

const String & gimbal::String::operator= ( std::string_view  view)
inline

Definition at line 266 of file gimbal_string.hpp.

266 {
267 Exception::checkThrow(GblStringBuffer_set(this, GBL_STRING_VIEW(view.data(), view.size())));
268 return *this;
269 }
const char * GblStringBuffer_set(GblStringBuffer *pSelf, const char *pStr, size_t len)
Assigns the value of the given GblStringBuffer to pStr, optionally taking its length,...

◆ operator=() [2/8]

const String & gimbal::String::operator= ( const String rhs)
inline

Definition at line 271 of file gimbal_string.hpp.

271 {
272 return *this = StringView(rhs);
273 }

◆ operator=() [3/8]

const String & gimbal::String::operator= ( StringView  string)
inline

Definition at line 275 of file gimbal_string.hpp.

275 {
276 return *this = string.toStringView();
277 }

◆ operator=() [4/8]

const String & gimbal::String::operator= ( const std::string &  stdString)
inline

Definition at line 279 of file gimbal_string.hpp.

279 {
280 return *this = stdString.c_str();
281 }

◆ operator=() [5/8]

const String & gimbal::String::operator= ( const char *  pCStr)
inline

Definition at line 289 of file gimbal_string.hpp.

289 {
290 if(pCStr) return *this = std::string_view(pCStr);
291 else return *this = nullptr;
292 }

◆ operator=() [6/8]

const String & gimbal::String::operator= ( std::nullptr_t  )
inline

Definition at line 294 of file gimbal_string.hpp.

294 {
295 clear();
296 return *this;
297 }

◆ operator=() [7/8]

const String & gimbal::String::operator= ( String &&  rhs)
inline

Definition at line 299 of file gimbal_string.hpp.

299 {
300 return *this = static_cast<GblStringBuffer&&>(rhs);
301 }

◆ operator=() [8/8]

const String & gimbal::String::operator= ( GblStringBuffer &&  rhs)
inline

Definition at line 303 of file gimbal_string.hpp.

303 {
304 StringView rhsView(rhs);
305
306 if(getContext() == rhsView.getContext() && rhsView.isHeap()) {
307 give(take(&rhs));
308 } else { //cannot move construct!
309 *this = rhsView.toStringView();
310 }
311
312 return *this;
313 }

◆ operator+=() [1/2]

const String & gimbal::String::operator+= ( std::string_view  view)
inline

Definition at line 315 of file gimbal_string.hpp.

315 {
316 concat(view);
317 return *this;
318 }

◆ operator+=() [2/2]

const String & gimbal::String::operator+= ( StringView  view)
inline

Definition at line 320 of file gimbal_string.hpp.

320 {
321 return *this += view.toStringView();
322 }

◆ reserve()

void gimbal::String::reserve ( Size  capacity)
inline

Definition at line 324 of file gimbal_string.hpp.

324 {
325 Exception::checkThrow(GblStringBuffer_reserve(this, capacity));
326 }
GBL_RESULT GblStringBuffer_reserve(GblStringBuffer *pSelf, size_t capacity)
Ensures that there is room for capacity characters within the given GblStringBuffer,...

◆ resize()

void gimbal::String::resize ( Size  size)
inline

Definition at line 328 of file gimbal_string.hpp.

328 {
329 Exception::checkThrow(GblStringBuffer_resize(this, size));
330 }
GBL_RESULT GblStringBuffer_resize(GblStringBuffer *pSelf, size_t size)
Sets the length of the given GblStringBuffer to size, regrowing if necessary, but not shrinking,...

◆ insert()

void gimbal::String::insert ( const_iterator  pos,
const char *  pString,
GblSize  count = 0 
)
inline

Definition at line 332 of file gimbal_string.hpp.

332 {
333 GBL_ASSERT(pString);
334 const auto index = std::distance(cbegin(), pos);
335 Exception::checkThrow(GblStringBuffer_insert(this, index, GBL_STRING_VIEW(pString, count)));
336 // return iterator(*this, index);
337 }
GBL_RESULT GblStringBuffer_insert(GblStringBuffer *pSelf, size_t index, const char *pString, size_t length)
Inserts pString at the given index with optional length, returning a result code.

◆ concat()

void gimbal::String::concat ( std::string_view  view)
inline

Definition at line 339 of file gimbal_string.hpp.

339 {
340 //insert(cend(), view.data(), view.size());
341 Exception::checkThrow(GblArrayList_append(&this->data, view.data(), view.size()));
342 }

◆ vasprintf()

String & gimbal::String::vasprintf ( const char *  pFmt,
va_list  varArgs 
)
inline

Definition at line 344 of file gimbal_string.hpp.

344 {
345 Exception::checkThrow(GblStringBuffer_appendVPrintf(this, pFmt, varArgs));
346 return *this;
347 }
GBL_RESULT GblStringBuffer_appendVPrintf(GblStringBuffer *pSelf, const char *pFmt, va_list varArgs)
Equivalent to GblStringBuffer_appendPrintf(), except additional arguments are provided as a va_list.

◆ sprintf()

String & gimbal::String::sprintf ( const char *  pFmt,
  ... 
)
inline

Definition at line 350 of file gimbal_string.hpp.

350 {
351 va_list varArgs;
352 va_start(varArgs, pFmt);
353 vasprintf(pFmt, varArgs);
354 va_end(varArgs);
355 return *this;
356 }

◆ varArgs()

template<typename... Args>
String & gimbal::String::varArgs ( Args &&...  args)
inline

Definition at line 360 of file gimbal_string.hpp.

360 {
361 String temp("", getContext());
362 temp.sprintf(getCString(), std::forward<Args>(args)...);
363 *this = temp;
364 return *this;
365 }

Friends And Related Symbol Documentation

◆ operator>>

std::istream & operator>> ( std::istream &  input,
String s 
)
friend

Definition at line 368 of file gimbal_string.hpp.

368 {
369 std::string line;
370 // Still failes gracefully, so whatever...
371 //if(input.good()) {
372 // input.clear();
373 std::getline(input, line);
374 // if(input.fail()) {
375 // Exception::checkThrow(Result::FileRead);
376 // }
377 s = line;
378 //}
379 return input;
380 }

◆ swap

void swap ( String lhs,
String rhs 
)
friend

Definition at line 382 of file gimbal_string.hpp.

382 {
383 void* pTemp = GBL_ALLOCA(sizeof(String));
384 memcpy(pTemp, &lhs, sizeof(String));
385 memcpy(&lhs, &rhs, sizeof(String));
386 memcpy(&rhs, pTemp, sizeof(String));
387 }

The documentation for this class was generated from the following file: