99import java .nio .file .WatchKey ;
1010import java .nio .file .WatchService ;
1111import java .util .ArrayDeque ;
12+ import java .util .ArrayList ;
1213import java .util .Arrays ;
1314import java .util .Deque ;
15+ import java .util .List ;
1416import java .util .Objects ;
17+ import java .util .StringJoiner ;
1518import java .util .function .IntBinaryOperator ;
16- import java .util .regex .Pattern ;
17- import java .util .stream .Collectors ;
1819import java .util .stream .IntStream ;
1920
2021public class UnionPath implements Path {
21- private static final Pattern SEPARATOR_BEGIN_END ;
22- private static final Pattern SEPARATOR_DUPLICATES ;
23-
24- private static final Pattern SEPARATOR_SPLIT ;
25-
26- static {
27- var sep = "(?:" + Pattern .quote (UnionFileSystem .SEP_STRING ) + ")" ;
28- SEPARATOR_BEGIN_END = Pattern .compile ("^" + sep + "*|" + sep + "*$" );
29- SEPARATOR_DUPLICATES = Pattern .compile (sep + "(?=" + sep + ")" );
30- SEPARATOR_SPLIT = Pattern .compile (sep );
31- }
3222 private final UnionFileSystem fileSystem ;
3323 private final boolean absolute ;
3424 private final String [] pathParts ;
@@ -42,9 +32,13 @@ public class UnionPath implements Path {
4232 this .absolute = false ;
4333 this .pathParts = new String [0 ];
4434 } else {
45- final var longstring = Arrays .stream (pathParts )
46- .filter (part -> !part .isEmpty ())
47- .collect (Collectors .joining (UnionFileSystem .SEP_STRING ));
35+ StringJoiner joiner = new StringJoiner (UnionFileSystem .SEP_STRING );
36+ for (String element : pathParts ) {
37+ if (!element .isEmpty ()) {
38+ joiner .add (element );
39+ }
40+ }
41+ final var longstring = joiner .toString ();
4842 this .absolute = longstring .startsWith (UnionFileSystem .SEP_STRING );
4943 this .pathParts = getPathParts (longstring );
5044 }
@@ -67,13 +61,22 @@ private UnionPath(final UnionFileSystem fileSystem, boolean absolute, boolean is
6761 }
6862
6963 private String [] getPathParts (final String longstring ) {
70- var clean = longstring .replace ("\\ " , UnionFileSystem .SEP_STRING );
71- clean = SEPARATOR_BEGIN_END .matcher (clean ).replaceAll ("" );
72- clean = SEPARATOR_DUPLICATES .matcher (clean ).replaceAll ("" );
73- if (clean .isEmpty ())
74- return new String [0 ];
75- else
76- return SEPARATOR_SPLIT .split (clean );
64+ var clean = longstring .replace ('\\' , '/' );
65+ int startIndex = 0 ;
66+ List <String > parts = new ArrayList <>();
67+ while (startIndex != longstring .length ()) {
68+ int index = clean .indexOf ('/' , startIndex );
69+ if (index == -1 ) {
70+ parts .add (clean .substring (startIndex ));
71+ break ;
72+ }
73+ // Skips double slash and slash and start/end
74+ if (index != startIndex ) {
75+ parts .add (clean .substring (startIndex , index ));
76+ }
77+ startIndex = (index + 1 );
78+ }
79+ return parts .toArray (String []::new );
7780 }
7881
7982 @ Override
0 commit comments