File tree Expand file tree Collapse file tree 2 files changed +11
-6
lines changed
main/java/by/andd3dfx/collections
test/java/by/andd3dfx/collections Expand file tree Collapse file tree 2 files changed +11
-6
lines changed Original file line number Diff line number Diff line change @@ -70,11 +70,7 @@ public static String simplifyPath(String path) {
7070 while (i < chars .length ) {
7171 switch (chars [i ]) {
7272 case '/' :
73- if (accumulator .toString ().equals (".." ) && !stack .isEmpty ()) {
74- stack .pop ();
75- } else {
76- checkAndPush (stack , accumulator );
77- }
73+ process2DotsCase (accumulator , stack );
7874 accumulator = new StringBuilder ();
7975 break ;
8076
@@ -83,14 +79,22 @@ public static String simplifyPath(String path) {
8379 }
8480 i ++;
8581 }
86- checkAndPush ( stack , accumulator );
82+ process2DotsCase ( accumulator , stack );
8783
8884 List <String > folders = Arrays .stream (stack .toArray (new String [0 ]))
8985 .toList ()
9086 .reversed ();
9187 return "/" + String .join ("/" , folders );
9288 }
9389
90+ private static void process2DotsCase (StringBuilder accumulator , Deque <String > stack ) {
91+ if (accumulator .toString ().equals (".." ) && !stack .isEmpty ()) {
92+ stack .pop ();
93+ } else {
94+ checkAndPush (stack , accumulator );
95+ }
96+ }
97+
9498 private static void checkAndPush (Deque <String > stack , StringBuilder accumulator ) {
9599 var string = accumulator .toString ();
96100 if (!string .isEmpty () && !string .matches ("\\ .{1,2}" )) {
Original file line number Diff line number Diff line change @@ -15,5 +15,6 @@ public void testSimplifyPath() {
1515 assertThat (SimplifyPath .simplifyPath ("/../" )).isEqualTo ("/" );
1616 assertThat (SimplifyPath .simplifyPath ("/.../a/../b/c/../d/./" ))
1717 .isEqualTo ("/.../b/d" );
18+ assertThat (SimplifyPath .simplifyPath ("/a//b////c/d//././/.." )).isEqualTo ("/a/b/c" );
1819 }
1920}
You can’t perform that action at this time.
0 commit comments