• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2009-2024, Google LLC
2 // All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "upb_generator/common/names.h"
9 
10 #include <cstddef>
11 #include <string>
12 
13 #include "absl/strings/ascii.h"
14 #include "absl/strings/str_cat.h"
15 #include "absl/strings/str_replace.h"
16 #include "absl/strings/string_view.h"
17 #include "absl/strings/substitute.h"
18 
19 namespace upb {
20 namespace generator {
21 
22 namespace {
23 
ToCIdent(absl::string_view str)24 std::string ToCIdent(absl::string_view str) {
25   return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}});
26 }
27 
ToPreproc(absl::string_view str)28 std::string ToPreproc(absl::string_view str) {
29   return absl::AsciiStrToUpper(ToCIdent(str));
30 }
31 
32 }  // namespace
33 
IsDescriptorProto(absl::string_view filename)34 bool IsDescriptorProto(absl::string_view filename) {
35   return filename == "net/proto2/proto/descriptor.proto" ||
36          filename == "google/protobuf/descriptor.proto";
37 }
38 
StripExtension(absl::string_view fname)39 std::string StripExtension(absl::string_view fname) {
40   size_t lastdot = fname.find_last_of('.');
41   if (lastdot == std::string::npos) {
42     return std::string(fname);
43   }
44   return std::string(fname.substr(0, lastdot));
45 }
46 
IncludeGuard(absl::string_view filename)47 std::string IncludeGuard(absl::string_view filename) {
48   return ToPreproc(filename) + "_UPB_H_";
49 }
50 
FileWarning(absl::string_view filename)51 std::string FileWarning(absl::string_view filename) {
52   return absl::Substitute(
53       "/* This file was generated by upb_generator from the input file:\n"
54       " *\n"
55       " *     $0\n"
56       " *\n"
57       " * Do not edit -- your changes will be discarded when the file is\n"
58       " * regenerated.\n"
59       " * NO CHECKED-IN "
60       // Intentional line break.
61       "PROTOBUF GENCODE */\n"
62       "\n",
63       filename);
64 }
65 
PadPrefix(absl::string_view tag)66 std::string PadPrefix(absl::string_view tag) {
67   return tag.empty() ? "" : absl::StrCat(" ", tag);
68 }
69 
70 }  // namespace generator
71 }  // namespace upb
72