• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 #include <google/protobuf/generated_enum_util.h>
32 
33 #include <algorithm>
34 
35 #include <google/protobuf/generated_message_util.h>
36 
37 namespace google {
38 namespace protobuf {
39 namespace internal {
40 namespace {
41 
EnumCompareByName(const EnumEntry & a,const EnumEntry & b)42 bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) {
43   return StringPiece(a.name) < StringPiece(b.name);
44 }
45 
46 // Gets the numeric value of the EnumEntry at the given index, but returns a
47 // special value for the index -1. This gives a way to use std::lower_bound on a
48 // sorted array of indices while searching for value that we associate with -1.
GetValue(const EnumEntry * enums,int i,int target)49 int GetValue(const EnumEntry* enums, int i, int target) {
50   if (i == -1) {
51     return target;
52   } else {
53     return enums[i].value;
54   }
55 }
56 
57 }  // namespace
58 
LookUpEnumValue(const EnumEntry * enums,size_t size,StringPiece name,int * value)59 bool LookUpEnumValue(const EnumEntry* enums, size_t size,
60                      StringPiece name, int* value) {
61   EnumEntry target{name, 0};
62   auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName);
63   if (it != enums + size && it->name == name) {
64     *value = it->value;
65     return true;
66   }
67   return false;
68 }
69 
LookUpEnumName(const EnumEntry * enums,const int * sorted_indices,size_t size,int value)70 int LookUpEnumName(const EnumEntry* enums, const int* sorted_indices,
71                    size_t size, int value) {
72   auto comparator = [enums, value](int a, int b) {
73     return GetValue(enums, a, value) < GetValue(enums, b, value);
74   };
75   auto it =
76       std::lower_bound(sorted_indices, sorted_indices + size, -1, comparator);
77   if (it != sorted_indices + size && enums[*it].value == value) {
78     return it - sorted_indices;
79   }
80   return -1;
81 }
82 
InitializeEnumStrings(const EnumEntry * enums,const int * sorted_indices,size_t size,internal::ExplicitlyConstructed<std::string> * enum_strings)83 bool InitializeEnumStrings(
84     const EnumEntry* enums, const int* sorted_indices, size_t size,
85     internal::ExplicitlyConstructed<std::string>* enum_strings) {
86   for (int i = 0; i < size; ++i) {
87     enum_strings[i].Construct(enums[sorted_indices[i]].name);
88     internal::OnShutdownDestroyString(enum_strings[i].get_mutable());
89   }
90   return true;
91 }
92 
93 }  // namespace internal
94 }  // namespace protobuf
95 }  // namespace google
96