BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
cstr.h File Reference

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)
 

Detailed Description

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:

  • cstr Class: A template class that manages strings using a fixed-size buffer. Unlike 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.
  • Helper Functions: cstr_strlen and cstr_strcmp are utility functions for measuring string length and comparing strings, respectively.
  • Comparison Function Object: The cmp_str struct provides comparison operators for different sizes of cstr objects.
  • Utility Functions: The tostr functions convert cstr objects to std::string.

cstr Class Details

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.

  • Constructors:
  • Methods:
    • 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.

Differences from std::string

  • Fixed Size vs. Variable Size: The cstr class uses a fixed-size buffer, while std::string can dynamically change its size.
  • Memory Management: cstr does not involve dynamic memory allocation, making it more efficient in scenarios with frequent memory operations.
  • Performance: 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.
  • Flexibility: std::string can handle strings of arbitrary length, whereas cstr is limited by the size specified at compile time.

Usage Example

Below is an example of how to use this module:

#include "cstr.h"
int main() {
// Define fixed-length strings
cstr<16> str1("Hello");
cstr<16> str2("World");
// Concatenate strings
cstr<16> str3 = str1 + str2;
// Display strings
printf("str1: %s\n", str1.c_str());
printf("str2: %s\n", str2.c_str());
printf("str3: %s\n", str3.c_str());
// Compare strings
if (str1 == str2) {
printf("str1 and str2 are equal\n");
} else {
printf("str1 and str2 are not equal\n");
}
return 0;
}
Definition cstr.h:109
Module for managing fixed-size strings.
Version
1.0
Date
2024
Note
This module depends on the b8/type.h header for type definitions.