Allow using `PathSegmentIterator` to iterate over `PathFragment` segments.
This is an initial pass to find callers of `PathFragment#get(First)Segment(s)` that can clearly and easily benefit from doing something smarter. There are plenty more places that can be improved, but will take more work.
Since there are no more callers of the method that returned an array of segments, have `getSegments()` populate an `ImmutableList.Builder` directly. To make sure it is a strict performance improvement, continue to count the segments first to accurately pre-size the builder.
PiperOrigin-RevId: 358204210
diff --git a/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java b/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
index 9b39d42..6f8967c 100644
--- a/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
+++ b/src/test/java/com/google/devtools/build/lib/vfs/PathFragmentTest.java
@@ -268,6 +268,13 @@
}
@Test
+ public void segments() {
+ assertThat(create("/this/is/a/path").segments())
+ .containsExactly("this", "is", "a", "path")
+ .inOrder();
+ }
+
+ @Test
public void testBasename() throws Exception {
assertThat(create("foo/bar").getBaseName()).isEqualTo("bar");
assertThat(create("/foo/bar").getBaseName()).isEqualTo("bar");