|
BEEP-8 Helper Lib 1.0.0
|
Module for managing fixed-size strings. More...
#include <stdio.h>#include <b8/type.h>#include <cstddef>#include <cstring>#include <string>#include <charconv>Go to the source code of this file.
Classes | |
| class | cstr< TBYTES > |
| struct | cmp_str |
Typedefs | |
| using | str8 = cstr<8> |
| using | str16 = cstr<16> |
| using | str32 = cstr<32> |
| using | str64 = cstr<64> |
Functions | |
| size_t | cstr_strlen (const char *sz_) |
| int | cstr_strcmp (const char *s1, const char *s2) |
| std::string | tostr (const str8 &x_) |
| std::string | tostr (const str16 &x_) |
| std::string | tostr (const str32 &x_) |
| std::string | tostr (const str64 &x_) |
| template<typename String , typename T > | |
| std::from_chars_result | from_chars (const String &str, T &value) |
Module for managing fixed-size strings.
This module provides functionality for managing strings using the cstr class and associated functions. The cstr class offers a fixed-size buffer to store strings, simplifying memory management by avoiding dynamic allocations. It provides various string operations such as assignment, concatenation, comparison, and more.
The main components of this module are:
std::string, which uses dynamic memory, cstr uses a static array whose size is defined at compile time. This can lead to performance improvements in scenarios where memory allocation and deallocation are frequent.cstr_strlen and cstr_strcmp are utility functions for measuring string length and comparing strings, respectively.cmp_str struct provides comparison operators for different sizes of cstr objects.tostr functions convert cstr objects to std::string.The cstr class is a template class that holds a fixed-size character buffer. For example, cstr<16> can hold a string of up to 15 characters plus a null terminator. It uses the _set method internally for string operations.
cstr(const char* sz): Initializes the string with a C-style string.cstr(): Default constructor.const char* c_str() const: Returns the C-style string.void clear(): Clears the string.void push_back(char cc): Appends a character to the string.void pop_back(): Removes the last character from the string.cstr& operator=(const char* sz): Assigns a C-style string to the cstr object.cstr& operator+=(char ch): Appends a character to the string.cstr operator+(const cstr& s) const: Concatenates two cstr strings.bool operator==(const cstr& s) const: Compares two cstr strings.char at(u32 pos) const: Returns the character at the specified position.size_t size() const: Returns the length of the string.bool empty() const: Checks if the string is empty.cstr class uses a fixed-size buffer, while std::string can dynamically change its size.cstr does not involve dynamic memory allocation, making it more efficient in scenarios with frequent memory operations.cstr can offer performance benefits due to the absence of dynamic memory allocation and deallocation. However, std::string is more flexible for handling longer strings.std::string can handle strings of arbitrary length, whereas cstr is limited by the size specified at compile time.Below is an example of how to use this module: