From f424b3c58956ae802a2a8bb03b9fa2b35b068ddc Mon Sep 17 00:00:00 2001 From: tengtengh <10678859+tengtengh@user.noreply.gitee.com> Date: Fri, 28 Feb 2025 19:08:34 +0800 Subject: [PATCH] Fix the util path Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IBPTJS Description: fix the util::path Tested-by: ninja tests (passed) ets_testrunner (passed) Signed-off-by: tengtengh --- ets2panda/util/path.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/ets2panda/util/path.cpp b/ets2panda/util/path.cpp index 83603c79ce..127cc7969c 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()); -- Gitee