This is an automated email from the ASF dual-hosted git repository. bodewig pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ant.git
commit 6d2f06bd9feea5b20c6999f026cc6cc2e6629476 Author: Stefan Bodewig <bode...@apache.org> AuthorDate: Sun Jun 8 09:06:04 2025 +0200 extract logic for detecting and stripping leading path separators --- src/main/org/apache/tools/ant/util/FileUtils.java | 27 ++++++++++++++++++++++ .../org/apache/tools/ant/util/FileUtilsTest.java | 18 +++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/main/org/apache/tools/ant/util/FileUtils.java b/src/main/org/apache/tools/ant/util/FileUtils.java index 15dd90ddd..1bf6d7e3e 100644 --- a/src/main/org/apache/tools/ant/util/FileUtils.java +++ b/src/main/org/apache/tools/ant/util/FileUtils.java @@ -1959,4 +1959,31 @@ public class FileUtils { } return Optional.of(caseSensitive); } + + /** + * Tests whether the given path starts with a path separator. + * @param path path to check. + * @return true if the path starts with a path separator. + * @since Ant 1.10.16 + */ + public boolean startsWithPathSeparator(String path) { + if (path == null || path.length() == 0) { + return false; + } + char c = path.charAt(0); + return c == File.pathSeparatorChar || c == '/' || c == '\\'; + } + + /** + * Strips a leading path separator from path that actually start + * with one. + * @param path the whose leading file separator should be stripped. + * @return path with the leading path separator stripped off if + * path starts with a path separator - or the unchanged path + * otherwise. + * @since Ant 1.10.16 + */ + public String stripLeadingPathSeparator(String path) { + return startsWithPathSeparator(path) ? path.substring(1) : path; + } } diff --git a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java index 5a168a514..bdf1f3245 100644 --- a/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java +++ b/src/tests/junit/org/apache/tools/ant/util/FileUtilsTest.java @@ -861,6 +861,24 @@ public class FileUtilsTest { ? "sensitive" : "insensitive"), expectedCaseSensitivity, actualCaseSensitivity.get()); } + @Test + public void testStartsWithPathSeparator() { + assertTrue(getFileUtils().startsWithPathSeparator("/foo")); + assertTrue(getFileUtils().startsWithPathSeparator("\\foo")); + assertFalse(getFileUtils().startsWithPathSeparator("c:\\foo")); + assertFalse(getFileUtils().startsWithPathSeparator("foo")); + assertFalse(getFileUtils().startsWithPathSeparator("file:/foo")); + } + + @Test + public void testStripLeadingPathSeparator() { + assertEquals("foo", getFileUtils().stripLeadingPathSeparator("/foo")); + assertEquals("foo", getFileUtils().stripLeadingPathSeparator("\\foo")); + assertEquals("c:\\foo", getFileUtils().stripLeadingPathSeparator("c:\\foo")); + assertEquals("foo", getFileUtils().stripLeadingPathSeparator("foo")); + assertEquals("file:/foo", getFileUtils().stripLeadingPathSeparator("file:/foo")); + } + /** * adapt file separators to local conventions */