00001 #ifndef OST_SMALL_STRING_HH
00002 #define OST_SMALL_STRING_HH
00003
00004 #include <ost/base.hh>
00005 #include <cassert>
00006 #include <string.h>
00007
00008 namespace ost {
00009
00012 template <size_t LENGTH>
00013 class TEMPLATE_EXPORT FixedString {
00014 public:
00015 FixedString() {
00016 ::memset(bytes_, 0, LENGTH+1);
00017 }
00018
00019 explicit FixedString(const String& str) {
00020 this->assign(str);
00021 }
00022
00023 explicit FixedString(const char* str) {
00024 this->assign(str);
00025 }
00026
00027
00028 size_t length() const {
00029 return strlen(bytes_);
00030 }
00031
00032 size_t size() const {
00033 return this->length();
00034 }
00035
00036 char operator[](size_t index) const {
00037 return bytes_[index];
00038 }
00039
00040 char& operator[](size_t index) {
00041 return bytes_[index];
00042 }
00043
00044 bool operator==(const String& rhs) const {
00045 return !strcmp(bytes_, rhs.c_str());
00046 }
00047
00048 bool operator!=(const String& rhs) const {
00049 return !this->operator==(rhs);
00050 }
00051 bool operator==(const FixedString<LENGTH>& rhs) const {
00052 return !strcmp(bytes_, rhs.bytes_);
00053 }
00054
00055 bool operator!=(const FixedString<LENGTH>& rhs) const {
00056 return !this->operator==(rhs);
00057 }
00058 size_t capacity() const {
00059 return LENGTH;
00060 }
00061 FixedString<LENGTH>& operator=(const String& rhs) {
00062 this->assign(rhs);
00063 return *this;
00064 }
00065
00066 template <typename DS>
00067 void Serialize(DS& ds) {
00068 RawSerialize(ds, bytes_, LENGTH);
00069 }
00070 const char* c_str() const {
00071 return bytes_;
00072 }
00073 char* data() { return bytes_; }
00074 const char* data() const { return bytes_; }
00075 private:
00076 void assign(const String& str) {
00077 assert(str.length()<=LENGTH);
00078 strcpy(bytes_, str.c_str());
00079 }
00080
00081 void assign(const char* str) {
00082 assert(strlen(str)<=LENGTH);
00083 strcpy(bytes_, str);
00084 }
00085 char bytes_[LENGTH+1];
00086 };
00087
00088 }
00089
00090 #endif