* @brief Check whether a path starts with a given directory prefix. * * @param[in] filePath The file path to check. * @param[in] directory The directory prefix to match against. * @return @c true if @p filePath starts with @p directory. */
| 51 | * @return @c true if @p filePath starts with @p directory. |
| 52 | */ |
| 53 | bool isUnderDirectory(std::string filePath, std::string directory) |
| 54 | { |
| 55 | std::ranges::replace(filePath, '\\', '/'); |
| 56 | std::ranges::replace(directory, '\\', '/'); |
| 57 | // Collapse repeated slashes (e.g. home path "build/debug//" + "Mod") |
| 58 | auto collapseSlashes = [](std::string& s) { |
| 59 | auto out = s.begin(); |
| 60 | for (auto it = s.begin(); it != s.end(); ++it) { |
| 61 | if (*it == '/' && out != s.begin() && *(out - 1) == '/') { |
| 62 | continue; |
| 63 | } |
| 64 | *out++ = *it; |
| 65 | } |
| 66 | s.erase(out, s.end()); |
| 67 | }; |
| 68 | collapseSlashes(filePath); |
| 69 | collapseSlashes(directory); |
| 70 | if (!directory.empty() && directory.back() != '/') { |
| 71 | directory += '/'; |
| 72 | } |
| 73 | return filePath.starts_with(directory); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @brief Check whether a module import should be allowed during document restore. |