1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
#ifndef STRING_VIEW_H
#define STRING_VIEW_H
#include <Ice/Ice.h>
//
// COMPILERFIX: G++ false positive "maybe-uninitialized" warnings when using
// string_view with Ice::optional in C++17 mode.
//
#if defined(__GNUC__) && !defined(__clang__) && ICE_CPLUSPLUS >= 201703L
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
namespace Util
{
//
// A simplified version of boost::string_ref and the new
// std::experimental::fundamentals_v1::string_view
//
class string_view
{
public:
string_view() :
_data(0),
_size(0)
{}
string_view(const string_view& sv) :
_data(sv._data),
_size(sv._size)
{}
string_view(const std::string& s) :
_data(s.data()),
_size(s.length())
{}
string_view(const char* str) :
_data(str),
_size(strlen(str))
{
}
string_view(const char* str, size_t len) :
_data(str),
_size(len)
{}
string_view& operator=(const string_view& sv)
{
_data = sv._data;
_size = sv._size;
return *this;
}
size_t size() const
{
return _size;
}
size_t length() const
{
return _size;
}
bool empty() const
{
return _size != 0;
}
const char* data() const
{
return _data;
}
void clear()
{
_data = 0;
_size = 0;
}
std::string to_string() const
{
return std::string(_data, _size);
}
int compare(string_view str) const
{
if(_size == str._size)
{
if(_data == str._data)
{
return 0;
}
else
{
return strncmp(_data, str._data, _size);
}
}
else if(_size < str._size)
{
return -1;
}
else
{
return 1;
}
}
private:
const char* _data;
size_t _size;
};
inline bool
operator==(string_view lhs, string_view rhs)
{
return lhs.compare(rhs) == 0;
}
inline bool
operator!=(string_view lhs, string_view rhs)
{
return lhs.compare(rhs) != 0;
}
}
namespace Ice
{
//
// Describes how to marshal/unmarshal a Util::string_view
// It would be the same for a string_ref or std...::string_view
//
template<>
struct StreamableTraits<Util::string_view>
{
static const StreamHelperCategory helper = StreamHelperCategoryBuiltin;
static const int minWireSize = 1;
static const bool fixedLength = false;
};
template<>
struct StreamHelper<Util::string_view, StreamHelperCategoryBuiltin>
{
template<class S> static inline void
write(S* stream, const Util::string_view& v)
{
#ifdef ICE_CPP11_MAPPING
stream->write(v.data(), v.size());
#else
//
// In C++98, for consistency with the read, we don't string-convert
//
stream->write(v.data(), v.size(), false);
#endif
}
template<class S> static inline void
read(S* stream, Util::string_view& v)
{
const char* vdata = 0;
size_t vsize = 0;
//
// In C++98, we ignore the string converter
//
stream->read(vdata, vsize);
if(vsize > 0)
{
v = Util::string_view(vdata, vsize);
}
else
{
v.clear();
}
}
};
}
#if defined(__GNUC__) && !defined(__clang__) && ICE_CPLUSPLUS >= 201703L
# pragma GCC diagnostic pop
#endif
#endif
|