9 static const char rcsid[] __attribute__((used)) =
"$Id: String.cpp 1702 2016-08-13 23:26:06Z sella $";
12 #include "../util/CommonMacro.h"
14 using namespace sella::variant;
16 String::String() throw () :
Nullable(std::make_shared<std::
string>()) { }
18 String::String(
const std::string &value)
throw () :
Nullable(std::make_shared<std::string>(value)) { }
20 String::String(std::string &&value) throw () :
Nullable(std::make_shared<std::
string>(std::move(value))) { }
23 if (LIKELY(
this != &other)) {
24 if (LIKELY(!other.isNull())) {
25 if (UNLIKELY(isNull())) {
26 this->value = std::make_shared<std::string>(other.get());
28 *std::static_pointer_cast<std::string>(this->value) = *std::static_pointer_cast<std::string>(other.value);
37 if (LIKELY(
this != &other)) {
38 if (LIKELY(!other.isNull())) {
39 this->value = std::move(other.value);
46 String::~String() throw () { }
48 bool String::operator==(
const String &other)
const throw () {
49 return (*std::static_pointer_cast<std::string>(this->value) == *std::static_pointer_cast<std::string>(other.value));
52 bool String::operator!=(
const String &other)
const throw () {
53 return !(*
this == other);
56 bool String::operator>(
const String &other)
const throw () {
57 return (*std::static_pointer_cast<std::string>(this->value) > *std::static_pointer_cast<std::string>(other.value));
60 bool String::operator<(
const String &other)
const throw () {
61 return (*std::static_pointer_cast<std::string>(this->value) < *std::static_pointer_cast<std::string>(other.value));
64 String& String::operator=(
const String &other)
throw () {
65 if (LIKELY(
this != &other)) {
66 if (LIKELY(!other.isNull())) {
67 if (UNLIKELY(isNull())) {
68 this->value = std::make_shared<std::string>(other.get());
70 *std::static_pointer_cast<std::string>(this->value) = *std::static_pointer_cast<std::string>(other.value);
81 if (LIKELY(
this != &other)) {
82 this->value = std::move(other.value);
88 String& String::operator=(
const std::string &other)
throw () {
89 if (UNLIKELY(isNull())) {
90 this->value = std::make_shared<std::string>(other);
92 *std::static_pointer_cast<std::string>(this->value) = other;
98 String& String::operator=(
const char *other)
throw () {
99 if (UNLIKELY(isNull())) {
100 this->value = std::make_shared<std::string>(other);
102 *std::static_pointer_cast<std::string>(this->value) = other;
108 String& String::operator+=(
const std::string &value)
throw () {
109 (*std::static_pointer_cast<std::string>(this->value)) += value;
114 const std::string& String::get()
const throw (UnsupportedOperatorException) {
115 if (UNLIKELY(isNull())) {
116 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
119 return *std::static_pointer_cast<std::string>(this->value);
122 std::string& String::get() throw (UnsupportedOperatorException) {
123 if (UNLIKELY(isNull())) {
124 THROW(UnsupportedOperatorException,
"unable to take reference of null value");
127 return *std::static_pointer_cast<std::string>(this->value);
130 size_t String::size()
const throw () {
131 if (UNLIKELY(isNull())) {
138 bool String::empty()
const throw () {
139 if (UNLIKELY(isNull())) {
143 return get().empty();
146 void String::clear() throw() {
147 if (UNLIKELY(isNull())) {
148 this->value = std::make_shared<std::string>();
150 std::static_pointer_cast<std::string>(this->value)->clear();
154 void String::shrink_to_fit() throw () {
155 if (LIKELY(!isNull())) {
156 auto &s = *std::static_pointer_cast<std::string>(this->value);
158 std::string(s.begin(), s.end()).swap(s);
165 if (UNLIKELY(isNull())) {
166 c = mpack.append(offset);
168 c = mpack.append(offset, *std::static_pointer_cast<std::string>(this->value));
173 if (UNLIKELY(c == 0)) {
174 THROW(TypeCastException,
"Failed to append string '%s' at offset %d (buffer length: %d)", str(JSON).c_str(), offset, mpack.length());
178 void String::msgpackBufferSize(
size_t &size)
const throw () {
179 if (UNLIKELY(isNull())) {
185 size += 5 + std::static_pointer_cast<std::string>(this->value)->size();
188 std::string String::str(
int type)
const throw () {
189 if (UNLIKELY(isNull())) {
196 bool String::isType(
const char *value,
size_t size)
throw () {
197 return (size > 1 && value[0] ==
'"' && value[size - 1] ==
'"');