Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // String[n] n+1, первый байт резервируется для хранения длины строки, поэтому
- // максимальная длина строки не больше 255 символов
- template<size_t S> struct String
- {
- enum {CAPACITY = S + 1}; // +1 - length byte
- char value[CAPACITY];
- static String<S> make(const char* s) {
- String<S> tmp = {0};
- tmp = s;
- return tmp;
- }
- String& operator=(const char* s) {
- size_t l = strlen(s);
- size_t c = min(l, S);
- std::copy(s, s + c, value+1);
- value[0] = c;
- return *this;
- }
- // WARING! data() is not null terminated string
- // to iterate use begin() and end() iterator
- const char* data() const {
- return value+1;
- }
- size_t length() const {
- return static_cast<unsigned char>(value[0]);
- }
- const char* begin() const {
- return data();
- }
- const char* end() const {
- return begin() + length();
- }
- bool operator == (const String<S> &other) const {
- if( length() != other.length() )
- return false;
- return strncmp(data(), other.data(), length()) == 0;
- }
- std::string str() const {
- return std::string(begin(), end());
- }
- }; // template<size_t S> struct String..
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement