1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "abc_file_utils.h"
17
18 namespace panda::abc2program {
19
GetPkgNameFromNormalizedImport(const std::string & normalizedImport)20 std::string GetPkgNameFromNormalizedImport(const std::string &normalizedImport)
21 {
22 std::string pkgName {};
23 size_t pos = normalizedImport.find(SLASH_TAG);
24 if (pos != std::string::npos) {
25 pkgName = normalizedImport.substr(0, pos);
26 }
27 if (normalizedImport[0] == AT_SEPARATOR) {
28 pos = normalizedImport.find(SLASH_TAG, pos + 1);
29 if (pos != std::string::npos) {
30 pkgName = normalizedImport.substr(0, pos);
31 }
32 }
33 return pkgName;
34 }
35
GetPkgNameFromRecordName(const std::string & recordName)36 std::string GetPkgNameFromRecordName(const std::string &recordName)
37 {
38 std::string normalizedImport {};
39 std::string pkgName {};
40 auto items = Split(recordName, NORMALIZED_OHMURL_SEPARATOR);
41 if (items.size() <= NORMALIZED_IMPORT_POS) {
42 return pkgName;
43 }
44 normalizedImport = items[NORMALIZED_IMPORT_POS];
45 return GetPkgNameFromNormalizedImport(normalizedImport);
46 }
47
Split(const std::string & str,const char delimiter)48 std::vector<std::string> Split(const std::string &str, const char delimiter)
49 {
50 std::vector<std::string> items;
51 size_t start = 0;
52 size_t pos = str.find(delimiter);
53 while (pos != std::string::npos) {
54 std::string item = str.substr(start, pos - start);
55 items.emplace_back(item);
56 start = pos + 1;
57 pos = str.find(delimiter, start);
58 }
59 std::string tail = str.substr(start);
60 items.emplace_back(tail);
61 return items;
62 }
63
IsGlobalTypeName(const std::string & type_name)64 bool AbcFileUtils::IsGlobalTypeName(const std::string &type_name)
65 {
66 return (type_name == GLOBAL_TYPE_NAME);
67 }
68
IsArrayTypeName(const std::string & type_name)69 bool AbcFileUtils::IsArrayTypeName(const std::string &type_name)
70 {
71 return (type_name.find(ARRAY_TYPE_PREFIX) != std::string::npos);
72 }
73
IsESTypeAnnotationName(const std::string & type_name)74 bool AbcFileUtils::IsESTypeAnnotationName(const std::string &type_name)
75 {
76 return (type_name == ES_TYPE_ANNOTATION_NAME);
77 }
78
IsSystemTypeName(const std::string & type_name)79 bool AbcFileUtils::IsSystemTypeName(const std::string &type_name)
80 {
81 return (IsGlobalTypeName(type_name) || IsArrayTypeName(type_name));
82 }
83
GetLabelNameByInstIdx(size_t inst_idx)84 std::string AbcFileUtils::GetLabelNameByInstIdx(size_t inst_idx)
85 {
86 std::stringstream name;
87 name << LABEL_PREFIX << inst_idx;
88 return name.str();
89 }
90
91 } // namespace panda::abc2program
92