Debian Codegraph @

tools/aapt2/NameMangler.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef AAPT_NAME_MANGLER_H
18 #define AAPT_NAME_MANGLER_H
19
20 #include <set>
21 #include <string>
22
23 #include "Resource.h"
24
25 namespace aapt {
26
27 struct NameManglerPolicy {
28 /**
29 * Represents the package we are trying to build. References pointing
30 * to this package are not mangled, and mangled references inherit this
31 * package name.
32 */
33 std::string target_package_name;
34
35 /**
36 * We must know which references to mangle, and which to keep (android vs.
37 * com.android.support).
38 */
39 std::set<std::string> packages_to_mangle;
40 };
41
42 class NameMangler {
43 public:
44 explicit NameMangler(NameManglerPolicy policy) : policy_(policy) {}
45
46 std::optional<ResourceName> MangleName(const ResourceName& name) {
47 if (policy_.target_package_name == name.package ||
48 policy_.packages_to_mangle.count(name.package) == 0) {
49 return {};
50 }
51
52 std::string mangled_entry_name = MangleEntry(name.package, name.entry);
53 return ResourceName(policy_.target_package_name, name.type,
54 mangled_entry_name);
55 }
56
57 bool ShouldMangle(const std::string& package) const {
58 if (package.empty() || policy_.target_package_name == package) {
59 return false;
60 }
61 return policy_.packages_to_mangle.count(package) != 0;
62 }
63
64 const std::string& GetTargetPackageName() const { return policy_.target_package_name; }
65
66 /**
67 * Returns a mangled name that is a combination of `name` and `package`.
68 * The mangled name should contain symbols that are illegal to define in XML,
69 * so that there will never be name mangling collisions.
70 */
71 static std::string MangleEntry(const std::string& package, const std::string& name) {
72 return package + "$" + name;
73 }
74
75 /**
76 * Unmangles the name in `outName`, storing the correct name back in `outName`
77 * and the package in `outPackage`. Returns true if the name was unmangled or
78 * false if the name was never mangled to begin with.
79 */
80 static bool Unmangle(std::string* out_name, std::string* out_package) {
81 size_t pivot = out_name->find('$');
82 if (pivot == std::string::npos) {
83 return false;
84 }
85
86 out_package->assign(out_name->data(), pivot);
87 std::string new_name = out_name->substr(pivot + 1);
88 *out_name = std::move(new_name);
89 return true;
90 }
91
92 private:
93 NameManglerPolicy policy_;
94 };
95
96 } // namespace aapt
97
98 #endif // AAPT_NAME_MANGLER_H