protobuf 1.4.2 → 2.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. data/.gitignore +4 -0
  2. data/Rakefile +7 -7
  3. data/bin/rpc_server +2 -188
  4. data/bin/rprotoc +44 -38
  5. data/examples/addressbook.pb.rb +2 -2
  6. data/ext/Makefile +11 -0
  7. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
  8. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
  9. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
  10. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
  11. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
  12. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
  13. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
  14. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
  15. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
  16. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
  17. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
  18. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
  19. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
  20. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
  21. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
  22. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
  23. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
  24. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
  25. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
  26. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
  27. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
  28. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
  29. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
  30. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
  31. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
  32. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
  33. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
  34. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
  35. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
  36. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
  37. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
  38. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
  39. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
  40. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
  41. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
  42. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
  43. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
  44. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
  45. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
  46. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
  47. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
  48. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
  49. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
  50. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
  51. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
  52. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
  53. data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
  54. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
  55. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
  56. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
  57. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
  58. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
  59. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
  60. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
  61. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
  62. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
  63. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
  64. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
  65. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
  66. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
  67. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
  68. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
  69. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
  70. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
  71. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
  72. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
  73. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
  74. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
  75. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
  76. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
  77. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
  78. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
  79. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
  80. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
  81. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
  82. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
  83. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
  84. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
  85. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
  86. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
  87. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
  88. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
  89. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
  90. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
  91. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
  92. data/ext/ruby_generator/RubyGenerator.cpp +435 -0
  93. data/ext/ruby_generator/RubyGenerator.h +180 -0
  94. data/ext/ruby_generator/extconf.rb +24 -0
  95. data/lib/protobuf.rb +55 -33
  96. data/lib/protobuf/cli.rb +176 -0
  97. data/lib/protobuf/common/logger.rb +11 -11
  98. data/lib/protobuf/{message/enum.rb → enum.rb} +0 -10
  99. data/lib/protobuf/evented.rb +22 -0
  100. data/lib/protobuf/field.rb +57 -0
  101. data/lib/protobuf/field/base_field.rb +210 -0
  102. data/lib/protobuf/field/bool_field.rb +24 -0
  103. data/lib/protobuf/field/bytes_field.rb +38 -0
  104. data/lib/protobuf/field/double_field.rb +19 -0
  105. data/lib/protobuf/field/enum_field.rb +58 -0
  106. data/lib/protobuf/field/extension_fields.rb +24 -0
  107. data/lib/protobuf/field/field_array.rb +55 -0
  108. data/lib/protobuf/field/fixed32_field.rb +19 -0
  109. data/lib/protobuf/field/fixed64_field.rb +22 -0
  110. data/lib/protobuf/field/float_field.rb +29 -0
  111. data/lib/protobuf/field/int32_field.rb +10 -0
  112. data/lib/protobuf/field/int64_field.rb +10 -0
  113. data/lib/protobuf/field/integer_field.rb +19 -0
  114. data/lib/protobuf/field/message_field.rb +51 -0
  115. data/lib/protobuf/field/sfixed32_field.rb +21 -0
  116. data/lib/protobuf/field/sfixed64_field.rb +24 -0
  117. data/lib/protobuf/field/signed_integer_field.rb +23 -0
  118. data/lib/protobuf/field/sint32_field.rb +10 -0
  119. data/lib/protobuf/field/sint64_field.rb +10 -0
  120. data/lib/protobuf/field/string_field.rb +14 -0
  121. data/lib/protobuf/field/uint32_field.rb +10 -0
  122. data/lib/protobuf/field/uint64_field.rb +10 -0
  123. data/lib/protobuf/field/varint_field.rb +63 -0
  124. data/lib/protobuf/message.rb +376 -0
  125. data/lib/protobuf/message/message.rb +1 -475
  126. data/lib/protobuf/rpc/buffer.rb +1 -1
  127. data/lib/protobuf/rpc/client.rb +26 -26
  128. data/lib/protobuf/rpc/connector.rb +15 -14
  129. data/lib/protobuf/rpc/connectors/eventmachine.rb +13 -13
  130. data/lib/protobuf/rpc/rpc.pb.rb +39 -28
  131. data/lib/protobuf/rpc/server.rb +7 -28
  132. data/lib/protobuf/rpc/servers/evented_runner.rb +15 -15
  133. data/lib/protobuf/rpc/servers/socket/server.rb +2 -1
  134. data/lib/protobuf/rpc/servers/socket_runner.rb +5 -6
  135. data/lib/protobuf/rpc/servers/zmq_runner.rb +4 -5
  136. data/lib/protobuf/socket.rb +22 -0
  137. data/lib/protobuf/version.rb +2 -1
  138. data/lib/protobuf/zmq.rb +21 -0
  139. data/proto/rpc.pb.rb +48 -0
  140. data/protobuf.gemspec +14 -6
  141. data/spec/benchmark/tasks.rb +42 -23
  142. data/spec/functional/evented_server_spec.rb +2 -0
  143. data/spec/functional/socket_server_spec.rb +22 -26
  144. data/spec/functional/zmq_server_spec.rb +22 -26
  145. data/spec/lib/protobuf/cli_spec.rb +182 -0
  146. data/spec/{unit → lib/protobuf}/common/logger_spec.rb +0 -0
  147. data/spec/{unit → lib/protobuf/message}/enum_spec.rb +0 -0
  148. data/spec/{unit → lib/protobuf/message}/message_spec.rb +0 -0
  149. data/spec/{unit → lib/protobuf}/rpc/client_spec.rb +6 -4
  150. data/spec/lib/protobuf/rpc/connector_spec.rb +41 -0
  151. data/spec/{unit → lib/protobuf}/rpc/connectors/base_spec.rb +18 -18
  152. data/spec/{unit → lib/protobuf}/rpc/connectors/common_spec.rb +20 -20
  153. data/spec/{unit → lib/protobuf}/rpc/connectors/socket_spec.rb +9 -8
  154. data/spec/{unit → lib/protobuf}/rpc/connectors/zmq_spec.rb +1 -1
  155. data/spec/{unit → lib/protobuf}/rpc/servers/evented_server_spec.rb +4 -4
  156. data/spec/{unit → lib/protobuf}/rpc/servers/socket_server_spec.rb +11 -23
  157. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/broker_spec.rb +5 -0
  158. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/server_spec.rb +5 -0
  159. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/util_spec.rb +5 -0
  160. data/spec/{unit → lib/protobuf}/rpc/servers/zmq/worker_spec.rb +5 -0
  161. data/spec/{unit → lib/protobuf}/rpc/service_spec.rb +0 -0
  162. data/spec/proto/test.pb.rb +48 -25
  163. data/spec/spec_helper.rb +9 -15
  164. data/spec/support/all.rb +7 -0
  165. data/spec/{helper → support}/server.rb +5 -7
  166. data/spec/{helper → support}/silent_constants.rb +4 -0
  167. data/spec/support/test_app_file.rb +2 -0
  168. data/spec/{helper → support}/tolerance_matcher.rb +0 -0
  169. data/test/proto/types.pb.rb +1 -1
  170. data/test/test_addressbook.rb +1 -1
  171. data/test/test_enum_value.rb +1 -1
  172. data/test/test_standard_message.rb +1 -1
  173. metadata +243 -107
  174. data/lib/protobuf/common/util.rb +0 -59
  175. data/lib/protobuf/compiler/compiler.rb +0 -52
  176. data/lib/protobuf/compiler/nodes.rb +0 -323
  177. data/lib/protobuf/compiler/proto.y +0 -216
  178. data/lib/protobuf/compiler/proto2.ebnf +0 -79
  179. data/lib/protobuf/compiler/proto_parser.rb +0 -1425
  180. data/lib/protobuf/compiler/template/rpc_service_implementation.erb +0 -42
  181. data/lib/protobuf/compiler/visitors.rb +0 -282
  182. data/lib/protobuf/descriptor/descriptor.proto +0 -286
  183. data/lib/protobuf/descriptor/descriptor.rb +0 -55
  184. data/lib/protobuf/descriptor/descriptor_builder.rb +0 -143
  185. data/lib/protobuf/descriptor/descriptor_proto.rb +0 -138
  186. data/lib/protobuf/descriptor/enum_descriptor.rb +0 -33
  187. data/lib/protobuf/descriptor/field_descriptor.rb +0 -49
  188. data/lib/protobuf/descriptor/file_descriptor.rb +0 -37
  189. data/lib/protobuf/message/field.rb +0 -684
  190. data/lib/protobuf/message/protoable.rb +0 -38
  191. data/spec/helper/all.rb +0 -7
  192. data/spec/unit/common/util_spec.rb +0 -17
  193. data/spec/unit/rpc/connector_spec.rb +0 -31
  194. data/spec/unit/rpc/connectors/eventmachine_client_spec.rb +0 -32
  195. data/spec/unit/rpc/connectors/eventmachine_spec.rb +0 -0
  196. data/test/check_unbuild.rb +0 -30
  197. data/test/test_compiler.rb +0 -325
  198. data/test/test_descriptor.rb +0 -122
@@ -0,0 +1,121 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ // http://code.google.com/p/protobuf/
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
+
31
+ // from google3/util/gtl/stl_util-inl.h
32
+
33
+ #ifndef GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
34
+ #define GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
35
+
36
+ #include <google/protobuf/stubs/common.h>
37
+
38
+ namespace google {
39
+ namespace protobuf {
40
+
41
+ // STLDeleteContainerPointers()
42
+ // For a range within a container of pointers, calls delete
43
+ // (non-array version) on these pointers.
44
+ // NOTE: for these three functions, we could just implement a DeleteObject
45
+ // functor and then call for_each() on the range and functor, but this
46
+ // requires us to pull in all of algorithm.h, which seems expensive.
47
+ // For hash_[multi]set, it is important that this deletes behind the iterator
48
+ // because the hash_set may call the hash function on the iterator when it is
49
+ // advanced, which could result in the hash function trying to deference a
50
+ // stale pointer.
51
+ template <class ForwardIterator>
52
+ void STLDeleteContainerPointers(ForwardIterator begin,
53
+ ForwardIterator end) {
54
+ while (begin != end) {
55
+ ForwardIterator temp = begin;
56
+ ++begin;
57
+ delete *temp;
58
+ }
59
+ }
60
+
61
+ // Inside Google, this function implements a horrible, disgusting hack in which
62
+ // we reach into the string's private implementation and resize it without
63
+ // initializing the new bytes. In some cases doing this can significantly
64
+ // improve performance. However, since it's totally non-portable it has no
65
+ // place in open source code. Feel free to fill this function in with your
66
+ // own disgusting hack if you want the perf boost.
67
+ inline void STLStringResizeUninitialized(string* s, size_t new_size) {
68
+ s->resize(new_size);
69
+ }
70
+
71
+ // Return a mutable char* pointing to a string's internal buffer,
72
+ // which may not be null-terminated. Writing through this pointer will
73
+ // modify the string.
74
+ //
75
+ // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
76
+ // next call to a string method that invalidates iterators.
77
+ //
78
+ // As of 2006-04, there is no standard-blessed way of getting a
79
+ // mutable reference to a string's internal buffer. However, issue 530
80
+ // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
81
+ // proposes this as the method. According to Matt Austern, this should
82
+ // already work on all current implementations.
83
+ inline char* string_as_array(string* str) {
84
+ // DO NOT USE const_cast<char*>(str->data())! See the unittest for why.
85
+ return str->empty() ? NULL : &*str->begin();
86
+ }
87
+
88
+ // STLDeleteElements() deletes all the elements in an STL container and clears
89
+ // the container. This function is suitable for use with a vector, set,
90
+ // hash_set, or any other STL container which defines sensible begin(), end(),
91
+ // and clear() methods.
92
+ //
93
+ // If container is NULL, this function is a no-op.
94
+ //
95
+ // As an alternative to calling STLDeleteElements() directly, consider
96
+ // ElementDeleter (defined below), which ensures that your container's elements
97
+ // are deleted when the ElementDeleter goes out of scope.
98
+ template <class T>
99
+ void STLDeleteElements(T *container) {
100
+ if (!container) return;
101
+ STLDeleteContainerPointers(container->begin(), container->end());
102
+ container->clear();
103
+ }
104
+
105
+ // Given an STL container consisting of (key, value) pairs, STLDeleteValues
106
+ // deletes all the "value" components and clears the container. Does nothing
107
+ // in the case it's given a NULL pointer.
108
+
109
+ template <class T>
110
+ void STLDeleteValues(T *v) {
111
+ if (!v) return;
112
+ for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
113
+ delete i->second;
114
+ }
115
+ v->clear();
116
+ }
117
+
118
+ } // namespace protobuf
119
+ } // namespace google
120
+
121
+ #endif // GOOGLE_PROTOBUF_STUBS_STL_UTIL_INL_H__
@@ -0,0 +1,457 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ // http://code.google.com/p/protobuf/
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
+
31
+ // from google3/strings/strutil.h
32
+
33
+ #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
34
+ #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
35
+
36
+ #include <stdlib.h>
37
+ #include <vector>
38
+ #include <google/protobuf/stubs/common.h>
39
+
40
+ namespace google {
41
+ namespace protobuf {
42
+
43
+ #ifdef _MSC_VER
44
+ #define strtoll _strtoi64
45
+ #define strtoull _strtoui64
46
+ #elif defined(__DECCXX) && defined(__osf__)
47
+ // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
48
+ #define strtoll strtol
49
+ #define strtoull strtoul
50
+ #endif
51
+
52
+ // ----------------------------------------------------------------------
53
+ // ascii_isalnum()
54
+ // Check if an ASCII character is alphanumeric. We can't use ctype's
55
+ // isalnum() because it is affected by locale. This function is applied
56
+ // to identifiers in the protocol buffer language, not to natural-language
57
+ // strings, so locale should not be taken into account.
58
+ // ascii_isdigit()
59
+ // Like above, but only accepts digits.
60
+ // ----------------------------------------------------------------------
61
+
62
+ inline bool ascii_isalnum(char c) {
63
+ return ('a' <= c && c <= 'z') ||
64
+ ('A' <= c && c <= 'Z') ||
65
+ ('0' <= c && c <= '9');
66
+ }
67
+
68
+ inline bool ascii_isdigit(char c) {
69
+ return ('0' <= c && c <= '9');
70
+ }
71
+
72
+ // ----------------------------------------------------------------------
73
+ // HasPrefixString()
74
+ // Check if a string begins with a given prefix.
75
+ // StripPrefixString()
76
+ // Given a string and a putative prefix, returns the string minus the
77
+ // prefix string if the prefix matches, otherwise the original
78
+ // string.
79
+ // ----------------------------------------------------------------------
80
+ inline bool HasPrefixString(const string& str,
81
+ const string& prefix) {
82
+ return str.size() >= prefix.size() &&
83
+ str.compare(0, prefix.size(), prefix) == 0;
84
+ }
85
+
86
+ inline string StripPrefixString(const string& str, const string& prefix) {
87
+ if (HasPrefixString(str, prefix)) {
88
+ return str.substr(prefix.size());
89
+ } else {
90
+ return str;
91
+ }
92
+ }
93
+
94
+ // ----------------------------------------------------------------------
95
+ // HasSuffixString()
96
+ // Return true if str ends in suffix.
97
+ // StripSuffixString()
98
+ // Given a string and a putative suffix, returns the string minus the
99
+ // suffix string if the suffix matches, otherwise the original
100
+ // string.
101
+ // ----------------------------------------------------------------------
102
+ inline bool HasSuffixString(const string& str,
103
+ const string& suffix) {
104
+ return str.size() >= suffix.size() &&
105
+ str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
106
+ }
107
+
108
+ inline string StripSuffixString(const string& str, const string& suffix) {
109
+ if (HasSuffixString(str, suffix)) {
110
+ return str.substr(0, str.size() - suffix.size());
111
+ } else {
112
+ return str;
113
+ }
114
+ }
115
+
116
+ // ----------------------------------------------------------------------
117
+ // StripString
118
+ // Replaces any occurrence of the character 'remove' (or the characters
119
+ // in 'remove') with the character 'replacewith'.
120
+ // Good for keeping html characters or protocol characters (\t) out
121
+ // of places where they might cause a problem.
122
+ // ----------------------------------------------------------------------
123
+ LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
124
+ char replacewith);
125
+
126
+ // ----------------------------------------------------------------------
127
+ // LowerString()
128
+ // UpperString()
129
+ // Convert the characters in "s" to lowercase or uppercase. ASCII-only:
130
+ // these functions intentionally ignore locale because they are applied to
131
+ // identifiers used in the Protocol Buffer language, not to natural-language
132
+ // strings.
133
+ // ----------------------------------------------------------------------
134
+
135
+ inline void LowerString(string * s) {
136
+ string::iterator end = s->end();
137
+ for (string::iterator i = s->begin(); i != end; ++i) {
138
+ // tolower() changes based on locale. We don't want this!
139
+ if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
140
+ }
141
+ }
142
+
143
+ inline void UpperString(string * s) {
144
+ string::iterator end = s->end();
145
+ for (string::iterator i = s->begin(); i != end; ++i) {
146
+ // toupper() changes based on locale. We don't want this!
147
+ if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
148
+ }
149
+ }
150
+
151
+ // ----------------------------------------------------------------------
152
+ // StringReplace()
153
+ // Give me a string and two patterns "old" and "new", and I replace
154
+ // the first instance of "old" in the string with "new", if it
155
+ // exists. RETURN a new string, regardless of whether the replacement
156
+ // happened or not.
157
+ // ----------------------------------------------------------------------
158
+
159
+ LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
160
+ const string& newsub, bool replace_all);
161
+
162
+ // ----------------------------------------------------------------------
163
+ // SplitStringUsing()
164
+ // Split a string using a character delimiter. Append the components
165
+ // to 'result'. If there are consecutive delimiters, this function skips
166
+ // over all of them.
167
+ // ----------------------------------------------------------------------
168
+ LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
169
+ vector<string>* res);
170
+
171
+ // ----------------------------------------------------------------------
172
+ // JoinStrings()
173
+ // These methods concatenate a vector of strings into a C++ string, using
174
+ // the C-string "delim" as a separator between components. There are two
175
+ // flavors of the function, one flavor returns the concatenated string,
176
+ // another takes a pointer to the target string. In the latter case the
177
+ // target string is cleared and overwritten.
178
+ // ----------------------------------------------------------------------
179
+ LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
180
+ const char* delim, string* result);
181
+
182
+ inline string JoinStrings(const vector<string>& components,
183
+ const char* delim) {
184
+ string result;
185
+ JoinStrings(components, delim, &result);
186
+ return result;
187
+ }
188
+
189
+ // ----------------------------------------------------------------------
190
+ // UnescapeCEscapeSequences()
191
+ // Copies "source" to "dest", rewriting C-style escape sequences
192
+ // -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
193
+ // equivalents. "dest" must be sufficiently large to hold all
194
+ // the characters in the rewritten string (i.e. at least as large
195
+ // as strlen(source) + 1 should be safe, since the replacements
196
+ // are always shorter than the original escaped sequences). It's
197
+ // safe for source and dest to be the same. RETURNS the length
198
+ // of dest.
199
+ //
200
+ // It allows hex sequences \xhh, or generally \xhhhhh with an
201
+ // arbitrary number of hex digits, but all of them together must
202
+ // specify a value of a single byte (e.g. \x0045 is equivalent
203
+ // to \x45, and \x1234 is erroneous).
204
+ //
205
+ // It also allows escape sequences of the form \uhhhh (exactly four
206
+ // hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
207
+ // hex digits, upper or lower case) to specify a Unicode code
208
+ // point. The dest array will contain the UTF8-encoded version of
209
+ // that code-point (e.g., if source contains \u2019, then dest will
210
+ // contain the three bytes 0xE2, 0x80, and 0x99).
211
+ //
212
+ // Errors: In the first form of the call, errors are reported with
213
+ // LOG(ERROR). The same is true for the second form of the call if
214
+ // the pointer to the string vector is NULL; otherwise, error
215
+ // messages are stored in the vector. In either case, the effect on
216
+ // the dest array is not defined, but rest of the source will be
217
+ // processed.
218
+ // ----------------------------------------------------------------------
219
+
220
+ LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
221
+ LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
222
+ vector<string> *errors);
223
+
224
+ // ----------------------------------------------------------------------
225
+ // UnescapeCEscapeString()
226
+ // This does the same thing as UnescapeCEscapeSequences, but creates
227
+ // a new string. The caller does not need to worry about allocating
228
+ // a dest buffer. This should be used for non performance critical
229
+ // tasks such as printing debug messages. It is safe for src and dest
230
+ // to be the same.
231
+ //
232
+ // The second call stores its errors in a supplied string vector.
233
+ // If the string vector pointer is NULL, it reports the errors with LOG().
234
+ //
235
+ // In the first and second calls, the length of dest is returned. In the
236
+ // the third call, the new string is returned.
237
+ // ----------------------------------------------------------------------
238
+
239
+ LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
240
+ LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
241
+ vector<string> *errors);
242
+ LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
243
+
244
+ // ----------------------------------------------------------------------
245
+ // CEscapeString()
246
+ // Copies 'src' to 'dest', escaping dangerous characters using
247
+ // C-style escape sequences. This is very useful for preparing query
248
+ // flags. 'src' and 'dest' should not overlap.
249
+ // Returns the number of bytes written to 'dest' (not including the \0)
250
+ // or -1 if there was insufficient space.
251
+ //
252
+ // Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped.
253
+ // ----------------------------------------------------------------------
254
+ LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
255
+ char* dest, int dest_len);
256
+
257
+ // ----------------------------------------------------------------------
258
+ // CEscape()
259
+ // More convenient form of CEscapeString: returns result as a "string".
260
+ // This version is slower than CEscapeString() because it does more
261
+ // allocation. However, it is much more convenient to use in
262
+ // non-speed-critical code like logging messages etc.
263
+ // ----------------------------------------------------------------------
264
+ LIBPROTOBUF_EXPORT string CEscape(const string& src);
265
+
266
+ namespace strings {
267
+ // Like CEscape() but does not escape bytes with the upper bit set.
268
+ LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
269
+
270
+ // Like CEscape() but uses hex (\x) escapes instead of octals.
271
+ LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
272
+ } // namespace strings
273
+
274
+ // ----------------------------------------------------------------------
275
+ // strto32()
276
+ // strtou32()
277
+ // strto64()
278
+ // strtou64()
279
+ // Architecture-neutral plug compatible replacements for strtol() and
280
+ // strtoul(). Long's have different lengths on ILP-32 and LP-64
281
+ // platforms, so using these is safer, from the point of view of
282
+ // overflow behavior, than using the standard libc functions.
283
+ // ----------------------------------------------------------------------
284
+ LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr,
285
+ int base);
286
+ LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr,
287
+ int base);
288
+
289
+ inline int32 strto32(const char *nptr, char **endptr, int base) {
290
+ if (sizeof(int32) == sizeof(long))
291
+ return strtol(nptr, endptr, base);
292
+ else
293
+ return strto32_adaptor(nptr, endptr, base);
294
+ }
295
+
296
+ inline uint32 strtou32(const char *nptr, char **endptr, int base) {
297
+ if (sizeof(uint32) == sizeof(unsigned long))
298
+ return strtoul(nptr, endptr, base);
299
+ else
300
+ return strtou32_adaptor(nptr, endptr, base);
301
+ }
302
+
303
+ // For now, long long is 64-bit on all the platforms we care about, so these
304
+ // functions can simply pass the call to strto[u]ll.
305
+ inline int64 strto64(const char *nptr, char **endptr, int base) {
306
+ GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
307
+ sizeof_int64_is_not_sizeof_long_long);
308
+ return strtoll(nptr, endptr, base);
309
+ }
310
+
311
+ inline uint64 strtou64(const char *nptr, char **endptr, int base) {
312
+ GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
313
+ sizeof_uint64_is_not_sizeof_long_long);
314
+ return strtoull(nptr, endptr, base);
315
+ }
316
+
317
+ // ----------------------------------------------------------------------
318
+ // FastIntToBuffer()
319
+ // FastHexToBuffer()
320
+ // FastHex64ToBuffer()
321
+ // FastHex32ToBuffer()
322
+ // FastTimeToBuffer()
323
+ // These are intended for speed. FastIntToBuffer() assumes the
324
+ // integer is non-negative. FastHexToBuffer() puts output in
325
+ // hex rather than decimal. FastTimeToBuffer() puts the output
326
+ // into RFC822 format.
327
+ //
328
+ // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
329
+ // padded to exactly 16 bytes (plus one byte for '\0')
330
+ //
331
+ // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
332
+ // padded to exactly 8 bytes (plus one byte for '\0')
333
+ //
334
+ // All functions take the output buffer as an arg.
335
+ // They all return a pointer to the beginning of the output,
336
+ // which may not be the beginning of the input buffer.
337
+ // ----------------------------------------------------------------------
338
+
339
+ // Suggested buffer size for FastToBuffer functions. Also works with
340
+ // DoubleToBuffer() and FloatToBuffer().
341
+ static const int kFastToBufferSize = 32;
342
+
343
+ LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
344
+ LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
345
+ char* FastUInt32ToBuffer(uint32 i, char* buffer); // inline below
346
+ char* FastUInt64ToBuffer(uint64 i, char* buffer); // inline below
347
+ LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
348
+ LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
349
+ LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
350
+
351
+ // at least 22 bytes long
352
+ inline char* FastIntToBuffer(int i, char* buffer) {
353
+ return (sizeof(i) == 4 ?
354
+ FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
355
+ }
356
+ inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
357
+ return (sizeof(i) == 4 ?
358
+ FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
359
+ }
360
+ inline char* FastLongToBuffer(long i, char* buffer) {
361
+ return (sizeof(i) == 4 ?
362
+ FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
363
+ }
364
+ inline char* FastULongToBuffer(unsigned long i, char* buffer) {
365
+ return (sizeof(i) == 4 ?
366
+ FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
367
+ }
368
+
369
+ // ----------------------------------------------------------------------
370
+ // FastInt32ToBufferLeft()
371
+ // FastUInt32ToBufferLeft()
372
+ // FastInt64ToBufferLeft()
373
+ // FastUInt64ToBufferLeft()
374
+ //
375
+ // Like the Fast*ToBuffer() functions above, these are intended for speed.
376
+ // Unlike the Fast*ToBuffer() functions, however, these functions write
377
+ // their output to the beginning of the buffer (hence the name, as the
378
+ // output is left-aligned). The caller is responsible for ensuring that
379
+ // the buffer has enough space to hold the output.
380
+ //
381
+ // Returns a pointer to the end of the string (i.e. the null character
382
+ // terminating the string).
383
+ // ----------------------------------------------------------------------
384
+
385
+ LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
386
+ LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
387
+ LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
388
+ LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
389
+
390
+ // Just define these in terms of the above.
391
+ inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
392
+ FastUInt32ToBufferLeft(i, buffer);
393
+ return buffer;
394
+ }
395
+ inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
396
+ FastUInt64ToBufferLeft(i, buffer);
397
+ return buffer;
398
+ }
399
+
400
+ // ----------------------------------------------------------------------
401
+ // SimpleItoa()
402
+ // Description: converts an integer to a string.
403
+ //
404
+ // Return value: string
405
+ // ----------------------------------------------------------------------
406
+ LIBPROTOBUF_EXPORT string SimpleItoa(int i);
407
+ LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
408
+ LIBPROTOBUF_EXPORT string SimpleItoa(long i);
409
+ LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
410
+ LIBPROTOBUF_EXPORT string SimpleItoa(long long i);
411
+ LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
412
+
413
+ // ----------------------------------------------------------------------
414
+ // SimpleDtoa()
415
+ // SimpleFtoa()
416
+ // DoubleToBuffer()
417
+ // FloatToBuffer()
418
+ // Description: converts a double or float to a string which, if
419
+ // passed to NoLocaleStrtod(), will produce the exact same original double
420
+ // (except in case of NaN; all NaNs are considered the same value).
421
+ // We try to keep the string short but it's not guaranteed to be as
422
+ // short as possible.
423
+ //
424
+ // DoubleToBuffer() and FloatToBuffer() write the text to the given
425
+ // buffer and return it. The buffer must be at least
426
+ // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
427
+ // bytes for floats. kFastToBufferSize is also guaranteed to be large
428
+ // enough to hold either.
429
+ //
430
+ // Return value: string
431
+ // ----------------------------------------------------------------------
432
+ LIBPROTOBUF_EXPORT string SimpleDtoa(double value);
433
+ LIBPROTOBUF_EXPORT string SimpleFtoa(float value);
434
+
435
+ LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
436
+ LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
437
+
438
+ // In practice, doubles should never need more than 24 bytes and floats
439
+ // should never need more than 14 (including null terminators), but we
440
+ // overestimate to be safe.
441
+ static const int kDoubleToBufferSize = 32;
442
+ static const int kFloatToBufferSize = 24;
443
+
444
+ // ----------------------------------------------------------------------
445
+ // NoLocaleStrtod()
446
+ // Exactly like strtod(), except it always behaves as if in the "C"
447
+ // locale (i.e. decimal points must be '.'s).
448
+ // ----------------------------------------------------------------------
449
+
450
+ LIBPROTOBUF_EXPORT double NoLocaleStrtod(const char* text, char** endptr);
451
+
452
+ } // namespace protobuf
453
+ } // namespace google
454
+
455
+ #endif // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
456
+
457
+