mirror of
https://github.com/bkaradzic/bx.git
synced 2026-06-08 03:03:48 +00:00
Added bx::StringLiteral.
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -734,12 +734,12 @@ namespace bx
|
||||
int32_t width;
|
||||
int32_t base;
|
||||
int32_t prec;
|
||||
char fill;
|
||||
char fill;
|
||||
uint8_t bits;
|
||||
bool left;
|
||||
bool upper;
|
||||
bool spec;
|
||||
bool sign;
|
||||
bool left;
|
||||
bool upper;
|
||||
bool spec;
|
||||
bool sign;
|
||||
};
|
||||
|
||||
static int32_t write(WriterI* _writer, const char* _str, int32_t _len, const Param& _param, Error* _err)
|
||||
@@ -956,7 +956,7 @@ namespace bx
|
||||
}
|
||||
else if ('%' == ch)
|
||||
{
|
||||
// %[Flags][Width][.Precision][Leegth]Type
|
||||
// %[Flags][Width][.Precision][Length]Type
|
||||
read(&reader, ch, &err);
|
||||
|
||||
Param param;
|
||||
@@ -1074,8 +1074,8 @@ namespace bx
|
||||
{
|
||||
case 'h': param.bits = sizeof(signed char )*8; break;
|
||||
case 'l': param.bits = sizeof(long long int)*8; break;
|
||||
case '3':
|
||||
case '6':
|
||||
|
||||
case '3': case '6':
|
||||
read(&reader, ch, &err);
|
||||
switch (ch)
|
||||
{
|
||||
@@ -1134,12 +1134,9 @@ namespace bx
|
||||
};
|
||||
break;
|
||||
|
||||
case 'e':
|
||||
case 'E':
|
||||
case 'f':
|
||||
case 'F':
|
||||
case 'g':
|
||||
case 'G':
|
||||
case 'e': case 'E':
|
||||
case 'f': case 'F':
|
||||
case 'g': case 'G':
|
||||
param.upper = isUpper(ch);
|
||||
size += write(_writer, va_arg(_argList, double), param, _err);
|
||||
break;
|
||||
@@ -1148,8 +1145,7 @@ namespace bx
|
||||
size += write(_writer, va_arg(_argList, void*), param, _err);
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
case 'X':
|
||||
case 'x': case 'X':
|
||||
param.base = 16;
|
||||
param.upper = isUpper(ch);
|
||||
switch (param.bits)
|
||||
|
||||
@@ -12,6 +12,35 @@
|
||||
|
||||
bx::AllocatorI* g_allocator;
|
||||
|
||||
TEST_CASE("StringLiteral", "")
|
||||
{
|
||||
constexpr bx::StringLiteral tmp[] = { "1389", "abvgd", "mac", "pod" };
|
||||
|
||||
REQUIRE(bx::isSorted(tmp, BX_COUNTOF(tmp) ) );
|
||||
|
||||
static_assert(4 == tmp[0].getLength() );
|
||||
REQUIRE(0 == bx::strCmp("1389", tmp[0]) );
|
||||
|
||||
static_assert(5 == tmp[1].getLength() );
|
||||
REQUIRE(0 == bx::strCmp("abvgd", tmp[1]) );
|
||||
|
||||
static_assert(3 == tmp[2].getLength() );
|
||||
REQUIRE(0 == bx::strCmp("mac", tmp[2]) );
|
||||
|
||||
static_assert(3 == tmp[3].getLength() );
|
||||
REQUIRE(0 == bx::strCmp("pod", tmp[3]) );
|
||||
|
||||
constexpr bx::StringLiteral copy = tmp[0];
|
||||
|
||||
static_assert(4 == copy.getLength() );
|
||||
REQUIRE(0 == bx::strCmp("1389", copy) );
|
||||
|
||||
constexpr bx::StringView sv = tmp[1];
|
||||
|
||||
REQUIRE(5 == sv.getLength() );
|
||||
REQUIRE(0 == bx::strCmp("abvgd", sv) );
|
||||
}
|
||||
|
||||
TEST_CASE("stringPrintfTy", "")
|
||||
{
|
||||
std::string test;
|
||||
|
||||
Reference in New Issue
Block a user