1/*
2 * Copyright (c) 2023 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#ifndef ECMASCRIPT_BASE_PATH_HELPER_H
16#define ECMASCRIPT_BASE_PATH_HELPER_H
17
18#include "ecmascript/compiler/aot_file/aot_file_manager.h"
19#include "ecmascript/base/string_helper.h"
20#include "ecmascript/ecma_macros.h"
21#include "ecmascript/ecma_string.h"
22#include "ecmascript/ecma_vm.h"
23#include "ecmascript/global_env.h"
24#include "ecmascript/js_tagged_value-inl.h"
25#include "ecmascript/jspandafile/js_pandafile.h"
26
27namespace panda::ecmascript::base {
28class PathHelper {
29public:
30    static constexpr char COLON_TAG[] = ":";
31    static constexpr char CURRENT_DIREATORY_TAG[] = "./";
32    static constexpr char DOUBLE_POINT_TAG[] = "..";
33    static constexpr char DOUBLE_SLASH_TAG[] = "//";
34    static constexpr char NAME_SPACE_TAG = '@';
35    static constexpr char NORMALIZED_OHMURL_TAG = '&';
36    static constexpr char POINT_STRING_TAG[] = ".";
37    static constexpr char POINT_TAG = '.';
38    static constexpr char SLASH_TAG = '/';
39
40    static CString NormalizePath(const CString &fileName);
41    static CString ResolveDirPath(const CString &fileName);
42
43    /*
44     * Before: moduleName@nameSpace
45     * After:  moduleName
46     */
47    inline static void DeleteNamespace(CString &moduleName)
48    {
49        size_t pos = moduleName.find(NAME_SPACE_TAG);
50        if (pos == CString::npos) {
51            return;
52        }
53        moduleName.erase(pos, moduleName.size() - pos);
54    }
55
56    /*
57     * Before: moduleName@nameSpace
58     * After:  nameSpace
59     */
60    inline static CString GetHarName(const CString &moduleName)
61    {
62        size_t pos = moduleName.find(NAME_SPACE_TAG);
63        ASSERT(pos != CString::npos);
64        return moduleName.substr(pos + 1);
65    }
66
67    /*
68     * Before: bundleName/moduleName@namespace/moduleName/xxx/xxx
69     * After:  moduleName/xxx/xxx
70     */
71    inline static void AdaptOldIsaRecord(CString &recordName)
72    {
73        size_t pos = recordName.find(SLASH_TAG);
74        if (pos != CString::npos) {
75            pos = recordName.find(SLASH_TAG, pos + 1);
76            if (pos != CString::npos) {
77                recordName = recordName.substr(pos + 1);
78            }
79        }
80    }
81
82    /*
83     * Before: @***:xxxx
84     * After:  xxxx
85     */
86    inline static CString GetStrippedModuleName(const CString &moduleRequestName)
87    {
88        size_t pos = moduleRequestName.find(COLON_TAG);
89        if (pos == CString::npos) {
90            LOG_FULL(FATAL) << "Unknown format " << moduleRequestName;
91        }
92        return moduleRequestName.substr(pos + 1);
93    }
94
95    /*
96     * Before: @xxx:****
97     * After:  xxx
98     */
99    inline static CString GetInternalModulePrefix(const CString &moduleRequestName)
100    {
101        size_t pos = moduleRequestName.find(COLON_TAG);
102        if (pos == CString::npos) {
103            LOG_FULL(FATAL) << "Unknown format " << moduleRequestName;
104        }
105        ASSERT(pos != 0);
106        return moduleRequestName.substr(1, pos - 1);
107    }
108};
109}  // namespace panda::ecmascript::base
110#endif  // ECMASCRIPT_BASE_PATH_HELPER_H