Код: Выделить всё
template
class FixedString
{
public:
using size_type = decltype(MaxSizeT);
constexpr static size_type maxSize = MaxSizeT;
FixedString() : length(0)
{
data[0] = '\0'; // Ensure null termination
}
FixedString(const char* inData) { assignStr(inData); }
FixedString(const FixedString& other) { assignStr(other); }
char data[maxSize + 1]; // Add space for null terminator
size_type length; // Current length of the string
FixedString operator+(const FixedString& other) const
{
FixedString result{*this};
result += other;
return result;
}
FixedString& operator+=(const FixedString& other)
{
if (length >= maxSize)
{
return *this;
}
size_type const space = maxSize - length;
auto copyLen = mcu::nmin(space, other.length);
memcpy(&data[length], other.data, copyLen);
length += copyLen;
data[length] = 0;
return *this;
}
FixedString& operator=(const FixedString& other)
{
assignStr(other);
return *this;
}
template
FixedString& operator=(const FixedString& other)
{
assignStr(other.data, other.length);
return *this;
}
FixedString& operator=(const char* other)
{
assignStr(other);
return *this;
}
bool operator==(const FixedString& other) const
{
return length == other.length && memcmp(data, other.data, length) == 0;
}
bool operator==(const char* other) const
{
auto const otherLen = strlen(other);
if (otherLen != length)
{
return false;
}
else
{
return memcmp(&data[0], other, length) == 0;
}
}
void assignStr(const char* inData)
{
auto len = strlen(inData);
assignStr(inData, len);
}
void assignStr(const char* inData, size_t len)
{
if (len > maxSize)
{
len = maxSize;
}
memcpy(data, inData, len);
data[len] = 0;
length = len;
}
void assignStr(const FixedString& other)
{
memcpy(data, other.data, maxSize);
length = other.length;
data[length] = 0;
}
char& operator[](size_t index) { return index < length ? data[index] : data[0]; }
const char& operator[](size_t index) const
{
// Bounds check: return a dummy value if out of range
static char dummy = '\0';
return index < length ? data[index] : dummy;
}
char* begin() { return data; }
char* end() { return data + length; }
const char* begin() const { return data; }
const char* end() const { return data + length; }
const char* cbegin() const { return data; }
const char* cend() const { return data + length; }
const char* c_str() const
{
return data; // Already null-terminated
}
size_t size() const { return length; }
size_t max_size() const { return maxSize; }
};
Код: Выделить всё
[[nodiscard]] FixedString substr(size_type start, size_type end) const
{
if(end > length) {
end = length;
}
if(start >= end || start >= length)
{
return {};
}
else {
const size_type newLen = end - start;
FixedString copy;
copy.assignStr(&data[start], newLen);
return copy;
}
}
< /code>
Однако, если я сделаю это: < /p>
FixedString myStr = "abcdef";
myStr = myStr.substr(1,4);
Код: Выделить всё
std::string_view[*] При использовании в качестве auto x = someststr.substr (x, y) он должен заплатить в фиксированное строение копию исходного
, если поставлен в конструктор -конструктор, и то же, и то же самое, и то же самое, и то же самое, и искачник, и то же, и то же самое, и то же самое, и то же самое, и иска Вместо этого < /li>
< /ul>
Я не уверен, возможно ли это. Я, конечно, могу просто иметь отдельную функцию Crop non Const, которая выполняет substr на месте, но мне было в основном любопытно, есть ли способ скрыть это поведение в методе substr .
Подробнее здесь: https://stackoverflow.com/questions/795 ... assigns-to
Мобильная версия