Skip to content
Snippets Groups Projects
Commit d6de34fe authored by marco's avatar marco
Browse files

add FixedString class

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@1838 5a81b35b-ba03-0410-adc8-b2c5c5119f08
parent cf47c876
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ message.hh
platform.hh
string_ref.hh
pod_vector.hh
fixed_string.hh
)
module(NAME base SOURCES ${OST_BASE_SOURCES}
......
#ifndef OST_SMALL_STRING_HH
#define OST_SMALL_STRING_HH
#include <ost/base.hh>
#include <cassert>
#include <string.h>
namespace ost {
/// \brief string class that uses an array of static size to hold the
/// characters
template <size_t LENGTH>
class TEMPLATE_EXPORT FixedString {
public:
FixedString() {
::memset(bytes_, 0, LENGTH+1);
}
explicit FixedString(const String& str) {
this->assign(str);
}
explicit FixedString(const char* str) {
this->assign(str);
}
size_t length() const {
return strlen(bytes_);
}
size_t size() const {
return this->length();
}
char operator[](size_t index) const {
return bytes_[index];
}
char& operator[](size_t index) {
return bytes_[index];
}
bool operator==(const String& rhs) const {
return !strcmp(bytes_, rhs.c_str());
}
bool operator!=(const String& rhs) const {
return !this->operator==(rhs);
}
bool operator==(const FixedString<LENGTH>& rhs) const {
return !strcmp(bytes_, rhs.bytes_);
}
bool operator!=(const FixedString<LENGTH>& rhs) const {
return !this->operator==(rhs);
}
FixedString<LENGTH>& operator=(const String& rhs) {
this->assign(rhs);
return *this;
}
template <typename DS>
void Serialize(DS& ds) {
RawSerialize(ds, bytes_, LENGTH);
}
const char* c_str() const {
return bytes_;
}
private:
void assign(const String& str) {
assert(str.length()<=LENGTH);
strcpy(bytes_, str.c_str());
}
void assign(const char* str) {
assert(strlen(str)<=LENGTH);
strcpy(bytes_, str);
}
char bytes_[LENGTH+1];
};
}
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment