Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit a52f740

Browse files
authored
Optimized context creation with regex, fixed whitespace issue (#124)
* optimize context creation with regex * version number * fixed whitespace issue * consistent variable name * test cleanup
1 parent 5ddac23 commit a52f740

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.kttdevelopment</groupId>
1212
<artifactId>simplehttpserver</artifactId>
13-
<version>4.3.0</version>
13+
<version>4.3.1</version>
1414

1515
<name>simplehttpserver</name>
1616
<description>📕 SimpleHttpServer :: Simplified implementation of the sun http server :: Simplified handlers to execute complex operations</description>

src/main/java/com/kttdevelopment/simplehttpserver/ContextUtil.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package com.kttdevelopment.simplehttpserver;
22

3+
import java.util.regex.Pattern;
4+
35
/**
46
* A utility class used to generate uniform contexts. Applications do not use this class.
57
*
68
* @since 03.05.03
7-
* @version 03.05.03
9+
* @version 4.3.1
810
* @author Ktt Development
911
*/
1012
public abstract class ContextUtil {
1113

14+
// replace consecutive slashes and back slashes with a forward slash
15+
private static final Pattern forwardSlashRegex = Pattern.compile("/{2,}|\\\\+");
16+
// remove start and end slashes as well as whitespace
17+
private static final Pattern stripSlashRegex = Pattern.compile("^\\s*/*|/*\\s*$");
18+
1219
/**
1320
* Generates a uniform context with forward slashes removing any consecutive slashes.
1421
*
@@ -22,11 +29,11 @@ public abstract class ContextUtil {
2229
* @author Ktt Development
2330
*/
2431
public static String getContext(final String context, final boolean leadingSlash, final boolean trailingSlash){
25-
final String linSlash = context.replace('\\','/').replaceAll("/{2,}","/");
26-
if(linSlash.isBlank() || linSlash.equals("/")) // handle blank or '/' contexts
27-
return leadingSlash || trailingSlash ? "/" : "";
28-
final String ltSlash = (!(linSlash.charAt(0) == '/') ? '/' : "") + linSlash + (!(linSlash.charAt(linSlash.length()-1) == '/') ? '/' : "");
29-
return ltSlash.substring(leadingSlash ? 0 : 1, ltSlash.length() + (trailingSlash ? 0 : -1));
32+
final String linSlash = forwardSlashRegex.matcher(context).replaceAll("/");
33+
final String strippedSlash = stripSlashRegex.matcher(linSlash).replaceAll("");
34+
return strippedSlash.length() == 0
35+
? leadingSlash || trailingSlash ? "/" : ""
36+
: (leadingSlash ? "/" : "") + strippedSlash + (trailingSlash ? "/" : "");
3037
}
3138

3239
/**

src/test/java/com/kttdevelopment/simplehttpserver/ContextUtilTests.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ public final void testContexts(){
3939
new test("testNoneBackSlash" , "\\testNoneBackSlash\\" , false , false),
4040
new test("/testBackSlash/" , "\\testBackSlash\\" , true , true ),
4141
new test("/testConsecutiveBackSlash/" , "\\\\testConsecutiveBackSlash\\\\", true , true ),
42-
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true )
42+
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true ),
43+
new test("/testWhitespace/" , " /testWhitespace/ " , true , true),
44+
new test("/ testWhitespace /" , "/ testWhitespace /" , true , true),
45+
new test(" testWhitespace " , "/ testWhitespace /" , false , false),
46+
new test("testWhitespace" , " testWhitespace " , false , false),
47+
new test("/testWhitespace/" , " /testWhitespace/ " , true , true),
4348
};
4449

4550
for(final test test : tests)
@@ -66,24 +71,24 @@ public testJoin(final String expected, final boolean leadingSlash, final boolean
6671
@Test
6772
public final void testJoin(){
6873
final testJoin[] tests = {
69-
new testJoin("testBlank" , false , false ,"testBlank",""),
70-
new testJoin("/testBlank/" , true , true ,"testBlank",""),
71-
new testJoin("testBlank" , false , false ,"","testBlank"),
72-
new testJoin("/testBlank/" , true , true ,"","testBlank"),
73-
new testJoin("" , false , false ,"",""),
74-
new testJoin("/" , true , true ,"",""),
75-
new testJoin("trailing/slash" , false , false ,"trailing/","slash/"),
76-
new testJoin("/trailing/slash/" , true , true ,"trailing/","slash/"),
77-
new testJoin("leading/slash" , false , false ,"leading/","slash/"),
78-
new testJoin("/leading/slash/" , true , true ,"leading/","slash/"),
79-
new testJoin("double/slash" , false , false ,"/double/","/slash/"),
80-
new testJoin("/double/slash/" , true , true ,"/double/","/slash/"),
81-
new testJoin("no/slash" , false , false ,"no","slash"),
82-
new testJoin("/no/slash/" , true , true ,"no","slash"),
83-
new testJoin("consecutive/slash" , false , false ,"//consecutive//","//slash//"),
84-
new testJoin("/consecutive/slash/" , true , true ,"//consecutive//","//slash//"),
85-
new testJoin("mixed/slash" , false , false ,"\\mixed\\","//slash//"),
86-
new testJoin("/mixed/slash/" , true , true ,"\\mixed\\","//slash//"),
74+
new testJoin("testBlank" , false , false , "testBlank" , ""),
75+
new testJoin("/testBlank/" , true , true , "testBlank" , ""),
76+
new testJoin("testBlank" , false , false , "" , "testBlank"),
77+
new testJoin("/testBlank/" , true , true , "" , "testBlank"),
78+
new testJoin("" , false , false , "" , ""),
79+
new testJoin("/" , true , true , "" , ""),
80+
new testJoin("trailing/slash" , false , false , "trailing/" , "slash/"),
81+
new testJoin("/trailing/slash/" , true , true , "trailing/" , "slash/"),
82+
new testJoin("leading/slash" , false , false , "leading/" , "slash/"),
83+
new testJoin("/leading/slash/" , true , true , "leading/" , "slash/"),
84+
new testJoin("double/slash" , false , false , "/double/" , "/slash/"),
85+
new testJoin("/double/slash/" , true , true , "/double/" , "/slash/"),
86+
new testJoin("no/slash" , false , false , "no" , "slash"),
87+
new testJoin("/no/slash/" , true , true , "no" , "slash"),
88+
new testJoin("consecutive/slash" , false , false , "//consecutive//" , "//slash//"),
89+
new testJoin("/consecutive/slash/" , true , true , "//consecutive//" , "//slash//"),
90+
new testJoin("mixed/slash" , false , false , "\\mixed\\" , "//slash//"),
91+
new testJoin("/mixed/slash/" , true , true , "\\mixed\\" , "//slash//"),
8792
};
8893

8994
for(final testJoin test : tests)

0 commit comments

Comments
 (0)