诸暨麻将添加redis
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

416 行
14 KiB

  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. #include <google/protobuf/util/internal/utility.h>
  31. #include <algorithm>
  32. #include <google/protobuf/stubs/callback.h>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/stubs/logging.h>
  35. #include <google/protobuf/wrappers.pb.h>
  36. #include <google/protobuf/descriptor.pb.h>
  37. #include <google/protobuf/descriptor.h>
  38. #include <google/protobuf/util/internal/constants.h>
  39. #include <google/protobuf/stubs/strutil.h>
  40. #include <google/protobuf/stubs/map_util.h>
  41. #include <google/protobuf/stubs/mathlimits.h>
  42. // clang-format off
  43. #include <google/protobuf/port_def.inc>
  44. // clang-format on
  45. namespace google {
  46. namespace protobuf {
  47. namespace util {
  48. namespace converter {
  49. bool GetBoolOptionOrDefault(
  50. const RepeatedPtrField<google::protobuf::Option>& options,
  51. StringPiece option_name, bool default_value) {
  52. const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
  53. if (opt == nullptr) {
  54. return default_value;
  55. }
  56. return GetBoolFromAny(opt->value());
  57. }
  58. int64 GetInt64OptionOrDefault(
  59. const RepeatedPtrField<google::protobuf::Option>& options,
  60. StringPiece option_name, int64 default_value) {
  61. const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
  62. if (opt == nullptr) {
  63. return default_value;
  64. }
  65. return GetInt64FromAny(opt->value());
  66. }
  67. double GetDoubleOptionOrDefault(
  68. const RepeatedPtrField<google::protobuf::Option>& options,
  69. StringPiece option_name, double default_value) {
  70. const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
  71. if (opt == nullptr) {
  72. return default_value;
  73. }
  74. return GetDoubleFromAny(opt->value());
  75. }
  76. std::string GetStringOptionOrDefault(
  77. const RepeatedPtrField<google::protobuf::Option>& options,
  78. StringPiece option_name, StringPiece default_value) {
  79. const google::protobuf::Option* opt = FindOptionOrNull(options, option_name);
  80. if (opt == nullptr) {
  81. return std::string(default_value);
  82. }
  83. return GetStringFromAny(opt->value());
  84. }
  85. template <typename T>
  86. void ParseFromAny(const std::string& data, T* result) {
  87. result->ParseFromString(data);
  88. }
  89. // Returns a boolean value contained in Any type.
  90. // TODO(skarvaje): Add type checking & error messages here.
  91. bool GetBoolFromAny(const google::protobuf::Any& any) {
  92. google::protobuf::BoolValue b;
  93. ParseFromAny(any.value(), &b);
  94. return b.value();
  95. }
  96. int64 GetInt64FromAny(const google::protobuf::Any& any) {
  97. google::protobuf::Int64Value i;
  98. ParseFromAny(any.value(), &i);
  99. return i.value();
  100. }
  101. double GetDoubleFromAny(const google::protobuf::Any& any) {
  102. google::protobuf::DoubleValue i;
  103. ParseFromAny(any.value(), &i);
  104. return i.value();
  105. }
  106. std::string GetStringFromAny(const google::protobuf::Any& any) {
  107. google::protobuf::StringValue s;
  108. ParseFromAny(any.value(), &s);
  109. return s.value();
  110. }
  111. const StringPiece GetTypeWithoutUrl(StringPiece type_url) {
  112. if (type_url.size() > kTypeUrlSize && type_url[kTypeUrlSize] == '/') {
  113. return type_url.substr(kTypeUrlSize + 1);
  114. } else {
  115. size_t idx = type_url.rfind('/');
  116. if (idx != type_url.npos) {
  117. type_url.remove_prefix(idx + 1);
  118. }
  119. return type_url;
  120. }
  121. }
  122. const std::string GetFullTypeWithUrl(StringPiece simple_type) {
  123. return StrCat(kTypeServiceBaseUrl, "/", simple_type);
  124. }
  125. const google::protobuf::Option* FindOptionOrNull(
  126. const RepeatedPtrField<google::protobuf::Option>& options,
  127. StringPiece option_name) {
  128. for (int i = 0; i < options.size(); ++i) {
  129. const google::protobuf::Option& opt = options.Get(i);
  130. if (opt.name() == option_name) {
  131. return &opt;
  132. }
  133. }
  134. return nullptr;
  135. }
  136. const google::protobuf::Field* FindFieldInTypeOrNull(
  137. const google::protobuf::Type* type, StringPiece field_name) {
  138. if (type != nullptr) {
  139. for (int i = 0; i < type->fields_size(); ++i) {
  140. const google::protobuf::Field& field = type->fields(i);
  141. if (field.name() == field_name) {
  142. return &field;
  143. }
  144. }
  145. }
  146. return nullptr;
  147. }
  148. const google::protobuf::Field* FindJsonFieldInTypeOrNull(
  149. const google::protobuf::Type* type, StringPiece json_name) {
  150. if (type != nullptr) {
  151. for (int i = 0; i < type->fields_size(); ++i) {
  152. const google::protobuf::Field& field = type->fields(i);
  153. if (field.json_name() == json_name) {
  154. return &field;
  155. }
  156. }
  157. }
  158. return nullptr;
  159. }
  160. const google::protobuf::Field* FindFieldInTypeByNumberOrNull(
  161. const google::protobuf::Type* type, int32 number) {
  162. if (type != nullptr) {
  163. for (int i = 0; i < type->fields_size(); ++i) {
  164. const google::protobuf::Field& field = type->fields(i);
  165. if (field.number() == number) {
  166. return &field;
  167. }
  168. }
  169. }
  170. return nullptr;
  171. }
  172. const google::protobuf::EnumValue* FindEnumValueByNameOrNull(
  173. const google::protobuf::Enum* enum_type, StringPiece enum_name) {
  174. if (enum_type != nullptr) {
  175. for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
  176. const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
  177. if (enum_value.name() == enum_name) {
  178. return &enum_value;
  179. }
  180. }
  181. }
  182. return nullptr;
  183. }
  184. const google::protobuf::EnumValue* FindEnumValueByNumberOrNull(
  185. const google::protobuf::Enum* enum_type, int32 value) {
  186. if (enum_type != nullptr) {
  187. for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
  188. const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
  189. if (enum_value.number() == value) {
  190. return &enum_value;
  191. }
  192. }
  193. }
  194. return nullptr;
  195. }
  196. const google::protobuf::EnumValue* FindEnumValueByNameWithoutUnderscoreOrNull(
  197. const google::protobuf::Enum* enum_type, StringPiece enum_name) {
  198. if (enum_type != nullptr) {
  199. for (int i = 0; i < enum_type->enumvalue_size(); ++i) {
  200. const google::protobuf::EnumValue& enum_value = enum_type->enumvalue(i);
  201. std::string enum_name_without_underscore = enum_value.name();
  202. // Remove underscore from the name.
  203. enum_name_without_underscore.erase(
  204. std::remove(enum_name_without_underscore.begin(),
  205. enum_name_without_underscore.end(), '_'),
  206. enum_name_without_underscore.end());
  207. // Make the name uppercase.
  208. for (std::string::iterator it = enum_name_without_underscore.begin();
  209. it != enum_name_without_underscore.end(); ++it) {
  210. *it = ascii_toupper(*it);
  211. }
  212. if (enum_name_without_underscore == enum_name) {
  213. return &enum_value;
  214. }
  215. }
  216. }
  217. return nullptr;
  218. }
  219. std::string EnumValueNameToLowerCamelCase(StringPiece input) {
  220. std::string input_string(input);
  221. std::transform(input_string.begin(), input_string.end(), input_string.begin(),
  222. ::tolower);
  223. return ToCamelCase(input_string);
  224. }
  225. std::string ToCamelCase(StringPiece input) {
  226. bool capitalize_next = false;
  227. bool was_cap = true;
  228. bool is_cap = false;
  229. bool first_word = true;
  230. std::string result;
  231. result.reserve(input.size());
  232. for (size_t i = 0; i < input.size(); ++i, was_cap = is_cap) {
  233. is_cap = ascii_isupper(input[i]);
  234. if (input[i] == '_') {
  235. capitalize_next = true;
  236. if (!result.empty()) first_word = false;
  237. continue;
  238. } else if (first_word) {
  239. // Consider when the current character B is capitalized,
  240. // first word ends when:
  241. // 1) following a lowercase: "...aB..."
  242. // 2) followed by a lowercase: "...ABc..."
  243. if (!result.empty() && is_cap &&
  244. (!was_cap || (i + 1 < input.size() && ascii_islower(input[i + 1])))) {
  245. first_word = false;
  246. result.push_back(input[i]);
  247. } else {
  248. result.push_back(ascii_tolower(input[i]));
  249. continue;
  250. }
  251. } else if (capitalize_next) {
  252. capitalize_next = false;
  253. if (ascii_islower(input[i])) {
  254. result.push_back(ascii_toupper(input[i]));
  255. continue;
  256. } else {
  257. result.push_back(input[i]);
  258. continue;
  259. }
  260. } else {
  261. result.push_back(ascii_tolower(input[i]));
  262. }
  263. }
  264. return result;
  265. }
  266. std::string ToSnakeCase(StringPiece input) {
  267. bool was_not_underscore = false; // Initialize to false for case 1 (below)
  268. bool was_not_cap = false;
  269. std::string result;
  270. result.reserve(input.size() << 1);
  271. for (size_t i = 0; i < input.size(); ++i) {
  272. if (ascii_isupper(input[i])) {
  273. // Consider when the current character B is capitalized:
  274. // 1) At beginning of input: "B..." => "b..."
  275. // (e.g. "Biscuit" => "biscuit")
  276. // 2) Following a lowercase: "...aB..." => "...a_b..."
  277. // (e.g. "gBike" => "g_bike")
  278. // 3) At the end of input: "...AB" => "...ab"
  279. // (e.g. "GoogleLAB" => "google_lab")
  280. // 4) Followed by a lowercase: "...ABc..." => "...a_bc..."
  281. // (e.g. "GBike" => "g_bike")
  282. if (was_not_underscore && // case 1 out
  283. (was_not_cap || // case 2 in, case 3 out
  284. (i + 1 < input.size() && // case 3 out
  285. ascii_islower(input[i + 1])))) { // case 4 in
  286. // We add an underscore for case 2 and case 4.
  287. result.push_back('_');
  288. }
  289. result.push_back(ascii_tolower(input[i]));
  290. was_not_underscore = true;
  291. was_not_cap = false;
  292. } else {
  293. result.push_back(input[i]);
  294. was_not_underscore = input[i] != '_';
  295. was_not_cap = true;
  296. }
  297. }
  298. return result;
  299. }
  300. std::set<std::string>* well_known_types_ = NULL;
  301. PROTOBUF_NAMESPACE_ID::internal::once_flag well_known_types_init_;
  302. const char* well_known_types_name_array_[] = {
  303. "google.protobuf.Timestamp", "google.protobuf.Duration",
  304. "google.protobuf.DoubleValue", "google.protobuf.FloatValue",
  305. "google.protobuf.Int64Value", "google.protobuf.UInt64Value",
  306. "google.protobuf.Int32Value", "google.protobuf.UInt32Value",
  307. "google.protobuf.BoolValue", "google.protobuf.StringValue",
  308. "google.protobuf.BytesValue", "google.protobuf.FieldMask"};
  309. void DeleteWellKnownTypes() { delete well_known_types_; }
  310. void InitWellKnownTypes() {
  311. well_known_types_ = new std::set<std::string>;
  312. for (int i = 0; i < GOOGLE_ARRAYSIZE(well_known_types_name_array_); ++i) {
  313. well_known_types_->insert(well_known_types_name_array_[i]);
  314. }
  315. google::protobuf::internal::OnShutdown(&DeleteWellKnownTypes);
  316. }
  317. bool IsWellKnownType(const std::string& type_name) {
  318. PROTOBUF_NAMESPACE_ID::internal::call_once(well_known_types_init_,
  319. InitWellKnownTypes);
  320. return ContainsKey(*well_known_types_, type_name);
  321. }
  322. bool IsValidBoolString(StringPiece bool_string) {
  323. return bool_string == "true" || bool_string == "false" ||
  324. bool_string == "1" || bool_string == "0";
  325. }
  326. bool IsMap(const google::protobuf::Field& field,
  327. const google::protobuf::Type& type) {
  328. return field.cardinality() == google::protobuf::Field::CARDINALITY_REPEATED &&
  329. (GetBoolOptionOrDefault(type.options(), "map_entry", false) ||
  330. GetBoolOptionOrDefault(type.options(),
  331. "google.protobuf.MessageOptions.map_entry",
  332. false));
  333. }
  334. bool IsMessageSetWireFormat(const google::protobuf::Type& type) {
  335. return GetBoolOptionOrDefault(type.options(), "message_set_wire_format",
  336. false) ||
  337. GetBoolOptionOrDefault(
  338. type.options(),
  339. "google.protobuf.MessageOptions.message_set_wire_format", false);
  340. }
  341. std::string DoubleAsString(double value) {
  342. if (MathLimits<double>::IsPosInf(value)) return "Infinity";
  343. if (MathLimits<double>::IsNegInf(value)) return "-Infinity";
  344. if (MathLimits<double>::IsNaN(value)) return "NaN";
  345. return SimpleDtoa(value);
  346. }
  347. std::string FloatAsString(float value) {
  348. if (MathLimits<float>::IsFinite(value)) return SimpleFtoa(value);
  349. return DoubleAsString(value);
  350. }
  351. bool SafeStrToFloat(StringPiece str, float* value) {
  352. double double_value;
  353. if (!safe_strtod(str, &double_value)) {
  354. return false;
  355. }
  356. if (MathLimits<double>::IsInf(double_value) ||
  357. MathLimits<double>::IsNaN(double_value))
  358. return false;
  359. // Fail if the value is not representable in float.
  360. if (double_value > std::numeric_limits<float>::max() ||
  361. double_value < -std::numeric_limits<float>::max()) {
  362. return false;
  363. }
  364. *value = static_cast<float>(double_value);
  365. return true;
  366. }
  367. } // namespace converter
  368. } // namespace util
  369. } // namespace protobuf
  370. } // namespace google