libfoedus-core
FOEDUS Core Library
fixed_string.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2015, Hewlett-Packard Development Company, LP.
3  * This program is free software; you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by the Free
5  * Software Foundation; either version 2 of the License, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11  * more details. You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the Free Software
13  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14  *
15  * HP designates this particular file as subject to the "Classpath" exception
16  * as provided by HP in the LICENSE.txt file that accompanied this code.
17  */
18 #ifndef FOEDUS_ASSORTED_FIXED_STRING_HPP_
19 #define FOEDUS_ASSORTED_FIXED_STRING_HPP_
20 #include <stdint.h>
21 
22 #include <algorithm>
23 #include <cstring>
24 #include <cwchar>
25 #include <ostream>
26 #include <string>
27 
28 #include "foedus/assert_nd.hpp"
29 #include "foedus/cxx11.hpp"
30 
31 namespace foedus {
32 namespace assorted {
33 
64 template <uint MAXLEN, typename CHAR = char>
65 class FixedString {
66  public:
68  FixedString() CXX11_NOEXCEPT : length_(0) {}
69 
71  template <uint MAXLEN2>
72  explicit FixedString(const FixedString<MAXLEN2, CHAR>& other) CXX11_NOEXCEPT { assign(other); }
73 
75  FixedString(const CHAR* str, uint32_t len) CXX11_NOEXCEPT { assign(str, len); }
76 
78  FixedString(const CHAR* str) CXX11_NOEXCEPT { // NOLINT(runtime/explicit) follows std::string
79  assign(str, strlen(str));
80  }
81 
82  static uint32_t strlen(const char* str) CXX11_NOEXCEPT { return std::strlen(str); }
83  static uint32_t strlen(const wchar_t* str) CXX11_NOEXCEPT { return std::wcslen(str); }
84 
86  template <uint MAXLEN2>
88  assign(other);
89  return *this;
90  }
91 
92  template <uint MAXLEN2>
94  if (length_ == 0) {
95  return other.length() == 0;
96  }
97  return length_ == other.length() &&
98  std::memcmp(data_, other.data(), length_ * sizeof(CHAR)) == 0;
99  }
100  template <uint MAXLEN2>
102  return !operator==(other);
103  }
104  template <uint MAXLEN2>
105  bool operator<(const FixedString<MAXLEN2, CHAR>& other) const CXX11_NOEXCEPT {
106  uint32_t min_len = std::min<uint32_t>(length_, other.length());
107  if (min_len == 0) {
108  return length_ < other.length();
109  }
110  int result = std::memcmp(data_, other.data(), min_len * sizeof(CHAR));
111  if (result != 0) {
112  return result < 0;
113  }
114  return length_ < other.length();
115  }
116 
118  template <uint MAXLEN2>
120  ASSERT_ND(other.length() <= MAXLEN2);
121  length_ = other.length() > MAXLEN ? MAXLEN : other.length();
122  std::memcpy(data_, other.data(), length_ * sizeof(CHAR));
123  }
125  void assign(const std::basic_string<CHAR>& str) CXX11_NOEXCEPT {
126  length_ = str.size() > MAXLEN ? MAXLEN : str.size();
127  std::memcpy(data_, str.data(), length_ * sizeof(CHAR));
128  }
129 
131  void assign(const CHAR* str, uint32_t len) CXX11_NOEXCEPT {
132  length_ = len > MAXLEN ? MAXLEN : len;
133  std::memcpy(data_, str, length_ * sizeof(CHAR));
134  }
135 
137  template <uint MAXLEN2>
139  ASSERT_ND(other.length() <= MAXLEN2);
140  uint32_t len = length_ + other.length() > MAXLEN ? MAXLEN - length_ : other.length();
141  std::memcpy(data_ + length_, other.data(), len * sizeof(CHAR));
142  length_ += len;
143  }
144 
146  void append(const std::basic_string<CHAR>& str) CXX11_NOEXCEPT {
147  uint32_t len = length_ + str.size() > MAXLEN ? MAXLEN - length_ : str.size();
148  std::memcpy(data_ + length_, str.data(), len * sizeof(CHAR));
149  length_ += len;
150  }
151 
153  void append(const CHAR* str, uint32_t len) CXX11_NOEXCEPT {
154  len = length_ + len > MAXLEN ? MAXLEN - length_ : len;
155  std::memcpy(data_ + length_, str, len * sizeof(CHAR));
156  length_ += len;
157  }
158 
159  // the following methods imitate std::string signatures.
160 
162  uint32_t length() const CXX11_NOEXCEPT { return length_; }
164  uint32_t size() const CXX11_NOEXCEPT { return length_; }
166  uint32_t capacity() const CXX11_NOEXCEPT { return MAXLEN; }
168  uint32_t max_size() const CXX11_NOEXCEPT { return MAXLEN; }
170  void clear() CXX11_NOEXCEPT { length_ = 0; }
172  bool empty() const CXX11_NOEXCEPT { return length_ == 0; }
175  if (length_ < MAXLEN) {
176  // this is not logically changing the content, so const is the right semantics.
177  std::memset(const_cast<char*>(data_ + length_), 0, MAXLEN - length_);
178  }
179  }
180 
181 
183  const CHAR* data() const CXX11_NOEXCEPT { return data_; }
185  std::basic_string<CHAR> str() const {
186  return std::basic_string<CHAR>(data_, length_);
187  }
189  const CHAR* c_str() const { return str().c_str(); }
190 
198  static const uint32_t npos = -1;
199 
200  friend std::ostream& operator<<(std::ostream& o, const FixedString& v) {
201  o << v.str();
202  return o;
203  }
204 
205  private:
207  uint32_t length_; // +4
209  CHAR data_[MAXLEN]; // +MAXLEN
210 };
211 
212 } // namespace assorted
213 } // namespace foedus
214 #endif // FOEDUS_ASSORTED_FIXED_STRING_HPP_
void assign(const std::basic_string< CHAR > &str) noexcept
Assign operator for std::string.
Root package of FOEDUS (Fast Optimistic Engine for Data Unification Services).
Definition: assert_nd.hpp:44
uint32_t max_size() const noexcept
Return maximum size of string.
#define CXX11_NOEXCEPT
Used in public headers in place of "noexcept" of C++11.
Definition: cxx11.hpp:133
FixedString(const CHAR *str, uint32_t len) noexcept
Copy constructor for char* and len.
const CHAR * data() const noexcept
Get string data.
bool operator!=(const FixedString< MAXLEN2, CHAR > &other) const noexcept
uint32_t capacity() const noexcept
Return size of allocated storage.
void append(const std::basic_string< CHAR > &str) noexcept
Append operator for std::string.
bool operator==(const FixedString< MAXLEN2, CHAR > &other) const noexcept
void append(const CHAR *str, uint32_t len) noexcept
Append operator for char* and length.
static uint32_t strlen(const char *str) noexcept
void append(const FixedString< MAXLEN2, CHAR > &other) noexcept
Append operator for all FixedString objects.
void assign(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
void assign(const CHAR *str, uint32_t len) noexcept
Assign operator for char* and length.
FixedString(const FixedString< MAXLEN2, CHAR > &other) noexcept
Copy constructor for all FixedString objects.
bool empty() const noexcept
Test if string is empty.
std::basic_string< CHAR > str() const
Convert to a std::string object.
static uint32_t strlen(const wchar_t *str) noexcept
FixedString(const CHAR *str) noexcept
Copy constructor for null-terminated char*.
void clear() noexcept
Clear string.
static const uint32_t npos
npos is a static member constant value with the greatest possible value for uint32_t.
An embedded string object of fixed max-length, which uses no external memory.
uint32_t length() const noexcept
Returns the length of this string.
#define ASSERT_ND(x)
A warning-free wrapper macro of assert() that has no performance effect in release mode even when 'x'...
Definition: assert_nd.hpp:72
const CHAR * c_str() const
Convert to a C string.
uint32_t size() const noexcept
Returns the length of this string.
FixedString & operator=(const FixedString< MAXLEN2, CHAR > &other) noexcept
Assign operator for all FixedString objects.
void zero_fill_remaining() const noexcept
Sets zeros to unused data_ region.
friend std::ostream & operator<<(std::ostream &o, const FixedString &v)
FixedString() noexcept
Constructs an empty string.