roo_collections
API Documentation for roo_collections
Loading...
Searching...
No Matches
small_string.h
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief Fixed-capacity string utility type.
5/// @ingroup roo_collections
6
7#include <stddef.h>
8
9#include <algorithm>
10#include <cstring>
11#include <string>
12
13#include "roo_backport.h"
14#include "roo_backport/string_view.h"
16
17namespace roo_collections {
18
19/// @brief Fixed-capacity string stored inline (no heap allocation).
20///
21/// Intended for short, bounded identifiers and other small keys/values where a
22/// constant memory footprint is preferred over unbounded growth.
23///
24/// @tparam N Capacity of the internal storage buffer, in bytes.
25template <size_t N>
27 public:
28 /// @brief Maximum storage capacity (including the trailing '\0').
29 static constexpr size_t kCapacity = N;
30
31 /// @brief Creates an empty string.
32 SmallString() { data_[0] = 0; }
33
34 /// @brief Constructs from a C string.
35 /// @param str Null-terminated source string.
36 SmallString(const char* str) { strncpy(data_, str, N); }
37
38 /// @brief Constructs from `std::string`.
39 /// @param str Source string.
40 SmallString(const std::string& str) { strncpy(data_, str.c_str(), N); }
41
42 /// @brief Constructs from `roo::string_view`.
43 /// @param str Source view.
44 SmallString(const roo::string_view& str) {
45 strncpy(data_, str.data(), std::min(N, str.size() + 1));
46 }
47
48 /// @brief Copy constructor.
49 SmallString(const SmallString& other) { strncpy(data_, other.data_, N); }
50
51 /// @brief Copy assignment.
53 strncpy(data_, other.data_, N);
54 return *this;
55 }
56
57 /// @brief Assigns from C string.
58 SmallString& operator=(const char* other) {
59 strncpy(data_, other, N);
60 return *this;
61 }
62
63 /// @brief Assigns from `roo::string_view`.
64 SmallString& operator=(roo::string_view other) {
65 strncpy(data_, other.data(), std::min(N, other.size() + 1));
66 return *this;
67 }
68
69 /// @brief Returns the string length.
70 /// @return Length in characters.
71 size_t length() const { return strlen(data_); }
72
73 /// @brief Returns pointer to null-terminated character data.
74 const char* c_str() const { return data_; }
75
76 /// @brief Checks whether the string is empty.
77 /// @return `true` when empty.
78 bool empty() const { return data_[0] == 0; }
79
80 /// @brief Equality comparison.
81 bool operator==(const SmallString& other) const {
82 return strcmp(data_, other.data_) == 0;
83 }
84
85 /// @brief Inequality comparison.
86 bool operator!=(const SmallString& other) const { return !operator==(other); }
87
88 /// @brief Implicit conversion to `roo::string_view`.
89 operator roo::string_view() const {
90 return roo::string_view(data_, length());
91 }
92
93 private:
94 char data_[N];
95};
96
97} // namespace roo_collections
Fixed-capacity string stored inline (no heap allocation).
bool operator==(const SmallString &other) const
Equality comparison.
SmallString(const std::string &str)
Constructs from std::string.
SmallString(const char *str)
Constructs from a C string.
size_t length() const
Returns the string length.
bool empty() const
Checks whether the string is empty.
SmallString(const roo::string_view &str)
Constructs from roo::string_view.
static constexpr size_t kCapacity
Maximum storage capacity (including the trailing '\0').
SmallString(const SmallString &other)
Copy constructor.
const char * c_str() const
Returns pointer to null-terminated character data.
SmallString & operator=(const SmallString &other)
Copy assignment.
SmallString & operator=(const char *other)
Assigns from C string.
SmallString()
Creates an empty string.
bool operator!=(const SmallString &other) const
Inequality comparison.
SmallString & operator=(roo::string_view other)
Assigns from roo::string_view.
Hashing utilities used by roo_collections containers.