OpenStructure
Loading...
Searching...
No Matches
fixed_string.hh
Go to the documentation of this file.
1#ifndef OST_SMALL_STRING_HH
2#define OST_SMALL_STRING_HH
3
4#include <ost/base.hh>
5#include <cassert>
6#include <string.h>
7
8namespace ost {
9
12template <size_t LENGTH>
13class TEMPLATE_EXPORT FixedString {
14public:
16 ::memset(bytes_, 0, LENGTH+1);
17 }
18
19 explicit FixedString(const String& str) {
20 this->assign(str);
21 }
22
23 explicit FixedString(const char* str) {
24 this->assign(str);
25 }
26
27
28 size_t length() const {
29 return strlen(bytes_);
30 }
31
32 size_t size() const {
33 return this->length();
34 }
35
36 char operator[](size_t index) const {
37 return bytes_[index];
38 }
39
40 char& operator[](size_t index) {
41 return bytes_[index];
42 }
43
44 bool operator==(const String& rhs) const {
45 return !strcmp(bytes_, rhs.c_str());
46 }
47
48 bool operator!=(const String& rhs) const {
49 return !this->operator==(rhs);
50 }
51 bool operator==(const FixedString<LENGTH>& rhs) const {
52 return !strcmp(bytes_, rhs.bytes_);
53 }
54
55 bool operator!=(const FixedString<LENGTH>& rhs) const {
56 return !this->operator==(rhs);
57 }
58 size_t capacity() const {
59 return LENGTH;
60 }
62 this->assign(rhs);
63 return *this;
64 }
65
66 template <typename DS>
67 void Serialize(DS& ds) {
68 RawSerialize(ds, bytes_, LENGTH);
69 }
70 const char* c_str() const {
71 return bytes_;
72 }
73 char* data() { return bytes_; }
74 const char* data() const { return bytes_; }
75private:
76 void assign(const String& str) {
77 assert(str.length()<=LENGTH);
78 strcpy(bytes_, str.c_str());
79 }
80
81 void assign(const char* str) {
82 assert(strlen(str)<=LENGTH);
83 strcpy(bytes_, str);
84 }
85 char bytes_[LENGTH+1];
86};
87
88}
89
90#endif
string class that uses an array of static size to hold the characters
const char * c_str() const
size_t size() const
FixedString(const char *str)
char operator[](size_t index) const
bool operator==(const String &rhs) const
size_t length() const
size_t capacity() const
bool operator!=(const FixedString< LENGTH > &rhs) const
void Serialize(DS &ds)
const char * data() const
bool operator==(const FixedString< LENGTH > &rhs) const
bool operator!=(const String &rhs) const
FixedString< LENGTH > & operator=(const String &rhs)
char & operator[](size_t index)
FixedString(const String &str)
std::string String
Definition base.hh:54
Definition base.dox:1