utility.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 
33 #include <algorithm>
34 
45 
46 #include <google/protobuf/port_def.inc>
47 
48 namespace google {
49 namespace protobuf {
50 namespace util {
51 namespace converter {
52 
55  const std::string& option_name, bool default_value) {
56  const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
57  if (opt == nullptr) {
58  return default_value;
59  }
60  return GetBoolFromAny(opt->value());
61 }
62 
65  const std::string& option_name, int64 default_value) {
66  const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
67  if (opt == nullptr) {
68  return default_value;
69  }
70  return GetInt64FromAny(opt->value());
71 }
72 
75  const std::string& option_name, double default_value) {
76  const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
77  if (opt == nullptr) {
78  return default_value;
79  }
80  return GetDoubleFromAny(opt->value());
81 }
82 
85  const std::string& option_name, const std::string& default_value) {
86  const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
87  if (opt == nullptr) {
88  return default_value;
89  }
90  return GetStringFromAny(opt->value());
91 }
92 
93 template <typename T>
94 void ParseFromAny(const std::string& data, T* result) {
95  result->ParseFromString(data);
96 }
97 
98 // Returns a boolean value contained in Any type.
99 // TODO(skarvaje): Add type checking & error messages here.
102  ParseFromAny(any.value(), &b);
103  return b.value();
104 }
105 
108  ParseFromAny(any.value(), &i);
109  return i.value();
110 }
111 
114  ParseFromAny(any.value(), &i);
115  return i.value();
116 }
117 
120  ParseFromAny(any.value(), &s);
121  return s.value();
122 }
123 
125  if (type_url.size() > kTypeUrlSize && type_url[kTypeUrlSize] == '/') {
126  return type_url.substr(kTypeUrlSize + 1);
127  } else {
128  size_t idx = type_url.rfind('/');
129  if (idx != type_url.npos) {
130  type_url.remove_prefix(idx + 1);
131  }
132  return type_url;
133  }
134 }
135 
137  return StrCat(kTypeServiceBaseUrl, "/", simple_type);
138 }
139 
142  const std::string& option_name) {
143  for (int i = 0; i < options.size(); ++i) {
144  const google::protobuf::Option& opt = options.Get(i);
145  if (opt.name() == option_name) {
146  return &opt;
147  }
148  }
149  return nullptr;
150 }
151 
153  const google::protobuf::Type* type, StringPiece field_name) {
154  if (type != nullptr) {
155  for (int i = 0; i < type->fields_size(); ++i) {
156  const google::protobuf::Field& field = type->fields(i);
157  if (field.name() == field_name) {
158  return &field;
159  }
160  }
161  }
162  return nullptr;
163 }
164 
166  const google::protobuf::Type* type, StringPiece json_name) {
167  if (type != nullptr) {
168  for (int i = 0; i < type->fields_size(); ++i) {
169  const google::protobuf::Field& field = type->fields(i);
170  if (field.json_name() == json_name) {
171  return &field;
172  }
173  }
174  }
175  return nullptr;
176 }
177 
180  if (type != nullptr) {
181  for (int i = 0; i < type->fields_size(); ++i) {
182  const google::protobuf::Field& field = type->fields(i);
183  if (field.number() == number) {
184  return &field;
185  }
186  }
187  }
188  return nullptr;
189 }
190 
192  const google::protobuf::Enum* enum_type, StringPiece enum_name) {
193  if (enum_type != nullptr) {
194  for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
195  const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
196  if (enum_value.name() == enum_name) {
197  return &enum_value;
198  }
199  }
200  }
201  return nullptr;
202 }
203 
206  if (enum_type != nullptr) {
207  for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
208  const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
209  if (enum_value.number() == value) {
210  return &enum_value;
211  }
212  }
213  }
214  return nullptr;
215 }
216 
218  const google::protobuf::Enum* enum_type, StringPiece enum_name) {
219  if (enum_type != nullptr) {
220  for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
221  const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
222  std::string enum_name_without_underscore = enum_value.name();
223 
224  // Remove underscore from the name.
225  enum_name_without_underscore.erase(
226  std::remove(enum_name_without_underscore.begin(),
227  enum_name_without_underscore.end(), '_'),
228  enum_name_without_underscore.end());
229  // Make the name uppercase.
230  for (std::string::iterator it = enum_name_without_underscore.begin();
231  it != enum_name_without_underscore.end(); ++it) {
232  *it = ascii_toupper(*it);
233  }
234 
235  if (enum_name_without_underscore == enum_name) {
236  return &enum_value;
237  }
238  }
239  }
240  return nullptr;
241 }
242 
244  std::string input_string(input);
245  std::transform(input_string.begin(), input_string.end(), input_string.begin(),
246  ::tolower);
247  return ToCamelCase(input_string);
248 }
249 
251  bool capitalize_next = false;
252  bool was_cap = true;
253  bool is_cap = false;
254  bool first_word = true;
255  std::string result;
256  result.reserve(input.size());
257 
258  for (size_t i = 0; i < input.size(); ++i, was_cap = is_cap) {
259  is_cap = ascii_isupper(input[i]);
260  if (input[i] == '_') {
261  capitalize_next = true;
262  if (!result.empty()) first_word = false;
263  continue;
264  } else if (first_word) {
265  // Consider when the current character B is capitalized,
266  // first word ends when:
267  // 1) following a lowercase: "...aB..."
268  // 2) followed by a lowercase: "...ABc..."
269  if (!result.empty() && is_cap &&
270  (!was_cap || (i + 1 < input.size() && ascii_islower(input[i + 1])))) {
271  first_word = false;
272  result.push_back(input[i]);
273  } else {
274  result.push_back(ascii_tolower(input[i]));
275  continue;
276  }
277  } else if (capitalize_next) {
278  capitalize_next = false;
279  if (ascii_islower(input[i])) {
280  result.push_back(ascii_toupper(input[i]));
281  continue;
282  } else {
283  result.push_back(input[i]);
284  continue;
285  }
286  } else {
287  result.push_back(ascii_tolower(input[i]));
288  }
289  }
290  return result;
291 }
292 
294  bool was_not_underscore = false; // Initialize to false for case 1 (below)
295  bool was_not_cap = false;
296  std::string result;
297  result.reserve(input.size() << 1);
298 
299  for (size_t i = 0; i < input.size(); ++i) {
300  if (ascii_isupper(input[i])) {
301  // Consider when the current character B is capitalized:
302  // 1) At beginning of input: "B..." => "b..."
303  // (e.g. "Biscuit" => "biscuit")
304  // 2) Following a lowercase: "...aB..." => "...a_b..."
305  // (e.g. "gBike" => "g_bike")
306  // 3) At the end of input: "...AB" => "...ab"
307  // (e.g. "GoogleLAB" => "google_lab")
308  // 4) Followed by a lowercase: "...ABc..." => "...a_bc..."
309  // (e.g. "GBike" => "g_bike")
310  if (was_not_underscore && // case 1 out
311  (was_not_cap || // case 2 in, case 3 out
312  (i + 1 < input.size() && // case 3 out
313  ascii_islower(input[i + 1])))) { // case 4 in
314  // We add an underscore for case 2 and case 4.
315  result.push_back('_');
316  }
317  result.push_back(ascii_tolower(input[i]));
318  was_not_underscore = true;
319  was_not_cap = false;
320  } else {
321  result.push_back(input[i]);
322  was_not_underscore = input[i] != '_';
323  was_not_cap = true;
324  }
325  }
326  return result;
327 }
328 
329 std::set<std::string>* well_known_types_ = NULL;
332  "google.protobuf.Timestamp", "google.protobuf.Duration",
333  "google.protobuf.DoubleValue", "google.protobuf.FloatValue",
334  "google.protobuf.Int64Value", "google.protobuf.UInt64Value",
335  "google.protobuf.Int32Value", "google.protobuf.UInt32Value",
336  "google.protobuf.BoolValue", "google.protobuf.StringValue",
337  "google.protobuf.BytesValue", "google.protobuf.FieldMask"};
338 
340 
342  well_known_types_ = new std::set<std::string>;
343  for (int i = 0; i < GOOGLE_ARRAYSIZE(well_known_types_name_array_); ++i) {
345  }
347 }
348 
349 bool IsWellKnownType(const std::string& type_name) {
352  return ContainsKey(*well_known_types_, type_name);
353 }
354 
355 bool IsValidBoolString(const std::string& bool_string) {
356  return bool_string == "true" || bool_string == "false" ||
357  bool_string == "1" || bool_string == "0";
358 }
359 
361  const google::protobuf::Type& type) {
362  return field.cardinality() ==
364  (GetBoolOptionOrDefault(type.options(), "map_entry", false) ||
365  GetBoolOptionOrDefault(type.options(),
366  "google.protobuf.MessageOptions.map_entry",
367  false));
368 }
369 
371  return GetBoolOptionOrDefault(type.options(), "message_set_wire_format",
372  false) ||
374  type.options(),
375  "google.protobuf.MessageOptions.message_set_wire_format", false);
376 }
377 
379  if (MathLimits<double>::IsPosInf(value)) return "Infinity";
380  if (MathLimits<double>::IsNegInf(value)) return "-Infinity";
381  if (MathLimits<double>::IsNaN(value)) return "NaN";
382 
383  return SimpleDtoa(value);
384 }
385 
388  return DoubleAsString(value);
389 }
390 
392  double double_value;
393  if (!safe_strtod(str, &double_value)) {
394  return false;
395  }
396 
397  if (MathLimits<double>::IsInf(double_value) ||
398  MathLimits<double>::IsNaN(double_value))
399  return false;
400 
401  // Fail if the value is not representable in float.
402  if (double_value > std::numeric_limits<float>::max() ||
403  double_value < -std::numeric_limits<float>::max()) {
404  return false;
405  }
406 
407  *value = static_cast<float>(double_value);
408  return true;
409 }
410 
411 } // namespace converter
412 } // namespace util
413 } // namespace protobuf
414 } // namespace google
google::protobuf::RepeatedPtrField
Definition: command_line_interface.h:62
map_util.h
BoolValue
struct BoolValue BoolValue
Definition: php/ext/google/protobuf/protobuf.h:626
google::protobuf::SimpleDtoa
string SimpleDtoa(double value)
Definition: strutil.cc:1219
google::protobuf::util::converter::FindJsonFieldInTypeOrNull
const google::protobuf::Field * FindJsonFieldInTypeOrNull(const google::protobuf::Type *type, StringPiece json_name)
Definition: utility.cc:165
google::protobuf::util::converter::ToCamelCase
std::string ToCamelCase(const StringPiece input)
Definition: utility.cc:250
google::protobuf::util::converter::IsWellKnownType
bool IsWellKnownType(const std::string &type_name)
Definition: utility.cc:349
google::protobuf::Enum
google::protobuf::ContainsKey
bool ContainsKey(const Collection &collection, const Key &key)
Definition: map_util.h:239
NULL
NULL
Definition: test_security_zap.cpp:405
google::protobuf::util::converter::GetTypeWithoutUrl
const StringPiece GetTypeWithoutUrl(StringPiece type_url)
Definition: utility.cc:124
google::protobuf::int64
int64_t int64
Definition: protobuf/src/google/protobuf/stubs/port.h:151
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: strutil.cc:1480
options
Message * options
Definition: src/google/protobuf/descriptor.cc:3119
input
std::string input
Definition: tokenizer_unittest.cc:197
s
XmlRpcServer s
google::protobuf::safe_strtod
bool safe_strtod(const char *str, double *value)
Definition: strutil.cc:1352
google::protobuf.internal::call_once
void call_once(Args &&... args)
Definition: once.h:45
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::ascii_tolower
char ascii_tolower(char c)
Definition: strutil.h:94
enum_type
zend_class_entry * enum_type
Definition: php/ext/google/protobuf/message.c:1904
google::protobuf::util::converter::ToSnakeCase
std::string ToSnakeCase(StringPiece input)
Definition: utility.cc:293
utility.h
google::protobuf::util::converter::kTypeServiceBaseUrl
const char kTypeServiceBaseUrl[]
Definition: constants.h:43
T
#define T(upbtypeconst, upbtype, ctype, default_value)
google::protobuf::EnumValue
callback.h
idx
static uint32_t idx(tarjan *t, const upb_refcounted *r)
Definition: ruby/ext/google/protobuf_c/upb.c:5925
google::protobuf.internal::OnShutdown
void OnShutdown(void(*func)())
Definition: common.cc:344
google::protobuf::Int64Value
google::protobuf::ascii_isupper
bool ascii_isupper(char c)
Definition: strutil.h:82
b
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:3228
google::protobuf::util::converter::IsMessageSetWireFormat
bool IsMessageSetWireFormat(const google::protobuf::Type &type)
Definition: utility.cc:370
GOOGLE_ARRAYSIZE
#define GOOGLE_ARRAYSIZE(a)
Definition: macros.h:88
google::protobuf::int32
int32_t int32
Definition: protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::MathLimits
Definition: mathlimits.h:78
google::protobuf::util::converter::GetFullTypeWithUrl
const std::string GetFullTypeWithUrl(StringPiece simple_type)
Definition: utility.cc:136
strutil.h
google::protobuf::util::converter::GetInt64OptionOrDefault
int64 GetInt64OptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, int64 default_value)
Definition: utility.cc:63
google::protobuf.internal::once_flag
std::once_flag once_flag
Definition: once.h:43
google::protobuf::util::converter::kTypeUrlSize
static const int64 kTypeUrlSize
Definition: utility.h:57
mathlimits.h
google::protobuf::StringPiece
Definition: stringpiece.h:180
google::protobuf::util::converter::InitWellKnownTypes
void InitWellKnownTypes()
Definition: utility.cc:341
update_failure_list.str
str
Definition: update_failure_list.py:41
google::protobuf::util::converter::GetDoubleFromAny
double GetDoubleFromAny(const google::protobuf::Any &any)
Definition: utility.cc:112
google::protobuf::util::converter::IsMap
bool IsMap(const google::protobuf::Field &field, const google::protobuf::Type &type)
Definition: utility.cc:360
google::protobuf::util::converter::FindOptionOrNull
const google::protobuf::Option * FindOptionOrNull(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name)
Definition: utility.cc:140
Field_Cardinality_CARDINALITY_REPEATED
@ Field_Cardinality_CARDINALITY_REPEATED
Definition: type.pb.h:132
google::protobuf::util::converter::FindEnumValueByNumberOrNull
const google::protobuf::EnumValue * FindEnumValueByNumberOrNull(const google::protobuf::Enum *enum_type, int32 value)
Definition: utility.cc:204
google::protobuf::util::converter::GetDoubleOptionOrDefault
double GetDoubleOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, double default_value)
Definition: utility.cc:73
google::protobuf::util::converter::EnumValueNameToLowerCamelCase
std::string EnumValueNameToLowerCamelCase(const StringPiece input)
Definition: utility.cc:243
google::protobuf::SimpleFtoa
string SimpleFtoa(float value)
Definition: strutil.cc:1224
google::protobuf::util::converter::well_known_types_
std::set< std::string > * well_known_types_
Definition: utility.cc:329
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
StringValue
struct StringValue StringValue
Definition: php/ext/google/protobuf/protobuf.h:660
google::protobuf::util::converter::GetStringFromAny
std::string GetStringFromAny(const google::protobuf::Any &any)
Definition: utility.cc:118
i
int i
Definition: gmock-matchers_test.cc:764
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::util::converter::FindFieldInTypeByNumberOrNull
const google::protobuf::Field * FindFieldInTypeByNumberOrNull(const google::protobuf::Type *type, int32 number)
Definition: utility.cc:178
Field
struct Field Field
Definition: php/ext/google/protobuf/protobuf.h:638
Any
struct Any Any
Definition: php/ext/google/protobuf/protobuf.h:624
type
GLenum type
Definition: glcorearb.h:2695
google::protobuf::util::converter::GetInt64FromAny
int64 GetInt64FromAny(const google::protobuf::Any &any)
Definition: utility.cc:106
google::protobuf::util::converter::GetStringOptionOrDefault
std::string GetStringOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, const std::string &default_value)
Definition: utility.cc:83
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::util::converter::ParseFromAny
void ParseFromAny(const std::string &data, T *result)
Definition: utility.cc:94
common.h
wrappers.pb.h
DoubleValue
struct DoubleValue DoubleValue
Definition: php/ext/google/protobuf/protobuf.h:630
google::protobuf::ascii_islower
bool ascii_islower(char c)
Definition: strutil.h:86
google::protobuf::util::converter::GetBoolOptionOrDefault
bool GetBoolOptionOrDefault(const RepeatedPtrField< google::protobuf::Option > &options, const std::string &option_name, bool default_value)
Definition: utility.cc:53
constants.h
google::protobuf::ascii_toupper
char ascii_toupper(char c)
Definition: strutil.h:90
logging.h
google::protobuf::util::converter::well_known_types_init_
PROTOBUF_NAMESPACE_ID::internal::once_flag well_known_types_init_
Definition: utility.cc:330
descriptor.h
default_value
def default_value(type_)
google::protobuf::util::converter::FindFieldInTypeOrNull
const google::protobuf::Field * FindFieldInTypeOrNull(const google::protobuf::Type *type, StringPiece field_name)
Definition: utility.cc:152
Type
struct Type Type
Definition: php/ext/google/protobuf/protobuf.h:664
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
google::protobuf::util::converter::well_known_types_name_array_
const char * well_known_types_name_array_[]
Definition: utility.cc:331
google::protobuf::Option
google::protobuf::util::converter::GetBoolFromAny
bool GetBoolFromAny(const google::protobuf::Any &any)
Definition: utility.cc:100
type_url
string * type_url
Definition: conformance_cpp.cc:98
google::protobuf::util::converter::FloatAsString
std::string FloatAsString(float value)
Definition: utility.cc:386
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
descriptor.pb.h
google::protobuf::util::converter::DeleteWellKnownTypes
void DeleteWellKnownTypes()
Definition: utility.cc:339
google::protobuf::util::converter::IsValidBoolString
bool IsValidBoolString(const std::string &bool_string)
Definition: utility.cc:355
number
double number
Definition: cJSON.h:326
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
google
Definition: data_proto2_to_proto3_util.h:11


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:01