18 #ifndef FOEDUS_ASSORTED_FIXED_STRING_HPP_
19 #define FOEDUS_ASSORTED_FIXED_STRING_HPP_
64 template <u
int MAXLEN,
typename CHAR =
char>
71 template <u
int MAXLEN2>
86 template <u
int MAXLEN2>
92 template <u
int MAXLEN2>
95 return other.length() == 0;
97 return length_ == other.length() &&
98 std::memcmp(data_, other.data(), length_ *
sizeof(CHAR)) == 0;
100 template <u
int MAXLEN2>
104 template <u
int MAXLEN2>
106 uint32_t min_len = std::min<uint32_t>(length_, other.length());
108 return length_ < other.length();
110 int result = std::memcmp(data_, other.data(), min_len *
sizeof(CHAR));
114 return length_ < other.length();
118 template <u
int MAXLEN2>
121 length_ = other.length() > MAXLEN ? MAXLEN : other.length();
122 std::memcpy(data_, other.data(), length_ *
sizeof(CHAR));
126 length_ =
str.size() > MAXLEN ? MAXLEN :
str.size();
127 std::memcpy(data_,
str.data(), length_ *
sizeof(CHAR));
132 length_ = len > MAXLEN ? MAXLEN : len;
133 std::memcpy(data_,
str, length_ *
sizeof(CHAR));
137 template <u
int MAXLEN2>
140 uint32_t len = length_ + other.length() > MAXLEN ? MAXLEN - length_ : other.length();
141 std::memcpy(data_ + length_, other.data(), len *
sizeof(CHAR));
147 uint32_t len = length_ +
str.size() > MAXLEN ? MAXLEN - length_ :
str.size();
148 std::memcpy(data_ + length_,
str.data(), len *
sizeof(CHAR));
154 len = length_ + len > MAXLEN ? MAXLEN - length_ : len;
155 std::memcpy(data_ + length_,
str, len *
sizeof(CHAR));
175 if (length_ < MAXLEN) {
177 std::memset(const_cast<char*>(data_ + length_), 0, MAXLEN - length_);
185 std::basic_string<CHAR>
str()
const {
186 return std::basic_string<CHAR>(data_, length_);
189 const CHAR*
c_str()
const {
return str().c_str(); }
198 static const uint32_t
npos = -1;
214 #endif // FOEDUS_ASSORTED_FIXED_STRING_HPP_
void assign(const std::basic_string< CHAR > &str) noexcept
Assign operator for std::string.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
uint32_t max_size() const noexcept
Return maximum size of string.
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
FixedString(const CHAR *str, uint32_t len) noexcept
Copy constructor for char* and len.
const CHAR * data() const noexcept
Get string data.
bool operator!=(const FixedString< MAXLEN2, CHAR > &other) const noexcept
uint32_t capacity() const noexcept
Return size of allocated storage.
void append(const std::basic_string< CHAR > &str) noexcept
Append operator for std::string.
bool operator==(const FixedString< MAXLEN2, CHAR > &other) const noexcept
void append(const CHAR *str, uint32_t len) noexcept
Append operator for char* and length.
static uint32_t strlen(const char *str) noexcept
void append(const FixedString< MAXLEN2, CHAR > &other) noexcept
Append operator for all FixedString objects.
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
void assign(const CHAR *str, uint32_t len) noexcept
Assign operator for char* and length.
FixedString(const FixedString< MAXLEN2, CHAR > &other) noexcept
Copy constructor for all FixedString objects.
bool empty() const noexcept
Test if string is empty.
std::basic_string< CHAR > str() const
Convert to a std::string object.
static uint32_t strlen(const wchar_t *str) noexcept
FixedString(const CHAR *str) noexcept
Copy constructor for null-terminated char*.
void clear() noexcept
Clear string.
static const uint32_t npos
npos is a static member constant value with the greatest possible value for uint32_t.
An embedded string object of fixed max-length, which uses no external memory.
uint32_t length() const noexcept
Returns the length of this string.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
const CHAR * c_str() const
Convert to a C string.
uint32_t size() const noexcept
Returns the length of this string.
FixedString & operator=(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
void zero_fill_remaining() const noexcept
Sets zeros to unused data_ region.
friend std::ostream & operator<<(std::ostream &o, const FixedString &v)
FixedString() noexcept
Constructs an empty string.