Skip to content

Commit

Permalink
Misc.java: Light refactoring.
Browse files Browse the repository at this point in the history
Use stripRight in trimOneTrailingNewline.
Remove redundant variable in getPrefix.
Use fixed string replace in replaceEscapes, no need for regex.
  • Loading branch information
Clement Cherlin committed Mar 3, 2022
1 parent 0ad8696 commit bc3804f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 8 additions & 11 deletions src/org/stringtemplate/v4/misc/Misc.java
Expand Up @@ -81,8 +81,8 @@ public static String trimOneStartingNewline(String s) {

/** Strip a single newline character from the end of {@code s}. */
public static String trimOneTrailingNewline(String s) {
if ( s.endsWith("\r\n") ) s = s.substring(0, s.length()-2);
else if ( s.endsWith("\n") ) s = s.substring(0, s.length()-1);
if ( s.endsWith("\r\n") ) s = stripRight(s, 2);
else if ( s.endsWith("\n") ) s = stripRight(s, 1);
return s;
}

Expand Down Expand Up @@ -137,16 +137,13 @@ public static String joinPathSegments(String path1, String path2) {
public static String getPrefix(String name) {
if (name==null) return "/";
String parent = getParent(name);
String prefix = parent;
if ( !parent.endsWith("/") ) prefix += '/';
return prefix;
return parent.endsWith("/") ? parent : parent+"/";
}

public static String replaceEscapes(String s) {
s = s.replaceAll("\n", "\\\\n");
s = s.replaceAll("\r", "\\\\r");
s = s.replaceAll("\t", "\\\\t");
return s;
return s.replace("\n", "\\n")
.replace("\r", "\\r")
.replace("\t", "\\t");
}

/**
Expand Down Expand Up @@ -196,8 +193,8 @@ public static boolean urlExists(URL url) {
is.close();
} catch (Throwable e) {
// Closing the input stream may throw an exception. See bug below. Most probabaly it was
// the true reason for this commit:
// https://github.com/antlr/stringtemplate4/commit/21484ed46f1b20b2cdaec49f9d5a626fb26a493c
// the true reason for this commit:
// https://github.com/antlr/stringtemplate4/commit/21484ed46f1b20b2cdaec49f9d5a626fb26a493c
// https://bugs.openjdk.java.net/browse/JDK-8080094
// e.printStackTrace();
}
Expand Down
2 changes: 0 additions & 2 deletions test/org/stringtemplate/v4/test/TestJoinPathSegments.java
Expand Up @@ -12,13 +12,11 @@ public class TestJoinPathSegments extends BaseTest {

@Test
public void testBareRootDirAndBareGroupDir() {

assertEquals(EXPECTED, Misc.joinPathSegments(ROOT_DIR, TEMPLATE_GROUP_FILE));
}

@Test
public void testRootDirWithTrailingSlashAndBareGroupDir() {

assertEquals(EXPECTED, Misc.joinPathSegments(ROOT_DIR + "/", TEMPLATE_GROUP_FILE));
}

Expand Down

0 comments on commit bc3804f

Please sign in to comment.