1 // Copyright (c) 2009-2024, Google LLC
2 // All rights reserved.
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
8 #include "upb_generator/common/names.h"
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"
24 std::string
ToCIdent(absl::string_view str
) {
25 return absl::StrReplaceAll(str
, {{".", "_"}, {"/", "_"}, {"-", "_"}});
28 std::string
ToPreproc(absl::string_view str
) {
29 return absl::AsciiStrToUpper(ToCIdent(str
));
34 bool IsDescriptorProto(absl::string_view filename
) {
35 return filename
== "net/proto2/proto/descriptor.proto" ||
36 filename
== "google/protobuf/descriptor.proto";
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
);
44 return std::string(fname
.substr(0, lastdot
));
47 std::string
IncludeGuard(absl::string_view filename
) {
48 return ToPreproc(filename
) + "_UPB_H_";
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"
57 " * Do not edit -- your changes will be discarded when the file is\n"
60 // Intentional line break.
61 "PROTOBUF GENCODE */\n"
66 std::string
PadPrefix(absl::string_view tag
) {
67 return tag
.empty() ? "" : absl::StrCat(" ", tag
);
70 } // namespace generator