String tweaks
This commit is contained in:
parent
a0e8e4265a
commit
c81a5d03c5
|
@ -16,7 +16,7 @@ EnvVarMap get_env_vars()
|
||||||
const char* value = name;
|
const char* value = name;
|
||||||
while (*value != 0 and *value != '=')
|
while (*value != 0 and *value != '=')
|
||||||
++value;
|
++value;
|
||||||
env_vars[String{name, value}] = (*value == '=') ? value+1 : value;
|
env_vars[{name, value}] = (*value == '=') ? value+1 : String{};
|
||||||
}
|
}
|
||||||
return env_vars;
|
return env_vars;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,17 +20,16 @@ class StringOps
|
||||||
public:
|
public:
|
||||||
using value_type = CharType;
|
using value_type = CharType;
|
||||||
|
|
||||||
|
[[gnu::always_inline]]
|
||||||
friend bool operator==(const Type& lhs, const Type& rhs)
|
friend bool operator==(const Type& lhs, const Type& rhs)
|
||||||
{
|
{
|
||||||
return lhs.length() == rhs.length() and
|
return lhs.length() == rhs.length() and
|
||||||
std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[gnu::always_inline]]
|
||||||
friend bool operator!=(const Type& lhs, const Type& rhs)
|
friend bool operator!=(const Type& lhs, const Type& rhs)
|
||||||
{
|
{ return not (lhs == rhs); }
|
||||||
return lhs.length() != rhs.length() or
|
|
||||||
not std::equal(lhs.begin(), lhs.end(), rhs.begin());
|
|
||||||
}
|
|
||||||
|
|
||||||
friend bool operator<(const Type& lhs, const Type& rhs)
|
friend bool operator<(const Type& lhs, const Type& rhs)
|
||||||
{
|
{
|
||||||
|
@ -75,8 +74,7 @@ public:
|
||||||
CharType& operator[](ByteCount pos) { return type().data()[(int)pos]; }
|
CharType& operator[](ByteCount pos) { return type().data()[(int)pos]; }
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
const CharType& operator[](ByteCount pos) const
|
const CharType& operator[](ByteCount pos) const { return type().data()[(int)pos]; }
|
||||||
{ return type().data()[(int)pos]; }
|
|
||||||
|
|
||||||
Codepoint operator[](CharCount pos) const
|
Codepoint operator[](CharCount pos) const
|
||||||
{ return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
{ return utf8::codepoint(utf8::advance(begin(), end(), pos), end()); }
|
||||||
|
@ -96,7 +94,9 @@ public:
|
||||||
StringView substr(CharCount from, CharCount length = INT_MAX) const;
|
StringView substr(CharCount from, CharCount length = INT_MAX) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
[[gnu::always_inline]]
|
||||||
Type& type() { return *static_cast<Type*>(this); }
|
Type& type() { return *static_cast<Type*>(this); }
|
||||||
|
[[gnu::always_inline]]
|
||||||
const Type& type() const { return *static_cast<const Type*>(this); }
|
const Type& type() const { return *static_cast<const Type*>(this); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -106,9 +106,8 @@ public:
|
||||||
using Content = std::basic_string<char, std::char_traits<char>,
|
using Content = std::basic_string<char, std::char_traits<char>,
|
||||||
Allocator<char, MemoryDomain::String>>;
|
Allocator<char, MemoryDomain::String>>;
|
||||||
|
|
||||||
String() {}
|
String() = default;
|
||||||
String(const char* content) : m_data(content) {}
|
String(const char* content) : m_data(content) {}
|
||||||
String(Content content) : m_data(std::move(content)) {}
|
|
||||||
explicit String(char content, CharCount count = 1) : m_data((size_t)(int)count, content) {}
|
explicit String(char content, CharCount count = 1) : m_data((size_t)(int)count, content) {}
|
||||||
explicit String(Codepoint cp, CharCount count = 1)
|
explicit String(Codepoint cp, CharCount count = 1)
|
||||||
{
|
{
|
||||||
|
@ -125,11 +124,12 @@ public:
|
||||||
const char* data() const { return m_data.data(); }
|
const char* data() const { return m_data.data(); }
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
ByteCount length() const { return ByteCount{(int)m_data.length()}; }
|
ByteCount length() const { return (int)m_data.length(); }
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
const char* c_str() const { return m_data.c_str(); }
|
const char* c_str() const { return m_data.c_str(); }
|
||||||
|
|
||||||
|
[[gnu::always_inline]]
|
||||||
void append(const char* data, ByteCount count) { m_data.append(data, (size_t)(int)count); }
|
void append(const char* data, ByteCount count) { m_data.append(data, (size_t)(int)count); }
|
||||||
|
|
||||||
void push_back(char c) { m_data.push_back(c); }
|
void push_back(char c) { m_data.push_back(c); }
|
||||||
|
@ -143,7 +143,7 @@ private:
|
||||||
class StringView : public StringOps<StringView, const char>
|
class StringView : public StringOps<StringView, const char>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
constexpr StringView() : m_data{nullptr}, m_length{0} {}
|
constexpr StringView() = default;
|
||||||
constexpr StringView(const char* data, ByteCount length)
|
constexpr StringView(const char* data, ByteCount length)
|
||||||
: m_data{data}, m_length{length} {}
|
: m_data{data}, m_length{length} {}
|
||||||
constexpr StringView(const char* data) : m_data{data}, m_length{strlen(data)} {}
|
constexpr StringView(const char* data) : m_data{data}, m_length{strlen(data)} {}
|
||||||
|
@ -152,12 +152,12 @@ public:
|
||||||
StringView(const char& c) : m_data(&c), m_length(1) {}
|
StringView(const char& c) : m_data(&c), m_length(1) {}
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
const char* data() const { return m_data; }
|
constexpr const char* data() const { return m_data; }
|
||||||
|
|
||||||
[[gnu::always_inline]]
|
[[gnu::always_inline]]
|
||||||
ByteCount length() const { return m_length; }
|
constexpr ByteCount length() const { return m_length; }
|
||||||
|
|
||||||
String str() const { return String{begin(), end()}; }
|
String str() const { return {begin(), end()}; }
|
||||||
|
|
||||||
struct ZeroTerminatedString
|
struct ZeroTerminatedString
|
||||||
{
|
{
|
||||||
|
@ -175,7 +175,7 @@ public:
|
||||||
const char* unowned = nullptr;
|
const char* unowned = nullptr;
|
||||||
|
|
||||||
};
|
};
|
||||||
ZeroTerminatedString zstr() const { return ZeroTerminatedString{begin(), end()}; }
|
ZeroTerminatedString zstr() const { return {begin(), end()}; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr ByteCount strlen(const char* s)
|
static constexpr ByteCount strlen(const char* s)
|
||||||
|
@ -183,8 +183,8 @@ private:
|
||||||
return *s == 0 ? 0 : strlen(s+1) + 1;
|
return *s == 0 ? 0 : strlen(s+1) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* m_data;
|
const char* m_data = nullptr;
|
||||||
ByteCount m_length;
|
ByteCount m_length = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename Type, typename CharType>
|
template<typename Type, typename CharType>
|
||||||
|
@ -214,8 +214,8 @@ inline String operator+(StringView lhs, StringView rhs)
|
||||||
{
|
{
|
||||||
String res;
|
String res;
|
||||||
res.reserve((int)(lhs.length() + rhs.length()));
|
res.reserve((int)(lhs.length() + rhs.length()));
|
||||||
res += lhs;
|
res.append(lhs.data(), lhs.length());
|
||||||
res += rhs;
|
res.append(rhs.data(), rhs.length());
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user