Added bx::StringLiteral.

This commit is contained in:
Бранимир Караџић
2023-05-04 20:09:54 -07:00
parent c5593ad749
commit d403162701
5 changed files with 113 additions and 16 deletions

View File

@@ -29,6 +29,12 @@ namespace bx
return strCmp(*(const char**)_lhs, *(const char**)_rhs);
}
template<>
inline int32_t compareAscending<StringLiteral>(const void* _lhs, const void* _rhs)
{
return strCmp(*(const StringLiteral*)_lhs, *(const StringLiteral*)_rhs);
}
template<>
inline int32_t compareAscending<StringView>(const void* _lhs, const void* _rhs)
{

View File

@@ -37,11 +37,42 @@ namespace bx
va_end(argList);
}
inline constexpr StringLiteral::StringLiteral()
: m_ptr("")
, m_len(0)
{
}
template<int32_t SizeT>
inline constexpr StringLiteral::StringLiteral(const char (&str)[SizeT])
: m_ptr(str)
, m_len(SizeT - 1)
{
BX_ASSERT('\0' == m_ptr[SizeT - 1], "Must be 0 terminated.");
}
inline constexpr int32_t StringLiteral::getLength() const
{
return m_len;
}
inline constexpr const char* StringLiteral::getCPtr() const
{
return m_ptr;
}
inline StringView::StringView()
{
clear();
}
inline constexpr StringView::StringView(const StringLiteral& _str)
: m_ptr(_str.getCPtr() )
, m_len(_str.getLength() )
, m_0terminated(true)
{
}
inline StringView::StringView(const StringView& _rhs, int32_t _start, int32_t _len)
{
set(_rhs, _start, _len);

View File

@@ -20,14 +20,46 @@ namespace bx
};
};
/// Zero-terminated string literal.
///
class StringLiteral
{
public:
/// Construct default/empty string literal.
///
constexpr StringLiteral();
/// Construct string literal from C-style string literal.
///
template<int32_t SizeT>
constexpr StringLiteral(const char (&str)[SizeT]);
/// Returns string length.
///
constexpr int32_t getLength() const;
/// Returns zero-terminated C string pointer to string literal.
///
constexpr const char* getCPtr() const;
private:
const char* m_ptr;
int32_t m_len;
};
/// Non-zero-terminated string view.
///
class StringView
{
public:
/// Construct default/empty string view.
///
StringView();
/// Construct string view from string literal.
///
constexpr StringView(const StringLiteral& _str);
///
StringView(const StringView& _rhs, int32_t _start = 0, int32_t _len = INT32_MAX);
@@ -75,12 +107,15 @@ namespace bx
const char* getTerm() const;
/// Returns `true` if string is empty.
///
bool isEmpty() const;
/// Returns string length.
///
int32_t getLength() const;
/// Returns `true` if string is zero terminated.
///
bool is0Terminated() const;
protected: