MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / isUnderDirectory

Function isUnderDirectory

src/App/PropertyPythonObject.cpp:53–74  ·  view source on GitHub ↗

* @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. */

Source from the content-addressed store, hash-verified

51 * @return @c true if @p filePath starts with @p directory.
52 */
53bool 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.

Callers 1

isAllowedModuleFunction · 0.85

Calls 6

replaceFunction · 0.85
beginMethod · 0.45
endMethod · 0.45
eraseMethod · 0.45
emptyMethod · 0.45
backMethod · 0.45

Tested by

no test coverage detected