diff --git a/ets2panda/util/path.cpp b/ets2panda/util/path.cpp index 83603c79ce9986e46538dc306bd5904df1885ecf..127cc7969ca29360372a370763df64549fa9d995 100644 --- a/ets2panda/util/path.cpp +++ b/ets2panda/util/path.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,6 +20,9 @@ namespace ark::es2panda::util { +constexpr size_t ALLOWED_EXTENSIONS_SIZE = 8; +static std::array supportedExtensions = {".d.sts", ".sts", ".d.ets", ".ets", + ".d.ts", ".ts", ".js", ".abc"}; Path::Path() = default; Path::Path(const util::StringView &absolutePath, ArenaAllocator *allocator) @@ -27,6 +30,14 @@ Path::Path(const util::StringView &absolutePath, ArenaAllocator *allocator) Initializer(absolutePath.Mutf8(), allocator); } +static bool EndsWith(const std::string &str, const std::string &suffix) +{ + if (str.length() < suffix.length()) { + return false; + } + return str.compare(str.length() - suffix.length(), suffix.length(), suffix) == 0; +} + void Path::Initializer(const std::string &path, ArenaAllocator *allocator) { isRelative_ = false; @@ -55,10 +66,18 @@ void Path::InitializeFileName() util::StringView fileName = path_.Substr(position + 1, path_.Length()); if (GetExtension().Empty()) { fileName_ = fileName; - } else { - int extensionPosition = fileName.Mutf8().find_last_of('.'); - fileName_ = fileName.Substr(0, extensionPosition); + return; } + + for (auto &extension : supportedExtensions) { + if (EndsWith(fileName.Mutf8(), extension)) { + fileName_ = fileName.Substr(0, fileName.Length() - extension.length()); + return; + } + } + + int extensionPosition = fileName.Mutf8().find_last_of('.'); + fileName_ = fileName.Substr(0, extensionPosition); } void Path::InitializeFileExtension() @@ -67,6 +86,13 @@ void Path::InitializeFileExtension() return; } + for (auto &extension : supportedExtensions) { + if (EndsWith(path_.Mutf8(), extension)) { + fileExtension_ = util::UString(extension, allocator_).View(); + return; + } + } + size_t position = path_.Mutf8().find_last_of('.'); if (position != std::string::npos && position + 1 <= path_.Length()) { fileExtension_ = path_.Substr(position + 1, path_.Length());