Coverage Report

Created: 2025-08-26 06:02

/src/sentencepiece/third_party/protobuf-lite/generated_enum_util.cc
Line
Count
Source (jump to first uncovered line)
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
42
0
bool EnumCompareByName(const EnumEntry& a, const EnumEntry& b) {
43
0
  return StringPiece(a.name) < StringPiece(b.name);
44
0
}
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.
49
0
int GetValue(const EnumEntry* enums, int i, int target) {
50
0
  if (i == -1) {
51
0
    return target;
52
0
  } else {
53
0
    return enums[i].value;
54
0
  }
55
0
}
56
57
}  // namespace
58
59
bool LookUpEnumValue(const EnumEntry* enums, size_t size,
60
0
                     StringPiece name, int* value) {
61
0
  EnumEntry target{name, 0};
62
0
  auto it = std::lower_bound(enums, enums + size, target, EnumCompareByName);
63
0
  if (it != enums + size && it->name == name) {
64
0
    *value = it->value;
65
0
    return true;
66
0
  }
67
0
  return false;
68
0
}
69
70
int LookUpEnumName(const EnumEntry* enums, const int* sorted_indices,
71
0
                   size_t size, int value) {
72
0
  auto comparator = [enums, value](int a, int b) {
73
0
    return GetValue(enums, a, value) < GetValue(enums, b, value);
74
0
  };
75
0
  auto it =
76
0
      std::lower_bound(sorted_indices, sorted_indices + size, -1, comparator);
77
0
  if (it != sorted_indices + size && enums[*it].value == value) {
78
0
    return it - sorted_indices;
79
0
  }
80
0
  return -1;
81
0
}
82
83
bool InitializeEnumStrings(
84
    const EnumEntry* enums, const int* sorted_indices, size_t size,
85
0
    internal::ExplicitlyConstructed<std::string>* enum_strings) {
86
0
  for (int i = 0; i < size; ++i) {
87
0
    enum_strings[i].Construct(enums[sorted_indices[i]].name);
88
0
    internal::OnShutdownDestroyString(enum_strings[i].get_mutable());
89
0
  }
90
0
  return true;
91
0
}
92
93
}  // namespace internal
94
}  // namespace protobuf
95
}  // namespace google