datapiece.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
32 
37 
41 
42 namespace google {
43 namespace protobuf {
44 namespace util {
45 namespace converter {
46 
47 ;
48 ;
49 ;
50 using util::Status;
51 using util::StatusOr;
52 using util::error::Code;
53 
54 namespace {
55 
56 inline Status InvalidArgument(StringPiece value_str) {
57  return Status(util::error::INVALID_ARGUMENT, value_str);
58 }
59 
60 template <typename To, typename From>
61 StatusOr<To> ValidateNumberConversion(To after, From before) {
62  if (after == before &&
63  MathUtil::Sign<From>(before) == MathUtil::Sign<To>(after)) {
64  return after;
65  } else {
66  return InvalidArgument(std::is_integral<From>::value
67  ? ValueAsString(before)
69  ? DoubleAsString(before)
70  : FloatAsString(before));
71  }
72 }
73 
74 // For general conversion between
75 // int32, int64, uint32, uint64, double and float
76 // except conversion between double and float.
77 template <typename To, typename From>
78 StatusOr<To> NumberConvertAndCheck(From before) {
79  if (std::is_same<From, To>::value) return before;
80 
81  To after = static_cast<To>(before);
82  return ValidateNumberConversion(after, before);
83 }
84 
85 // For conversion to integer types (int32, int64, uint32, uint64) from floating
86 // point types (double, float) only.
87 template <typename To, typename From>
88 StatusOr<To> FloatingPointToIntConvertAndCheck(From before) {
89  if (std::is_same<From, To>::value) return before;
90 
91  To after = static_cast<To>(before);
92  return ValidateNumberConversion(after, before);
93 }
94 
95 // For conversion between double and float only.
96 StatusOr<double> FloatToDouble(float before) {
97  // Casting float to double should just work as double has more precision
98  // than float.
99  return static_cast<double>(before);
100 }
101 
102 StatusOr<float> DoubleToFloat(double before) {
103  if (MathLimits<double>::IsNaN(before)) {
104  return std::numeric_limits<float>::quiet_NaN();
105  } else if (!MathLimits<double>::IsFinite(before)) {
106  // Converting a double +inf/-inf to float should just work.
107  return static_cast<float>(before);
108  } else if (before > std::numeric_limits<float>::max() ||
109  before < -std::numeric_limits<float>::max()) {
110  // Double value outside of the range of float.
111  return InvalidArgument(DoubleAsString(before));
112  } else {
113  return static_cast<float>(before);
114  }
115 }
116 
117 } // namespace
118 
120  if (type_ == TYPE_STRING) return StringToNumber<int32>(safe_strto32);
121 
122  if (type_ == TYPE_DOUBLE)
123  return FloatingPointToIntConvertAndCheck<int32, double>(double_);
124 
125  if (type_ == TYPE_FLOAT)
126  return FloatingPointToIntConvertAndCheck<int32, float>(float_);
127 
128  return GenericConvert<int32>();
129 }
130 
132  if (type_ == TYPE_STRING)
133  return StringToNumber<uint32>(safe_strtou32);
134 
135  if (type_ == TYPE_DOUBLE)
136  return FloatingPointToIntConvertAndCheck<uint32, double>(double_);
137 
138  if (type_ == TYPE_FLOAT)
139  return FloatingPointToIntConvertAndCheck<uint32, float>(float_);
140 
141  return GenericConvert<uint32>();
142 }
143 
145  if (type_ == TYPE_STRING) return StringToNumber<int64>(safe_strto64);
146 
147  if (type_ == TYPE_DOUBLE)
148  return FloatingPointToIntConvertAndCheck<int64, double>(double_);
149 
150  if (type_ == TYPE_FLOAT)
151  return FloatingPointToIntConvertAndCheck<int64, float>(float_);
152 
153  return GenericConvert<int64>();
154 }
155 
157  if (type_ == TYPE_STRING)
158  return StringToNumber<uint64>(safe_strtou64);
159 
160  if (type_ == TYPE_DOUBLE)
161  return FloatingPointToIntConvertAndCheck<uint64, double>(double_);
162 
163  if (type_ == TYPE_FLOAT)
164  return FloatingPointToIntConvertAndCheck<uint64, float>(float_);
165 
166  return GenericConvert<uint64>();
167 }
168 
170  if (type_ == TYPE_FLOAT) {
171  return FloatToDouble(float_);
172  }
173  if (type_ == TYPE_STRING) {
174  if (str_ == "Infinity") return std::numeric_limits<double>::infinity();
175  if (str_ == "-Infinity") return -std::numeric_limits<double>::infinity();
176  if (str_ == "NaN") return std::numeric_limits<double>::quiet_NaN();
177  StatusOr<double> value = StringToNumber<double>(safe_strtod);
178  if (value.ok() && !MathLimits<double>::IsFinite(value.ValueOrDie())) {
179  // safe_strtod converts out-of-range values to +inf/-inf, but we want
180  // to report them as errors.
181  return InvalidArgument(StrCat("\"", str_, "\""));
182  } else {
183  return value;
184  }
185  }
186  return GenericConvert<double>();
187 }
188 
190  if (type_ == TYPE_DOUBLE) {
191  return DoubleToFloat(double_);
192  }
193  if (type_ == TYPE_STRING) {
194  if (str_ == "Infinity") return std::numeric_limits<float>::infinity();
195  if (str_ == "-Infinity") return -std::numeric_limits<float>::infinity();
196  if (str_ == "NaN") return std::numeric_limits<float>::quiet_NaN();
197  // SafeStrToFloat() is used instead of safe_strtof() because the later
198  // does not fail on inputs like SimpleDtoa(DBL_MAX).
199  return StringToNumber<float>(SafeStrToFloat);
200  }
201  return GenericConvert<float>();
202 }
203 
205  switch (type_) {
206  case TYPE_BOOL:
207  return bool_;
208  case TYPE_STRING:
209  return StringToNumber<bool>(safe_strtob);
210  default:
211  return InvalidArgument(
212  ValueAsStringOrDefault("Wrong type. Cannot convert to Bool."));
213  }
214 }
215 
217  switch (type_) {
218  case TYPE_STRING:
219  return std::string(str_);
220  case TYPE_BYTES: {
223  return base64;
224  }
225  default:
226  return InvalidArgument(
227  ValueAsStringOrDefault("Cannot convert to string."));
228  }
229 }
230 
232  StringPiece default_string) const {
233  switch (type_) {
234  case TYPE_INT32:
235  return StrCat(i32_);
236  case TYPE_INT64:
237  return StrCat(i64_);
238  case TYPE_UINT32:
239  return StrCat(u32_);
240  case TYPE_UINT64:
241  return StrCat(u64_);
242  case TYPE_DOUBLE:
243  return DoubleAsString(double_);
244  case TYPE_FLOAT:
245  return FloatAsString(float_);
246  case TYPE_BOOL:
247  return SimpleBtoa(bool_);
248  case TYPE_STRING:
249  return StrCat("\"", str_.ToString(), "\"");
250  case TYPE_BYTES: {
253  return StrCat("\"", base64, "\"");
254  }
255  case TYPE_NULL:
256  return "null";
257  default:
258  return std::string(default_string);
259  }
260 }
261 
263  if (type_ == TYPE_BYTES) return str_.ToString();
264  if (type_ == TYPE_STRING) {
265  std::string decoded;
266  if (!DecodeBase64(str_, &decoded)) {
267  return InvalidArgument(ValueAsStringOrDefault("Invalid data in input."));
268  }
269  return decoded;
270  } else {
271  return InvalidArgument(ValueAsStringOrDefault(
272  "Wrong type. Only String or Bytes can be converted to Bytes."));
273  }
274 }
275 
277  bool use_lower_camel_for_enums,
278  bool case_insensitive_enum_parsing,
279  bool ignore_unknown_enum_values,
280  bool* is_unknown_enum_value) const {
282 
283  if (type_ == TYPE_STRING) {
284  // First try the given value as a name.
285  std::string enum_name = std::string(str_);
288  if (value != nullptr) return value->number();
289 
290  // Check if int version of enum is sent as string.
291  StatusOr<int32> int_value = ToInt32();
292  if (int_value.ok()) {
293  if (const google::protobuf::EnumValue* enum_value =
295  return enum_value->number();
296  }
297  }
298 
299  // Next try a normalized name.
300  bool should_normalize_enum =
301  case_insensitive_enum_parsing || use_lower_camel_for_enums;
302  if (should_normalize_enum) {
303  for (std::string::iterator it = enum_name.begin(); it != enum_name.end();
304  ++it) {
305  *it = *it == '-' ? '_' : ascii_toupper(*it);
306  }
308  if (value != nullptr) return value->number();
309  }
310 
311  // If use_lower_camel_for_enums is true try with enum name without
312  // underscore. This will also accept camel case names as the enum_name has
313  // been normalized before.
314  if (use_lower_camel_for_enums) {
316  if (value != nullptr) return value->number();
317  }
318 
319  // If ignore_unknown_enum_values is true an unknown enum value is ignored.
320  if (ignore_unknown_enum_values) {
321  *is_unknown_enum_value = true;
322  return enum_type->enumvalue(0).number();
323  }
324  } else {
325  // We don't need to check whether the value is actually declared in the
326  // enum because we preserve unknown enum values as well.
327  return ToInt32();
328  }
329  return InvalidArgument(
330  ValueAsStringOrDefault("Cannot find enum with given value."));
331 }
332 
333 template <typename To>
335  switch (type_) {
336  case TYPE_INT32:
337  return NumberConvertAndCheck<To, int32>(i32_);
338  case TYPE_INT64:
339  return NumberConvertAndCheck<To, int64>(i64_);
340  case TYPE_UINT32:
341  return NumberConvertAndCheck<To, uint32>(u32_);
342  case TYPE_UINT64:
343  return NumberConvertAndCheck<To, uint64>(u64_);
344  case TYPE_DOUBLE:
345  return NumberConvertAndCheck<To, double>(double_);
346  case TYPE_FLOAT:
347  return NumberConvertAndCheck<To, float>(float_);
348  default: // TYPE_ENUM, TYPE_STRING, TYPE_CORD, TYPE_BOOL
349  return InvalidArgument(ValueAsStringOrDefault(
350  "Wrong type. Bool, Enum, String and Cord not supported in "
351  "GenericConvert."));
352  }
353 }
354 
355 template <typename To>
357  To*)) const {
358  if (str_.size() > 0 && (str_[0] == ' ' || str_[str_.size() - 1] == ' ')) {
359  return InvalidArgument(StrCat("\"", str_, "\""));
360  }
361  To result;
362  if (func(str_, &result)) return result;
363  return InvalidArgument(StrCat("\"", string(str_), "\""));
364 }
365 
367  // Try web-safe decode first, if it fails, try the non-web-safe decode.
370  // In strict mode, check if the escaped version gives us the same value as
371  // unescaped.
372  std::string encoded;
373  // WebSafeBase64Escape does no padding by default.
374  WebSafeBase64Escape(*dest, &encoded);
375  // Remove trailing padding '=' characters before comparison.
376  StringPiece src_no_padding = StringPiece(src).substr(
377  0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1
378  : src.length());
379  return encoded == src_no_padding;
380  }
381  return true;
382  }
383 
384  if (Base64Unescape(src, dest)) {
386  std::string encoded;
387  Base64Escape(
388  reinterpret_cast<const unsigned char*>(dest->data()), dest->length(),
389  &encoded, false);
390  StringPiece src_no_padding = StringPiece(src).substr(
391  0, HasSuffixString(src, "=") ? src.find_last_not_of('=') + 1
392  : src.length());
393  return encoded == src_no_padding;
394  }
395  return true;
396  }
397 
398  return false;
399 }
400 
401 void DataPiece::InternalCopy(const DataPiece& other) {
402  type_ = other.type_;
404  switch (type_) {
405  case TYPE_INT32:
406  case TYPE_INT64:
407  case TYPE_UINT32:
408  case TYPE_UINT64:
409  case TYPE_DOUBLE:
410  case TYPE_FLOAT:
411  case TYPE_BOOL:
412  case TYPE_ENUM:
413  case TYPE_NULL:
414  case TYPE_BYTES:
415  case TYPE_STRING: {
416  str_ = other.str_;
417  break;
418  }
419  }
420 }
421 
422 } // namespace converter
423 } // namespace util
424 } // namespace protobuf
425 } // namespace google
google::protobuf::WebSafeBase64Unescape
int WebSafeBase64Unescape(const char *src, int szsrc, char *dest, int szdest)
Definition: strutil.cc:2061
google::protobuf::value
const Descriptor::ReservedRange value
Definition: src/google/protobuf/descriptor.h:1954
struct.pb.h
google::protobuf::Enum
google::protobuf::util::converter::DataPiece::StringToNumber
util::StatusOr< To > StringToNumber(bool(*func)(StringPiece, To *)) const
google::protobuf::util::StatusOr::ValueOrDie
const T & ValueOrDie() const
Definition: statusor.h:251
src
GLenum src
Definition: glcorearb.h:3364
google::protobuf::safe_strto32
bool safe_strto32(const string &str, int32 *value)
Definition: strutil.cc:1364
google::protobuf::util::converter::DataPiece::u64_
uint64 u64_
Definition: datapiece.h:202
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
google::protobuf::util::error::Code
Code
Definition: status.h:46
google::protobuf::util::converter::DataPiece::u32_
uint32 u32_
Definition: datapiece.h:201
google::protobuf::util::converter::DataPiece::ToInt64
util::StatusOr< int64 > ToInt64() const
Definition: datapiece.cc:144
google::protobuf::safe_strtob
bool safe_strtob(StringPiece str, bool *value)
Definition: strutil.cc:1324
google::protobuf::safe_strtod
bool safe_strtod(const char *str, double *value)
Definition: strutil.cc:1352
google::protobuf::safe_strto64
bool safe_strto64(const string &str, int64 *value)
Definition: strutil.cc:1372
google::protobuf::util::converter::DataPiece::float_
float float_
Definition: datapiece.h:204
google::protobuf::MathLimits::IsNaN
static bool IsNaN(const Type x)
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
google::protobuf::util::converter::SafeStrToFloat
bool SafeStrToFloat(StringPiece str, float *value)
Definition: utility.cc:391
google::protobuf::util::converter::DataPiece::GenericConvert
util::StatusOr< To > GenericConvert() const
google::protobuf::StringPiece::substr
StringPiece substr(size_type pos, size_type n=npos) const
Definition: stringpiece.cc:261
enum_type
zend_class_entry * enum_type
Definition: php/ext/google/protobuf/message.c:1904
google::protobuf::safe_strtou32
bool safe_strtou32(const string &str, uint32 *value)
Definition: strutil.cc:1368
utility.h
google::protobuf::EnumValue
google::protobuf::util::converter::DataPiece::DecodeBase64
bool DecodeBase64(StringPiece src, std::string *dest) const
Definition: datapiece.cc:366
google::protobuf::util::converter::DataPiece::ToDouble
util::StatusOr< double > ToDouble() const
Definition: datapiece.cc:169
google::protobuf::util::converter::DataPiece::ToInt32
util::StatusOr< int32 > ToInt32() const
Definition: datapiece.cc:119
google::protobuf::util::converter::DataPiece::ToFloat
util::StatusOr< float > ToFloat() const
Definition: datapiece.cc:189
google::protobuf::Base64Unescape
bool Base64Unescape(StringPiece src, string *dest)
Definition: strutil.cc:2090
google::protobuf::MathLimits
Definition: mathlimits.h:78
NULL_VALUE
@ NULL_VALUE
Definition: struct.pb.h:84
base64
google::protobuf::util::converter::DataPiece::ValueAsStringOrDefault
std::string ValueAsStringOrDefault(StringPiece default_string) const
Definition: datapiece.cc:231
google::protobuf::util::converter::DataPiece::TYPE_BYTES
@ TYPE_BYTES
Definition: datapiece.h:73
google::protobuf::util::converter::DataPiece::bool_
bool bool_
Definition: datapiece.h:205
strutil.h
google::protobuf::util::converter::DataPiece::i32_
int32 i32_
Definition: datapiece.h:199
google::protobuf::util::converter::DataPiece::TYPE_FLOAT
@ TYPE_FLOAT
Definition: datapiece.h:69
google::protobuf::util::converter::DataPiece::TYPE_NULL
@ TYPE_NULL
Definition: datapiece.h:74
mathlimits.h
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::converter::DataPiece::type_
Type type_
Definition: datapiece.h:193
google::protobuf::util::converter::DataPiece::ToUint32
util::StatusOr< uint32 > ToUint32() const
Definition: datapiece.cc:131
google::protobuf::util::converter::DataPiece::TYPE_BOOL
@ TYPE_BOOL
Definition: datapiece.h:70
google::protobuf::util::converter::DataPiece::InternalCopy
void InternalCopy(const DataPiece &other)
Definition: datapiece.cc:401
google::protobuf::WebSafeBase64Escape
int WebSafeBase64Escape(const unsigned char *src, int szsrc, char *dest, int szdest, bool do_padding)
Definition: strutil.cc:2207
mathutil.h
google::protobuf::util::converter::DataPiece::TYPE_STRING
@ TYPE_STRING
Definition: datapiece.h:72
google::protobuf::util::converter::FindEnumValueByNumberOrNull
const google::protobuf::EnumValue * FindEnumValueByNumberOrNull(const google::protobuf::Enum *enum_type, int32 value)
Definition: utility.cc:204
google::protobuf.internal::StringPiecePod::ToString
std::string ToString() const
Definition: stringpiece.h:465
google::protobuf::util::converter::DataPiece::ToString
util::StatusOr< std::string > ToString() const
Definition: datapiece.cc:216
google::protobuf::util::error::INVALID_ARGUMENT
@ INVALID_ARGUMENT
Definition: status.h:50
google::protobuf::util::converter::DataPiece::use_strict_base64_decoding_
bool use_strict_base64_decoding_
Definition: datapiece.h:210
google::protobuf::util::StatusOr
Definition: statusor.h:99
aditof::Status
Status
Status of any operation that the TOF sdk performs.
Definition: status_definitions.h:48
google::protobuf::util::converter::DataPiece::TYPE_INT32
@ TYPE_INT32
Definition: datapiece.h:64
datapiece.h
google::protobuf::util::converter::DoubleAsString
std::string DoubleAsString(double value)
Definition: utility.cc:378
google::protobuf::util::converter::FindEnumValueByNameWithoutUnderscoreOrNull
const google::protobuf::EnumValue * FindEnumValueByNameWithoutUnderscoreOrNull(const google::protobuf::Enum *enum_type, StringPiece enum_name)
Definition: utility.cc:217
google::protobuf::SimpleBtoa
string SimpleBtoa(bool value)
Definition: strutil.h:525
google::protobuf::util::converter::FindEnumValueByNameOrNull
const google::protobuf::EnumValue * FindEnumValueByNameOrNull(const google::protobuf::Enum *enum_type, StringPiece enum_name)
Definition: utility.cc:191
google::protobuf::Base64Escape
int Base64Escape(const unsigned char *src, int szsrc, char *dest, int szdest)
Definition: strutil.cc:2204
benchmarks.python.py_benchmark.dest
dest
Definition: py_benchmark.py:13
google::protobuf::util::converter::DataPiece::TYPE_UINT32
@ TYPE_UINT32
Definition: datapiece.h:66
google::protobuf::util::converter::DataPiece::ToBool
util::StatusOr< bool > ToBool() const
Definition: datapiece.cc:204
google::protobuf::util::converter::DataPiece::TYPE_DOUBLE
@ TYPE_DOUBLE
Definition: datapiece.h:68
google::protobuf::util::converter::DataPiece::i64_
int64 i64_
Definition: datapiece.h:200
google::protobuf::util::converter::DataPiece::ToEnum
util::StatusOr< int > ToEnum(const google::protobuf::Enum *enum_type, bool use_lower_camel_for_enums, bool case_insensitive_enum_parsing, bool ignore_unknown_enum_values, bool *is_unknown_enum_value) const
Definition: datapiece.cc:276
google::protobuf::ascii_toupper
char ascii_toupper(char c)
Definition: strutil.h:90
func
GLenum func
Definition: glcorearb.h:3052
descriptor.h
type.pb.h
google::protobuf::util::StatusOr::ok
bool ok() const
Definition: statusor.h:246
google::protobuf::util::converter::DataPiece::ToUint64
util::StatusOr< uint64 > ToUint64() const
Definition: datapiece.cc:156
google::protobuf::HasSuffixString
bool HasSuffixString(const string &str, const string &suffix)
Definition: strutil.h:137
google::protobuf::util::converter::DataPiece::TYPE_ENUM
@ TYPE_ENUM
Definition: datapiece.h:71
google::protobuf::util::converter::ValueAsString
std::string ValueAsString(T value)
Definition: utility.h:179
google::protobuf::util::converter::FloatAsString
std::string FloatAsString(float value)
Definition: utility.cc:386
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
google::protobuf.internal::StringPiecePod::size
stringpiece_ssize_type size() const
Definition: stringpiece.h:461
google::protobuf::util::converter::DataPiece::TYPE_UINT64
@ TYPE_UINT64
Definition: datapiece.h:67
google::protobuf::safe_strtou64
bool safe_strtou64(const string &str, uint64 *value)
Definition: strutil.cc:1376
google::protobuf::util::converter::DataPiece
Definition: datapiece.h:59
google::protobuf::util::converter::DataPiece::str_
StringPiecePod str_
Definition: datapiece.h:206
google::protobuf::util::converter::DataPiece::double_
double double_
Definition: datapiece.h:203
google::protobuf::util::converter::DataPiece::ToBytes
util::StatusOr< std::string > ToBytes() const
Definition: datapiece.cc:262
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::util::converter::DataPiece::TYPE_INT64
@ TYPE_INT64
Definition: datapiece.h:65
google::protobuf::MathLimits::IsFinite
static bool IsFinite(const Type x)


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:49