Skip to content

Commit

Permalink
[Util] Add a FastString helper class.
Browse files Browse the repository at this point in the history
  • Loading branch information
num0005 committed Jul 26, 2021
1 parent 03da3b8 commit ed55d46
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions H2Codez/util/FastString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#pragma once
#include <functional>
#include <variant>
#include <string>

class FastString
{
public:
FastString() {
contents = "";
}
/// <summary>
/// Construct the string with a const string, it is your responsibility to ensure this string remains valid
/// </summary>
/// <param name="string"></param>
FastString(const char* string) {
contents = string;
}

FastString(const std::string& string) {
contents = string;
}

inline const char* get() const {
return std::holds_alternative<std::string>(contents) ?
std::get<std::string>(contents).c_str() :
std::get<const char*>(contents);
}

bool operator<(const FastString& other) const {
if (contents.index() == other.contents.index() && contents.index() == 1) {
return std::get<std::string>(contents) < std::get<std::string>(other.contents);
}

return strcmp(get(), other.get()) < 0;
}

bool operator>(const FastString& other) const {
if (contents.index() == other.contents.index() && contents.index() == 1) {
return std::get<std::string>(contents) > std::get<std::string>(other.contents);
}

return strcmp(get(), other.get()) > 0;
}

bool operator==(const FastString& other) const {
if (contents.index() == other.contents.index() && contents.index() == 1) {
return std::get<std::string>(contents) == std::get<std::string>(other.contents);
}

return strcmp(get(), other.get()) == 0;
}

private:
std::variant<const char*, std::string> contents;
};

0 comments on commit ed55d46

Please sign in to comment.