1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008-2024 Google Inc. 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 "google/protobuf/compiler/code_generator_lite.h"
9
10 #include <string>
11 #include <utility>
12 #include <vector>
13
14 #include "absl/strings/match.h"
15 #include "absl/strings/str_split.h"
16 #include "absl/strings/string_view.h"
17 #include "absl/strings/strip.h"
18
19 namespace google {
20 namespace protobuf {
21 namespace compiler {
22
23 // Parses a set of comma-delimited name/value pairs.
ParseGeneratorParameter(absl::string_view text,std::vector<std::pair<std::string,std::string>> * output)24 void ParseGeneratorParameter(
25 absl::string_view text,
26 std::vector<std::pair<std::string, std::string> >* output) {
27 std::vector<absl::string_view> parts =
28 absl::StrSplit(text, ',', absl::SkipEmpty());
29
30 for (absl::string_view part : parts) {
31 auto equals_pos = part.find_first_of('=');
32 if (equals_pos == absl::string_view::npos) {
33 output->emplace_back(part, "");
34 } else {
35 output->emplace_back(part.substr(0, equals_pos),
36 part.substr(equals_pos + 1));
37 }
38 }
39 }
40
41 // Strips ".proto" or ".protodevel" from the end of a filename.
StripProto(absl::string_view filename)42 std::string StripProto(absl::string_view filename) {
43 if (absl::EndsWith(filename, ".protodevel")) {
44 return std::string(absl::StripSuffix(filename, ".protodevel"));
45 } else {
46 return std::string(absl::StripSuffix(filename, ".proto"));
47 }
48 }
49
IsKnownFeatureProto(absl::string_view filename)50 bool IsKnownFeatureProto(absl::string_view filename) {
51 if (filename == "google/protobuf/cpp_features.proto" ||
52 filename == "google/protobuf/java_features.proto") {
53 return true;
54 }
55 return false;
56 }
57
58 } // namespace compiler
59 } // namespace protobuf
60 } // namespace google
61