libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
libxr_string.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstring>
6
7#include "libxr_def.hpp"
8
9namespace LibXR
10{
11
19template <unsigned int MaxLength>
20class String
21{
22 public:
29
36 String(const char *str)
37 {
38 std::strncpy(&raw_string_[0], str, MaxLength);
39 raw_string_[MaxLength] = '\0';
40 }
41
49 String(const char *str, size_t len)
50 {
52 std::strncpy(&raw_string_[0], str, copy_len);
53 raw_string_[copy_len] = '\0'; // 确保字符串终止
54 }
55
62 const char *Raw() const { return &raw_string_[0]; }
63
71 char operator[](unsigned int i)
72 {
73 ASSERT(i < MaxLength);
74 return raw_string_[i];
75 }
76
84 char operator[](unsigned int i) const
85 {
86 ASSERT(i < MaxLength);
87 return raw_string_[i];
88 }
89
97 const char *operator+(unsigned int i) { return &raw_string_[i]; }
98
106 String &operator+=(const char *str)
107 {
108 auto len = strnlen(this->Raw(), MaxLength);
109 size_t copy_len = std::min(MaxLength - len, std::strlen(str));
110 std::strncat(&raw_string_[0], str, copy_len);
111 raw_string_[MaxLength] = '\0';
112 return *this;
113 }
114
122 int Find(const char *str) const
123 {
124 if (!str)
125 {
126 return -1;
127 }
128 const char *result = std::strstr(this->Raw(), str);
129 return result ? static_cast<int>(result - this->Raw()) : -1;
130 }
131
140 template <unsigned int NextStrLength>
142 {
143 return strncmp(this->Raw(), other.Raw(), MaxLength) == 0;
144 }
145
154 template <unsigned int NextStrLength>
156 {
157 return strncmp(this->Raw(), other.Raw(), MaxLength) != 0;
158 }
159
168 template <unsigned int NextStrLength>
170 {
171 return strncmp(this->Raw(), other.Raw(), MaxLength) < 0;
172 }
173
182 template <unsigned int NextStrLength>
184 {
185 return strncmp(this->Raw(), other.Raw(), MaxLength) > 0;
186 }
187
196 template <unsigned int NextStrLength>
198 {
199 return strncmp(this->Raw(), other.Raw(), MaxLength) <= 0;
200 }
201
210 template <unsigned int NextStrLength>
212 {
213 return strncmp(this->Raw(), other.Raw(), MaxLength) >= 0;
214 }
215
222 size_t Length() const { return strnlen(this->Raw(), MaxLength); }
223
229 void Clear() { raw_string_[0] = '\0'; }
230
239 template <unsigned int SubStrLength>
241 {
242 ASSERT(pos < MaxLength);
245 }
246
247 private:
248 std::array<char, MaxLength + 1> raw_string_;
250};
251
252} // namespace LibXR
A fixed-length string class with safe operations.
int Find(const char *str) const
Finds the first occurrence of a substring.
bool operator==(const String< NextStrLength > &other) const
Compares two strings for equality.
bool operator!=(const String< NextStrLength > &other) const
Compares two strings for inequality.
String(const char *str)
Constructs a String from a C-style string.
String< SubStrLength > Substr(size_t pos) const
Extracts a substring starting at a given position.
bool operator>(const String< NextStrLength > &other) const
Greater than comparison between two strings.
String(const char *str, size_t len)
Constructs a String from a C-style string with a given length.
char operator[](unsigned int i)
Accesses a character at a given index.
char operator[](unsigned int i) const
Accesses a character at a given index (const version).
std::array< char, MaxLength+1 > raw_string_
bool operator<(const String< NextStrLength > &other) const
Less than comparison between two strings.
bool operator>=(const String< NextStrLength > &other) const
Greater than or equal comparison.
bool operator<=(const String< NextStrLength > &other) const
Less than or equal comparison.
size_t Length() const
Gets the length of the string.
String & operator+=(const char *str)
Appends a C-style string to the current string.
void Clear()
Clears the string, making it empty.
const char * operator+(unsigned int i)
Returns a pointer to a substring starting from index i.
String()
Default constructor, initializes an empty string.
const char * Raw() const
Gets the raw C-style string.
LibXR Color Control Library / LibXR终端颜色控制库
Definition esp_gpio.hpp:8
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值