Rework fixed strings handling

pull/593/head
Andrei Kortunov 4 years ago
parent 4faaa86449
commit 822764d0fa

@ -97,7 +97,6 @@ TEST(EsmFixedString, struct_size)
ASSERT_EQ(4, sizeof(ESM::NAME)); ASSERT_EQ(4, sizeof(ESM::NAME));
ASSERT_EQ(32, sizeof(ESM::NAME32)); ASSERT_EQ(32, sizeof(ESM::NAME32));
ASSERT_EQ(64, sizeof(ESM::NAME64)); ASSERT_EQ(64, sizeof(ESM::NAME64));
ASSERT_EQ(256, sizeof(ESM::NAME256));
} }
TEST(EsmFixedString, is_pod) TEST(EsmFixedString, is_pod)
@ -105,5 +104,4 @@ TEST(EsmFixedString, is_pod)
ASSERT_TRUE(std::is_pod<ESM::NAME>::value); ASSERT_TRUE(std::is_pod<ESM::NAME>::value);
ASSERT_TRUE(std::is_pod<ESM::NAME32>::value); ASSERT_TRUE(std::is_pod<ESM::NAME32>::value);
ASSERT_TRUE(std::is_pod<ESM::NAME64>::value); ASSERT_TRUE(std::is_pod<ESM::NAME64>::value);
ASSERT_TRUE(std::is_pod<ESM::NAME256>::value);
} }

@ -6,7 +6,6 @@
#include <vector> #include <vector>
#include <stdint.h> #include <stdint.h>
#include <string.h>
namespace ESM namespace ESM
{ {
@ -57,11 +56,16 @@ public:
} }
bool operator!=(const std::string& str) const { return !( (*this) == str ); } bool operator!=(const std::string& str) const { return !( (*this) == str ); }
size_t data_size() const { return size; } static size_t data_size() { return size; }
size_t length() const { return strnlen(self()->ro_data(), size); } size_t length() const { return strnlen(self()->ro_data(), size); }
std::string toString() const { return std::string(self()->ro_data(), this->length()); } std::string toString() const { return std::string(self()->ro_data(), this->length()); }
void assign(const std::string& value) { std::strncpy(self()->rw_data(), value.c_str(), size); } void assign(const std::string& value)
{
std::strncpy(self()->rw_data(), value.c_str(), size-1);
self()->rw_data()[size-1] = '\0';
}
void clear() { this->assign(""); } void clear() { this->assign(""); }
private: private:
DERIVED<size> const* self() const DERIVED<size> const* self() const
@ -103,6 +107,20 @@ struct FIXED_STRING<4> : public FIXED_STRING_BASE<FIXED_STRING, 4>
bool operator==(uint32_t v) const { return v == intval; } bool operator==(uint32_t v) const { return v == intval; }
bool operator!=(uint32_t v) const { return v != intval; } bool operator!=(uint32_t v) const { return v != intval; }
void assign(const std::string& value)
{
intval = 0;
size_t length = value.size();
if (length == 0) return;
data[0] = value[0];
if (length == 1) return;
data[1] = value[1];
if (length == 2) return;
data[2] = value[2];
if (length == 3) return;
data[3] = value[3];
}
char const* ro_data() const { return data; } char const* ro_data() const { return data; }
char* rw_data() { return data; } char* rw_data() { return data; }
}; };
@ -110,7 +128,6 @@ struct FIXED_STRING<4> : public FIXED_STRING_BASE<FIXED_STRING, 4>
typedef FIXED_STRING<4> NAME; typedef FIXED_STRING<4> NAME;
typedef FIXED_STRING<32> NAME32; typedef FIXED_STRING<32> NAME32;
typedef FIXED_STRING<64> NAME64; typedef FIXED_STRING<64> NAME64;
typedef FIXED_STRING<256> NAME256;
/* This struct defines a file 'context' which can be saved and later /* This struct defines a file 'context' which can be saved and later
restored by an ESMReader instance. It will save the position within restored by an ESMReader instance. It will save the position within

Loading…
Cancel
Save