From 3b3c8abb9635eb3ea078a821a99c9ef29d66dff7 Mon Sep 17 00:00:00 2001 From: Jisi Liu Date: Wed, 30 Mar 2016 11:39:59 -0700 Subject: Integrate google internal changes. --- src/google/protobuf/any.cc | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'src/google/protobuf/any.cc') 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; } -- cgit v1.2.3