64 lines
1.3 KiB
C
Raw Normal View History

2024-02-07 20:59:24 +08:00
#pragma once
2024-08-21 20:25:56 +08:00
inline size_t str_len(const char *s) { return strlen(s); }
inline size_t str_len(const wchar_t *s) { return wcslen(s); }
2024-02-07 20:59:24 +08:00
2024-08-21 20:25:56 +08:00
template <class CharT>
2024-02-07 20:59:24 +08:00
struct TextUnion
{
2024-08-21 20:25:56 +08:00
enum
{
ShortTextCapacity = 0x10 / sizeof(CharT)
};
2024-02-07 20:59:24 +08:00
2024-08-21 20:25:56 +08:00
union
{
2024-02-07 20:59:24 +08:00
const CharT *text; // 0x0
CharT chars[ShortTextCapacity];
};
2024-08-21 20:25:56 +08:00
size_t size, // 0x10
2024-02-07 20:59:24 +08:00
capacity;
bool isValid() const
{
if (size <= 0 || size > capacity)
return false;
const CharT *t = getText();
return Engine::isAddressWritable(t, size) && str_len(t) == size;
}
const CharT *getText() const
2024-08-21 20:25:56 +08:00
{
return capacity < ShortTextCapacity ? chars : text;
}
2024-02-07 20:59:24 +08:00
2024-08-21 20:25:56 +08:00
void setText(const CharT *_text, size_t _size)
2024-02-07 20:59:24 +08:00
{
if (_size < ShortTextCapacity)
::memcpy(chars, _text, (_size + 1) * sizeof(CharT));
else
text = _text;
capacity = size = _size;
}
2024-08-21 20:25:56 +08:00
void setLongText(const CharT *_text, size_t _size)
2024-02-07 20:59:24 +08:00
{
text = _text;
size = _size;
capacity = max(ShortTextCapacity, _size);
}
void setText(const std::basic_string<CharT> &text)
2024-08-21 20:25:56 +08:00
{
setText((const CharT *)text.c_str(), text.size());
}
2024-02-07 20:59:24 +08:00
void setLongText(const std::basic_string<CharT> &text)
2024-08-21 20:25:56 +08:00
{
setLongText((const CharT *)text.c_str(), text.size());
}
2024-02-07 20:59:24 +08:00
};
2024-08-21 20:25:56 +08:00
using TextUnionA = TextUnion<char>;
using TextUnionW = TextUnion<wchar_t>;
2024-02-07 20:59:24 +08:00
// EOF