aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/any.cc
diff options
context:
space:
mode:
authorGravatar Jisi Liu <jisi.liu@gmail.com>2016-03-30 11:39:59 -0700
committerGravatar Jisi Liu <jisi.liu@gmail.com>2016-03-30 11:39:59 -0700
commit3b3c8abb9635eb3ea078a821a99c9ef29d66dff7 (patch)
tree7d2ec154f15c9f9153d890e76b6cf30e471ea488 /src/google/protobuf/any.cc
parent78105897a8f01c7be9cf8502b6c58d47eb1ccdd7 (diff)
Integrate google internal changes.
Diffstat (limited to 'src/google/protobuf/any.cc')
-rw-r--r--src/google/protobuf/any.cc46
1 files changed, 23 insertions, 23 deletions
diff --git a/src/google/protobuf/any.cc b/src/google/protobuf/any.cc
index f3ca06bf..f7b1d310 100644
--- a/src/google/protobuf/any.cc
+++ b/src/google/protobuf/any.cc
@@ -35,10 +35,15 @@ namespace protobuf {
namespace internal {
namespace {
-string GetTypeUrl(const Descriptor* message) {
- return string(kTypeGoogleApisComPrefix) + message->full_name();
+string GetTypeUrl(const Descriptor* message,
+ const string& type_url_prefix) {
+ if (!type_url_prefix.empty() &&
+ type_url_prefix[type_url_prefix.size() - 1] == '/') {
+ return type_url_prefix + message->full_name();
+ } else {
+ return type_url_prefix + "/" + message->full_name();
+ }
}
-
} // namespace
const char kAnyFullTypeName[] = "google.protobuf.Any";
@@ -50,8 +55,13 @@ AnyMetadata::AnyMetadata(UrlType* type_url, ValueType* value)
}
void AnyMetadata::PackFrom(const Message& message) {
+ PackFrom(message, kTypeGoogleApisComPrefix);
+}
+
+void AnyMetadata::PackFrom(const Message& message,
+ const string& type_url_prefix) {
type_url_->SetNoArena(&::google::protobuf::internal::GetEmptyString(),
- GetTypeUrl(message.GetDescriptor()));
+ GetTypeUrl(message.GetDescriptor(), type_url_prefix));
message.SerializeToString(value_->MutableNoArena(
&::google::protobuf::internal::GetEmptyStringAlreadyInited()));
}
@@ -67,30 +77,20 @@ bool AnyMetadata::UnpackTo(Message* message) const {
bool AnyMetadata::InternalIs(const Descriptor* descriptor) const {
const string type_url = type_url_->GetNoArena(
&::google::protobuf::internal::GetEmptyString());
- const string full_name = descriptor->full_name();
- if (type_url.length() < full_name.length()) {
- return false;
+ string full_name;
+ if (!ParseAnyTypeUrl(type_url, &full_name)) {
+ return false;
}
- return (0 == type_url.compare(
- type_url.length() - full_name.length(),
- full_name.length(),
- full_name));
+ return full_name == descriptor->full_name();
}
bool ParseAnyTypeUrl(const string& type_url, string* full_type_name) {
- static const char* prefix[] = {
- kTypeGoogleApisComPrefix,
- kTypeGoogleProdComPrefix
- };
- for (int i = 0; i < 2; i++) {
- const int prefix_len = strlen(prefix[i]);
- if (strncmp(type_url.c_str(), prefix[i], prefix_len) == 0) {
- full_type_name->assign(type_url.data() + prefix_len,
- type_url.size() - prefix_len);
- return true;
- }
+ size_t pos = type_url.find_last_of("/");
+ if (pos == string::npos || pos + 1 == type_url.size()) {
+ return false;
}
- return false;
+ *full_type_name = type_url.substr(pos + 1);
+ return true;
}