Skip to content

Commit

Permalink
Add a few functions to SFixedString (#2144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pirulax committed Apr 8, 2023
1 parent e9351a6 commit 0798bb9
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions Shared/sdk/SharedUtil.Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,23 +659,49 @@ namespace SharedUtil
//
// Fixed sized string buffer
//
template <int MAX_LENGTH>
template <size_t MAX_LENGTH>
class SFixedString
{
char szData[MAX_LENGTH + 1];

public:
SFixedString() { szData[0] = 0; }
constexpr SFixedString() { szData[0] = 0; }


// In
SFixedString& operator=(const char* szOther)
constexpr SFixedString& Assign(const char* szOther, size_t len)
{
STRNCPY(szData, szOther, len + 1);
return *this;
}

constexpr SFixedString& operator=(const char* szOther)
{
Assign(szOther, MAX_LENGTH + 1);
return *this;
}
#ifdef __cpp_lib_string_view
constexpr SFixedString& operator=(std::string_view other)
{
STRNCPY(szData, szOther, MAX_LENGTH + 1);
Assign(other.data(), other.length() + 1);
return *this;
}
#endif

#ifdef __cpp_lib_string_view
// Out
operator const char*() const { return szData; }
constexpr operator std::string_view() const { return { szData }; }
#endif
constexpr operator const char*() const { return szData; }
constexpr char* Data() { return &szData[0]; }

constexpr size_t GetMaxLength() const { return MAX_LENGTH; }
size_t GetLength() const { return strlen(szData); }

// Shake it all about
void Encrypt();
constexpr bool Empty() { return szData[0] == 0; }
constexpr void Clear() const { szData[0] = 0; }

// Returns a pointer to a null-terminated character array
const char* c_str() const noexcept { return &szData[0]; }
Expand Down

0 comments on commit 0798bb9

Please sign in to comment.