libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::String< MaxLength > Class Template Reference

A fixed-length string class with safe operations. More...

#include <libxr_string.hpp>

Public Member Functions

 String ()
 Default constructor, initializes an empty string.
 
 String (const char *str)
 Constructs a String from a C-style string.
 
 String (const char *str, size_t len)
 Constructs a String from a C-style string with a given length.
 
const char * Raw () const
 Gets the raw C-style string.
 
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).
 
const char * operator+ (unsigned int i)
 Returns a pointer to a substring starting from index i.
 
Stringoperator+= (const char *str)
 Appends a C-style string to the current string.
 
int Find (const char *str) const
 Finds the first occurrence of a substring.
 
template<unsigned int NextStrLength>
bool operator== (const String< NextStrLength > &other) const
 Compares two strings for equality.
 
template<unsigned int NextStrLength>
bool operator!= (const String< NextStrLength > &other) const
 Compares two strings for inequality.
 
template<unsigned int NextStrLength>
bool operator< (const String< NextStrLength > &other) const
 Less than comparison between two strings.
 
template<unsigned int NextStrLength>
bool operator> (const String< NextStrLength > &other) const
 Greater than comparison between two strings.
 
template<unsigned int NextStrLength>
bool operator<= (const String< NextStrLength > &other) const
 Less than or equal comparison.
 
template<unsigned int NextStrLength>
bool operator>= (const String< NextStrLength > &other) const
 Greater than or equal comparison.
 
size_t Length () const
 Gets the length of the string.
 
void Clear ()
 Clears the string, making it empty.
 
template<unsigned int SubStrLength>
String< SubStrLength > Substr (size_t pos) const
 Extracts a substring starting at a given position.
 

Private Attributes

std::array< char, MaxLength+1 > raw_string_
 

Detailed Description

template<unsigned int MaxLength>
class LibXR::String< MaxLength >

A fixed-length string class with safe operations.

Template Parameters
MaxLengthThe maximum length of the string.

具有固定长度的字符串类,提供安全的字符串操作。

Definition at line 20 of file libxr_string.hpp.

Constructor & Destructor Documentation

◆ String() [1/3]

template<unsigned int MaxLength>
LibXR::String< MaxLength >::String ( )
inline

Default constructor, initializes an empty string.

默认构造函数,初始化为空字符串。

Definition at line 28 of file libxr_string.hpp.

28: raw_string_() {}
std::array< char, MaxLength+1 > raw_string_

◆ String() [2/3]

template<unsigned int MaxLength>
LibXR::String< MaxLength >::String ( const char * str)
inline

Constructs a String from a C-style string.

Parameters
strThe C-style string to copy from.

从 C 风格字符串构造 String。

Definition at line 36 of file libxr_string.hpp.

37 {
38 std::strncpy(&raw_string_[0], str, MaxLength);
39 raw_string_[MaxLength] = '\0';
40 }

◆ String() [3/3]

template<unsigned int MaxLength>
LibXR::String< MaxLength >::String ( const char * str,
size_t len )
inline

Constructs a String from a C-style string with a given length.

Parameters
strThe C-style string to copy from.
lenThe maximum number of characters to copy.

从 C 风格字符串构造 String,并指定最大拷贝长度。

Definition at line 49 of file libxr_string.hpp.

50 {
51 size_t copy_len = LibXR::min(MaxLength, len);
52 std::strncpy(&raw_string_[0], str, copy_len);
53 raw_string_[copy_len] = '\0'; // 确保字符串终止
54 }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Member Function Documentation

◆ Clear()

template<unsigned int MaxLength>
void LibXR::String< MaxLength >::Clear ( )
inline

Clears the string, making it empty.

清空字符串,使其变为空字符串。

Definition at line 229 of file libxr_string.hpp.

229{ raw_string_[0] = '\0'; }

◆ Find()

template<unsigned int MaxLength>
int LibXR::String< MaxLength >::Find ( const char * str) const
inline

Finds the first occurrence of a substring.

Parameters
strThe substring to search for.
Returns
The index of the first occurrence, or -1 if not found.

查找子字符串的首次出现位置。

Definition at line 122 of file libxr_string.hpp.

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 }
const char * Raw() const
Gets the raw C-style string.

◆ Length()

template<unsigned int MaxLength>
size_t LibXR::String< MaxLength >::Length ( ) const
inline

Gets the length of the string.

Returns
The length of the string.

获取字符串的长度。

Definition at line 222 of file libxr_string.hpp.

222{ return strnlen(this->Raw(), MaxLength); }

◆ operator!=()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator!= ( const String< NextStrLength > & other) const
inline

Compares two strings for inequality.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if not equal, false otherwise.

比较两个字符串是否不相等。

Definition at line 155 of file libxr_string.hpp.

156 {
157 return strncmp(this->Raw(), other.Raw(), MaxLength) != 0;
158 }

◆ operator+()

template<unsigned int MaxLength>
const char * LibXR::String< MaxLength >::operator+ ( unsigned int i)
inline

Returns a pointer to a substring starting from index i.

Parameters
iThe starting index.
Returns
A pointer to the substring.

返回从索引 i 开始的子字符串的指针。

Definition at line 97 of file libxr_string.hpp.

97{ return &raw_string_[i]; }

◆ operator+=()

template<unsigned int MaxLength>
String & LibXR::String< MaxLength >::operator+= ( const char * str)
inline

Appends a C-style string to the current string.

Parameters
strThe string to append.
Returns
Reference to the modified string.

将 C 风格字符串追加到当前字符串。

Definition at line 106 of file libxr_string.hpp.

107 {
108 auto len = strnlen(this->Raw(), MaxLength);
109 size_t copy_len = LibXR::min(MaxLength - len, std::strlen(str));
110 std::strncat(&raw_string_[0], str, copy_len);
111 raw_string_[MaxLength] = '\0';
112 return *this;
113 }

◆ operator<()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator< ( const String< NextStrLength > & other) const
inline

Less than comparison between two strings.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if this string is less than the other.

比较两个字符串,判断当前字符串是否小于另一个字符串。

Definition at line 169 of file libxr_string.hpp.

170 {
171 return strncmp(this->Raw(), other.Raw(), MaxLength) < 0;
172 }

◆ operator<=()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator<= ( const String< NextStrLength > & other) const
inline

Less than or equal comparison.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if this string is less than or equal to the other.

比较两个字符串,判断当前字符串是否小于或等于另一个字符串。

Definition at line 197 of file libxr_string.hpp.

198 {
199 return strncmp(this->Raw(), other.Raw(), MaxLength) <= 0;
200 }

◆ operator==()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator== ( const String< NextStrLength > & other) const
inline

Compares two strings for equality.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if equal, false otherwise.

比较两个字符串是否相等。

Definition at line 141 of file libxr_string.hpp.

142 {
143 return strncmp(this->Raw(), other.Raw(), MaxLength) == 0;
144 }

◆ operator>()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator> ( const String< NextStrLength > & other) const
inline

Greater than comparison between two strings.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if this string is greater than the other.

比较两个字符串,判断当前字符串是否大于另一个字符串。

Definition at line 183 of file libxr_string.hpp.

184 {
185 return strncmp(this->Raw(), other.Raw(), MaxLength) > 0;
186 }

◆ operator>=()

template<unsigned int MaxLength>
template<unsigned int NextStrLength>
bool LibXR::String< MaxLength >::operator>= ( const String< NextStrLength > & other) const
inline

Greater than or equal comparison.

Template Parameters
NextStrLengthThe length of the other string.
Parameters
otherThe other string to compare.
Returns
True if this string is greater than or equal to the other.

比较两个字符串,判断当前字符串是否大于或等于另一个字符串。

Definition at line 211 of file libxr_string.hpp.

212 {
213 return strncmp(this->Raw(), other.Raw(), MaxLength) >= 0;
214 }

◆ operator[]() [1/2]

template<unsigned int MaxLength>
char LibXR::String< MaxLength >::operator[] ( unsigned int i)
inline

Accesses a character at a given index.

Parameters
iThe index of the character.
Returns
The character at the specified index.

访问指定索引的字符。

Definition at line 71 of file libxr_string.hpp.

72 {
73 ASSERT(i < MaxLength);
74 return raw_string_[i];
75 }

◆ operator[]() [2/2]

template<unsigned int MaxLength>
char LibXR::String< MaxLength >::operator[] ( unsigned int i) const
inline

Accesses a character at a given index (const version).

Parameters
iThe index of the character.
Returns
The character at the specified index.

访问指定索引的字符(常量版本)。

Definition at line 84 of file libxr_string.hpp.

85 {
86 ASSERT(i < MaxLength);
87 return raw_string_[i];
88 }

◆ Raw()

template<unsigned int MaxLength>
const char * LibXR::String< MaxLength >::Raw ( ) const
inline

Gets the raw C-style string.

Returns
A pointer to the raw string.

获取原始 C 风格字符串。

Definition at line 62 of file libxr_string.hpp.

62{ return &raw_string_[0]; }

◆ Substr()

template<unsigned int MaxLength>
template<unsigned int SubStrLength>
String< SubStrLength > LibXR::String< MaxLength >::Substr ( size_t pos) const
inline

Extracts a substring starting at a given position.

Template Parameters
SubStrLengthThe length of the substring.
Parameters
posThe starting position.
Returns
The extracted substring.

提取从指定位置开始的子字符串。

Definition at line 240 of file libxr_string.hpp.

241 {
242 ASSERT(pos < MaxLength);
244 LibXR::min(SubStrLength, MaxLength - pos));
245 }
String()
Default constructor, initializes an empty string.

Field Documentation

◆ raw_string_

template<unsigned int MaxLength>
std::array<char, MaxLength + 1> LibXR::String< MaxLength >::raw_string_
private

The raw character array storing the string. 原始字符数组存储字符串。

Definition at line 248 of file libxr_string.hpp.


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