protobuf 2.8.0.beta9 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/{UPGRADING.md → CHANGES.md} +20 -12
  4. data/LICENSE.txt +14 -0
  5. data/README.md +60 -74
  6. data/bin/protoc-gen-ruby +26 -0
  7. data/bin/rprotoc +4 -58
  8. data/lib/protobuf.rb +15 -13
  9. data/lib/protobuf/code_generator.rb +41 -0
  10. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
  11. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
  12. data/lib/protobuf/field/base_field.rb +0 -1
  13. data/lib/protobuf/field/bytes_field.rb +1 -1
  14. data/lib/protobuf/field/message_field.rb +2 -4
  15. data/lib/protobuf/field/varint_field.rb +4 -7
  16. data/lib/protobuf/generators/base.rb +71 -0
  17. data/lib/protobuf/generators/enum_generator.rb +31 -0
  18. data/lib/protobuf/generators/extension_generator.rb +28 -0
  19. data/lib/protobuf/generators/field_generator.rb +132 -0
  20. data/lib/protobuf/generators/file_generator.rb +140 -0
  21. data/lib/protobuf/generators/group_generator.rb +113 -0
  22. data/lib/protobuf/generators/message_generator.rb +99 -0
  23. data/lib/protobuf/generators/printable.rb +161 -0
  24. data/lib/protobuf/generators/service_generator.rb +27 -0
  25. data/lib/protobuf/lifecycle.rb +7 -3
  26. data/lib/protobuf/message.rb +51 -34
  27. data/lib/protobuf/rpc/connectors/common.rb +4 -6
  28. data/lib/protobuf/rpc/server.rb +1 -1
  29. data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
  31. data/lib/protobuf/rpc/service.rb +3 -3
  32. data/lib/protobuf/rpc/service_directory.rb +24 -12
  33. data/lib/protobuf/version.rb +1 -1
  34. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  35. data/proto/google/protobuf/descriptor.proto +620 -0
  36. data/protobuf.gemspec +12 -21
  37. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  38. data/spec/data/data.bin +3 -0
  39. data/{test/data/unk.png → spec/data/types.bin} +0 -0
  40. data/spec/encoding/all_types_spec.rb +91 -0
  41. data/spec/encoding/extreme_values_spec.rb +0 -0
  42. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  43. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  44. data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
  45. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  46. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  47. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  48. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  49. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  50. data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
  51. data/spec/lib/protobuf/logger_spec.rb +5 -0
  52. data/spec/lib/protobuf/message_spec.rb +22 -8
  53. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
  54. data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
  55. data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
  56. data/spec/spec_helper.rb +15 -16
  57. data/spec/support/test/all_types.data.bin +0 -0
  58. data/spec/support/test/all_types.data.txt +119 -0
  59. data/spec/support/test/defaults.pb.rb +25 -0
  60. data/spec/support/test/defaults.proto +9 -0
  61. data/spec/support/test/enum.pb.rb +10 -8
  62. data/spec/support/test/extended.pb.rb +5 -3
  63. data/spec/support/test/extreme_values.data.bin +0 -0
  64. data/spec/support/test/google_unittest.pb.rb +543 -0
  65. data/spec/support/test/google_unittest.proto +713 -0
  66. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  67. data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
  68. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  69. data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
  70. data/spec/support/test/multi_field_extensions.pb.rb +29 -7
  71. data/spec/support/test/multi_field_extensions.proto +12 -2
  72. data/spec/support/test/resource.pb.rb +20 -16
  73. metadata +149 -236
  74. data/examples/addressbook.pb.rb +0 -55
  75. data/examples/addressbook.proto +0 -24
  76. data/examples/reading_a_message.rb +0 -32
  77. data/examples/writing_a_message.rb +0 -46
  78. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
  79. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
  80. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
  81. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
  82. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
  83. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
  84. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
  85. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
  86. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
  87. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
  88. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
  89. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
  90. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
  91. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
  92. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
  93. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
  94. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
  95. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
  96. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
  97. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
  98. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
  99. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
  100. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
  101. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
  102. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
  103. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
  104. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
  105. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
  106. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
  107. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
  108. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
  109. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
  110. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
  111. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
  112. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
  113. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
  114. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
  115. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
  116. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
  117. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
  118. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
  119. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
  120. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
  121. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
  122. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
  123. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
  124. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
  125. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
  126. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
  127. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
  128. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
  129. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
  130. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
  131. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
  132. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
  133. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
  134. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
  135. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
  136. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
  137. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
  138. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
  139. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
  140. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
  141. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
  142. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
  143. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
  144. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
  145. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
  146. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
  147. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
  148. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
  149. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
  150. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
  151. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
  152. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
  153. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
  154. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
  155. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
  156. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
  157. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
  158. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
  159. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
  160. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
  161. data/ext/ruby_generator/Makefile +0 -10
  162. data/ext/ruby_generator/RubyGenerator.cpp +0 -544
  163. data/ext/ruby_generator/RubyGenerator.h +0 -206
  164. data/ext/ruby_generator/extconf.rb +0 -35
  165. data/test/data/data_source.py +0 -14
  166. data/test/data/types_source.py +0 -22
  167. data/test/proto/addressbook.pb.rb +0 -66
  168. data/test/proto/addressbook.proto +0 -33
  169. data/test/proto/addressbook_base.pb.rb +0 -58
  170. data/test/proto/addressbook_base.proto +0 -26
  171. data/test/proto/addressbook_ext.pb.rb +0 -20
  172. data/test/proto/addressbook_ext.proto +0 -6
  173. data/test/proto/collision.pb.rb +0 -17
  174. data/test/proto/collision.proto +0 -5
  175. data/test/proto/ext_collision.pb.rb +0 -24
  176. data/test/proto/ext_collision.proto +0 -8
  177. data/test/proto/ext_range.pb.rb +0 -22
  178. data/test/proto/ext_range.proto +0 -7
  179. data/test/proto/float_default.proto +0 -10
  180. data/test/proto/lowercase.pb.rb +0 -30
  181. data/test/proto/lowercase.proto +0 -9
  182. data/test/proto/merge.pb.rb +0 -39
  183. data/test/proto/merge.proto +0 -15
  184. data/test/proto/nested.pb.rb +0 -30
  185. data/test/proto/nested.proto +0 -9
  186. data/test/proto/optional_field.pb.rb +0 -35
  187. data/test/proto/optional_field.proto +0 -12
  188. data/test/proto/packed.pb.rb +0 -22
  189. data/test/proto/packed.proto +0 -6
  190. data/test/proto/rpc.proto +0 -6
  191. data/test/proto/types.pb.rb +0 -84
  192. data/test/proto/types.proto +0 -37
  193. data/test/test_addressbook.rb +0 -56
  194. data/test/test_enum_value.rb +0 -41
  195. data/test/test_extension.rb +0 -36
  196. data/test/test_lowercase.rb +0 -11
  197. data/test/test_message.rb +0 -128
  198. data/test/test_optional_field.rb +0 -103
  199. data/test/test_packed_field.rb +0 -40
  200. data/test/test_parse.rb +0 -15
  201. data/test/test_repeated_types.rb +0 -132
  202. data/test/test_serialize.rb +0 -61
  203. data/test/test_standard_message.rb +0 -96
  204. data/test/test_types.rb +0 -226
@@ -1,121 +0,0 @@
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__
@@ -1,457 +0,0 @@
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
-