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 <string>
21 
22 namespace aapt {
23 
24 struct NameMangler {
25     /**
26      * Mangles the name in `outName` with the `package` and stores the mangled
27      * result in `outName`. The mangled name should contain symbols that are
28      * illegal to define in XML, so that there will never be name mangling
29      * collisions.
30      */
mangleNameMangler31     static void mangle(const std::u16string& package, std::u16string* outName) {
32         *outName = package + u"$" + *outName;
33     }
34 
35     /**
36      * Unmangles the name in `outName`, storing the correct name back in `outName`
37      * and the package in `outPackage`. Returns true if the name was unmangled or
38      * false if the name was never mangled to begin with.
39      */
unmangleNameMangler40     static bool unmangle(std::u16string* outName, std::u16string* outPackage) {
41         size_t pivot = outName->find(u'$');
42         if (pivot == std::string::npos) {
43             return false;
44         }
45 
46         outPackage->assign(outName->data(), pivot);
47         outName->assign(outName->data() + pivot + 1, outName->size() - (pivot + 1));
48         return true;
49     }
50 };
51 
52 } // namespace aapt
53 
54 #endif // AAPT_NAME_MANGLER_H
55