protobuf 2.2.5-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/.yardopts +5 -0
- data/Gemfile +3 -0
- data/README.md +316 -0
- data/Rakefile +29 -0
- data/UPGRADING.md +60 -0
- data/bin/rpc_server +5 -0
- data/bin/rprotoc +62 -0
- data/examples/addressbook.pb.rb +55 -0
- data/examples/addressbook.proto +24 -0
- data/examples/reading_a_message.rb +32 -0
- data/examples/writing_a_message.rb +46 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +142 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +318 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +99 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +85 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +167 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +159 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +103 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +118 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +104 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +2721 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +303 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +84 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +77 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +72 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +213 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +109 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +134 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +120 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +113 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +434 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +73 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +790 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +156 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +108 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +93 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +1367 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +5223 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +366 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +904 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +424 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +82 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +1102 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +207 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h +54 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +136 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +313 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +238 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +357 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +340 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message.h +692 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +239 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +64 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +80 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +1295 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/service.h +291 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +1211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +220 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +119 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +123 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +121 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +457 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +170 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +174 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +101 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +83 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +98 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +285 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +11915 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +2895 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +211 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +56 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +188 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +151 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +4752 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +150 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +816 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +197 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +403 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +268 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +304 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +620 -0
- data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +774 -0
- data/ext/ruby_generator/Makefile +10 -0
- data/ext/ruby_generator/RubyGenerator.cpp +450 -0
- data/ext/ruby_generator/RubyGenerator.h +199 -0
- data/ext/ruby_generator/extconf.rb +36 -0
- data/ext/ruby_generator/protoc-ruby +0 -0
- data/lib/protobuf/cli.rb +188 -0
- data/lib/protobuf/enum.rb +58 -0
- data/lib/protobuf/enum_value.rb +59 -0
- data/lib/protobuf/evented.rb +22 -0
- data/lib/protobuf/exceptions.rb +11 -0
- data/lib/protobuf/ext/eventmachine.rb +14 -0
- data/lib/protobuf/field/base_field.rb +240 -0
- data/lib/protobuf/field/bool_field.rb +36 -0
- data/lib/protobuf/field/bytes_field.rb +38 -0
- data/lib/protobuf/field/double_field.rb +19 -0
- data/lib/protobuf/field/enum_field.rb +50 -0
- data/lib/protobuf/field/extension_fields.rb +32 -0
- data/lib/protobuf/field/field_array.rb +65 -0
- data/lib/protobuf/field/fixed32_field.rb +19 -0
- data/lib/protobuf/field/fixed64_field.rb +22 -0
- data/lib/protobuf/field/float_field.rb +31 -0
- data/lib/protobuf/field/int32_field.rb +12 -0
- data/lib/protobuf/field/int64_field.rb +12 -0
- data/lib/protobuf/field/integer_field.rb +19 -0
- data/lib/protobuf/field/message_field.rb +53 -0
- data/lib/protobuf/field/sfixed32_field.rb +21 -0
- data/lib/protobuf/field/sfixed64_field.rb +24 -0
- data/lib/protobuf/field/signed_integer_field.rb +23 -0
- data/lib/protobuf/field/sint32_field.rb +12 -0
- data/lib/protobuf/field/sint64_field.rb +12 -0
- data/lib/protobuf/field/string_field.rb +14 -0
- data/lib/protobuf/field/uint32_field.rb +12 -0
- data/lib/protobuf/field/uint64_field.rb +12 -0
- data/lib/protobuf/field/varint_field.rb +61 -0
- data/lib/protobuf/field.rb +57 -0
- data/lib/protobuf/logger.rb +86 -0
- data/lib/protobuf/message/decoder.rb +83 -0
- data/lib/protobuf/message/encoder.rb +48 -0
- data/lib/protobuf/message/extend.rb +8 -0
- data/lib/protobuf/message/message.rb +1 -0
- data/lib/protobuf/message.rb +320 -0
- data/lib/protobuf/rpc/buffer.rb +79 -0
- data/lib/protobuf/rpc/client.rb +166 -0
- data/lib/protobuf/rpc/connector.rb +19 -0
- data/lib/protobuf/rpc/connectors/base.rb +38 -0
- data/lib/protobuf/rpc/connectors/common.rb +156 -0
- data/lib/protobuf/rpc/connectors/em_client.rb +84 -0
- data/lib/protobuf/rpc/connectors/eventmachine.rb +87 -0
- data/lib/protobuf/rpc/connectors/socket.rb +73 -0
- data/lib/protobuf/rpc/connectors/zmq.rb +69 -0
- data/lib/protobuf/rpc/error/client_error.rb +31 -0
- data/lib/protobuf/rpc/error/server_error.rb +43 -0
- data/lib/protobuf/rpc/error.rb +25 -0
- data/lib/protobuf/rpc/rpc.pb.rb +118 -0
- data/lib/protobuf/rpc/server.rb +89 -0
- data/lib/protobuf/rpc/servers/evented/server.rb +41 -0
- data/lib/protobuf/rpc/servers/evented_runner.rb +21 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +111 -0
- data/lib/protobuf/rpc/servers/socket/worker.rb +66 -0
- data/lib/protobuf/rpc/servers/socket_runner.rb +27 -0
- data/lib/protobuf/rpc/servers/zmq/broker.rb +87 -0
- data/lib/protobuf/rpc/servers/zmq/server.rb +50 -0
- data/lib/protobuf/rpc/servers/zmq/util.rb +27 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +60 -0
- data/lib/protobuf/rpc/servers/zmq_runner.rb +25 -0
- data/lib/protobuf/rpc/service.rb +173 -0
- data/lib/protobuf/rpc/service_dispatcher.rb +130 -0
- data/lib/protobuf/rpc/service_filters.rb +267 -0
- data/lib/protobuf/rpc/stat.rb +83 -0
- data/lib/protobuf/socket.rb +22 -0
- data/lib/protobuf/version.rb +4 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/lib/protobuf/zmq.rb +21 -0
- data/lib/protobuf.rb +86 -0
- data/proto/rpc.pb.rb +48 -0
- data/proto/rpc.proto +73 -0
- data/protobuf.gemspec +44 -0
- data/spec/benchmark/tasks.rb +179 -0
- data/spec/functional/embedded_service_spec.rb +7 -0
- data/spec/functional/evented_server_spec.rb +64 -0
- data/spec/functional/socket_server_spec.rb +58 -0
- data/spec/functional/zmq_server_spec.rb +58 -0
- data/spec/lib/protobuf/cli_spec.rb +212 -0
- data/spec/lib/protobuf/enum_spec.rb +98 -0
- data/spec/lib/protobuf/enum_value_spec.rb +15 -0
- data/spec/lib/protobuf/logger_spec.rb +131 -0
- data/spec/lib/protobuf/message/encoder_spec.rb +19 -0
- data/spec/lib/protobuf/message_spec.rb +209 -0
- data/spec/lib/protobuf/rpc/client_spec.rb +158 -0
- data/spec/lib/protobuf/rpc/connector_spec.rb +32 -0
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
- data/spec/lib/protobuf/rpc/connectors/common_spec.rb +128 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +36 -0
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +22 -0
- data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +18 -0
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +43 -0
- data/spec/lib/protobuf/rpc/servers/zmq/broker_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +45 -0
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +44 -0
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +116 -0
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +451 -0
- data/spec/lib/protobuf/rpc/service_spec.rb +165 -0
- data/spec/lib/protobuf_spec.rb +62 -0
- data/spec/spec_helper.rb +51 -0
- data/spec/support/all.rb +6 -0
- data/spec/support/server.rb +101 -0
- data/spec/support/test/enum.pb.rb +34 -0
- data/spec/support/test/enum.proto +12 -0
- data/spec/support/test/resource.pb.rb +58 -0
- data/spec/support/test/resource.proto +31 -0
- data/spec/support/test/resource_service.rb +14 -0
- data/spec/support/test_app_file.rb +2 -0
- data/spec/support/tolerance_matcher.rb +40 -0
- data/test/data/data.bin +3 -0
- data/test/data/data_source.py +14 -0
- data/test/data/types.bin +0 -0
- data/test/data/types_source.py +22 -0
- data/test/data/unk.png +0 -0
- data/test/proto/addressbook.pb.rb +66 -0
- data/test/proto/addressbook.proto +33 -0
- data/test/proto/addressbook_base.pb.rb +58 -0
- data/test/proto/addressbook_base.proto +26 -0
- data/test/proto/addressbook_ext.pb.rb +20 -0
- data/test/proto/addressbook_ext.proto +6 -0
- data/test/proto/collision.pb.rb +17 -0
- data/test/proto/collision.proto +5 -0
- data/test/proto/ext_collision.pb.rb +24 -0
- data/test/proto/ext_collision.proto +8 -0
- data/test/proto/ext_range.pb.rb +22 -0
- data/test/proto/ext_range.proto +7 -0
- data/test/proto/float_default.proto +10 -0
- data/test/proto/lowercase.pb.rb +30 -0
- data/test/proto/lowercase.proto +9 -0
- data/test/proto/merge.pb.rb +39 -0
- data/test/proto/merge.proto +15 -0
- data/test/proto/nested.pb.rb +30 -0
- data/test/proto/nested.proto +9 -0
- data/test/proto/optional_field.pb.rb +35 -0
- data/test/proto/optional_field.proto +12 -0
- data/test/proto/packed.pb.rb +22 -0
- data/test/proto/packed.proto +6 -0
- data/test/proto/rpc.proto +6 -0
- data/test/proto/types.pb.rb +84 -0
- data/test/proto/types.proto +37 -0
- data/test/test_addressbook.rb +56 -0
- data/test/test_enum_value.rb +41 -0
- data/test/test_extension.rb +36 -0
- data/test/test_lowercase.rb +11 -0
- data/test/test_message.rb +128 -0
- data/test/test_optional_field.rb +103 -0
- data/test/test_packed_field.rb +40 -0
- data/test/test_parse.rb +15 -0
- data/test/test_repeated_types.rb +132 -0
- data/test/test_serialize.rb +61 -0
- data/test/test_standard_message.rb +96 -0
- data/test/test_types.rb +226 -0
- metadata +461 -0
|
@@ -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
|
+
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
// Author: kenton@google.com (Kenton Varda)
|
|
32
|
+
// from google3/strings/substitute.h
|
|
33
|
+
|
|
34
|
+
#include <string>
|
|
35
|
+
#include <google/protobuf/stubs/common.h>
|
|
36
|
+
#include <google/protobuf/stubs/strutil.h>
|
|
37
|
+
|
|
38
|
+
#ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
|
|
39
|
+
#define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
|
|
40
|
+
|
|
41
|
+
namespace google {
|
|
42
|
+
namespace protobuf {
|
|
43
|
+
namespace strings {
|
|
44
|
+
|
|
45
|
+
// ----------------------------------------------------------------------
|
|
46
|
+
// strings::Substitute()
|
|
47
|
+
// strings::SubstituteAndAppend()
|
|
48
|
+
// Kind of like StringPrintf, but different.
|
|
49
|
+
//
|
|
50
|
+
// Example:
|
|
51
|
+
// string GetMessage(string first_name, string last_name, int age) {
|
|
52
|
+
// return strings::Substitute("My name is $0 $1 and I am $2 years old.",
|
|
53
|
+
// first_name, last_name, age);
|
|
54
|
+
// }
|
|
55
|
+
//
|
|
56
|
+
// Differences from StringPrintf:
|
|
57
|
+
// * The format string does not identify the types of arguments.
|
|
58
|
+
// Instead, the magic of C++ deals with this for us. See below
|
|
59
|
+
// for a list of accepted types.
|
|
60
|
+
// * Substitutions in the format string are identified by a '$'
|
|
61
|
+
// followed by a digit. So, you can use arguments out-of-order and
|
|
62
|
+
// use the same argument multiple times.
|
|
63
|
+
// * It's much faster than StringPrintf.
|
|
64
|
+
//
|
|
65
|
+
// Supported types:
|
|
66
|
+
// * Strings (const char*, const string&)
|
|
67
|
+
// * Note that this means you do not have to add .c_str() to all of
|
|
68
|
+
// your strings. In fact, you shouldn't; it will be slower.
|
|
69
|
+
// * int32, int64, uint32, uint64: Formatted using SimpleItoa().
|
|
70
|
+
// * float, double: Formatted using SimpleFtoa() and SimpleDtoa().
|
|
71
|
+
// * bool: Printed as "true" or "false".
|
|
72
|
+
//
|
|
73
|
+
// SubstituteAndAppend() is like Substitute() but appends the result to
|
|
74
|
+
// *output. Example:
|
|
75
|
+
//
|
|
76
|
+
// string str;
|
|
77
|
+
// strings::SubstituteAndAppend(&str,
|
|
78
|
+
// "My name is $0 $1 and I am $2 years old.",
|
|
79
|
+
// first_name, last_name, age);
|
|
80
|
+
//
|
|
81
|
+
// Substitute() is significantly faster than StringPrintf(). For very
|
|
82
|
+
// large strings, it may be orders of magnitude faster.
|
|
83
|
+
// ----------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
namespace internal { // Implementation details.
|
|
86
|
+
|
|
87
|
+
class SubstituteArg {
|
|
88
|
+
public:
|
|
89
|
+
inline SubstituteArg(const char* value)
|
|
90
|
+
: text_(value), size_(strlen(text_)) {}
|
|
91
|
+
inline SubstituteArg(const string& value)
|
|
92
|
+
: text_(value.data()), size_(value.size()) {}
|
|
93
|
+
|
|
94
|
+
// Indicates that no argument was given.
|
|
95
|
+
inline explicit SubstituteArg()
|
|
96
|
+
: text_(NULL), size_(-1) {}
|
|
97
|
+
|
|
98
|
+
// Primitives
|
|
99
|
+
// We don't overload for signed and unsigned char because if people are
|
|
100
|
+
// explicitly declaring their chars as signed or unsigned then they are
|
|
101
|
+
// probably actually using them as 8-bit integers and would probably
|
|
102
|
+
// prefer an integer representation. But, we don't really know. So, we
|
|
103
|
+
// make the caller decide what to do.
|
|
104
|
+
inline SubstituteArg(char value)
|
|
105
|
+
: text_(scratch_), size_(1) { scratch_[0] = value; }
|
|
106
|
+
inline SubstituteArg(short value)
|
|
107
|
+
: text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
108
|
+
inline SubstituteArg(unsigned short value)
|
|
109
|
+
: text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
110
|
+
inline SubstituteArg(int value)
|
|
111
|
+
: text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
112
|
+
inline SubstituteArg(unsigned int value)
|
|
113
|
+
: text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
114
|
+
inline SubstituteArg(long value)
|
|
115
|
+
: text_(FastLongToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
116
|
+
inline SubstituteArg(unsigned long value)
|
|
117
|
+
: text_(FastULongToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
118
|
+
inline SubstituteArg(long long value)
|
|
119
|
+
: text_(FastInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
120
|
+
inline SubstituteArg(unsigned long long value)
|
|
121
|
+
: text_(FastUInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
122
|
+
inline SubstituteArg(float value)
|
|
123
|
+
: text_(FloatToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
124
|
+
inline SubstituteArg(double value)
|
|
125
|
+
: text_(DoubleToBuffer(value, scratch_)), size_(strlen(text_)) {}
|
|
126
|
+
inline SubstituteArg(bool value)
|
|
127
|
+
: text_(value ? "true" : "false"), size_(strlen(text_)) {}
|
|
128
|
+
|
|
129
|
+
inline const char* data() const { return text_; }
|
|
130
|
+
inline int size() const { return size_; }
|
|
131
|
+
|
|
132
|
+
private:
|
|
133
|
+
const char* text_;
|
|
134
|
+
int size_;
|
|
135
|
+
char scratch_[kFastToBufferSize];
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
} // namespace internal
|
|
139
|
+
|
|
140
|
+
LIBPROTOBUF_EXPORT string Substitute(
|
|
141
|
+
const char* format,
|
|
142
|
+
const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
|
|
143
|
+
const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
|
|
144
|
+
const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
|
|
145
|
+
const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
|
|
146
|
+
const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
|
|
147
|
+
const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
|
|
148
|
+
const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
|
|
149
|
+
const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
|
|
150
|
+
const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
|
|
151
|
+
const internal::SubstituteArg& arg9 = internal::SubstituteArg());
|
|
152
|
+
|
|
153
|
+
LIBPROTOBUF_EXPORT void SubstituteAndAppend(
|
|
154
|
+
string* output, const char* format,
|
|
155
|
+
const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
|
|
156
|
+
const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
|
|
157
|
+
const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
|
|
158
|
+
const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
|
|
159
|
+
const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
|
|
160
|
+
const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
|
|
161
|
+
const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
|
|
162
|
+
const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
|
|
163
|
+
const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
|
|
164
|
+
const internal::SubstituteArg& arg9 = internal::SubstituteArg());
|
|
165
|
+
|
|
166
|
+
} // namespace strings
|
|
167
|
+
} // namespace protobuf
|
|
168
|
+
} // namespace google
|
|
169
|
+
|
|
170
|
+
#endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
|