Skip to content

Commit 1811cf4

Browse files
bactgoneall
authored andcommitted
Pre-compile Patterns fo removeCommentChars
Signed-off-by: Arthit Suriyawongkul <[email protected]>
1 parent ee4487e commit 1811cf4

File tree

1 file changed

+45
-32
lines changed

1 file changed

+45
-32
lines changed

src/main/java/org/spdx/utility/compare/LicenseCompareHelper.java

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ public class LicenseCompareHelper {
6363

6464
protected static final Pattern REGEX_QUANTIFIER_PATTERN = Pattern.compile(".*\\.\\{(\\d+),(\\d+)}$");
6565
static final String START_COMMENT_CHAR_PATTERN = "(//|/\\*|\\*|#|' |REM |<!--|--|;|\\(\\*|\\{-)|\\.\\\\\"";
66-
66+
67+
static final Pattern END_COMMENT_PATTERN = Pattern.compile("(\\*/|-->|-}|\\*\\)|\\s\\*)\\s*$");
68+
static final Pattern START_COMMENT_PATTERN = Pattern.compile("^\\s*" + START_COMMENT_CHAR_PATTERN);
69+
static final Pattern BEGIN_OPTIONAL_COMMENT_PATTERN = Pattern
70+
.compile("^\\s*<<beginOptional>>\\s*" + START_COMMENT_CHAR_PATTERN);
71+
6772
/**
68-
* @param objectUri URI of the license
69-
* @return license ID
73+
* Convert a license object URI to its corresponding License ID
74+
*
75+
* @param objectUri The URI of the license.
76+
* @return The SPDX License ID extracted from the URI, or the original
77+
* {@code objectUri} if no known prefix is found.
7078
*/
7179
public static String licenseUriToLicenseId(String objectUri) {
7280
if (objectUri.startsWith(SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX)) {
@@ -77,41 +85,46 @@ public static String licenseUriToLicenseId(String objectUri) {
7785
return objectUri; // no match - should we throw an exception?
7886
}
7987
}
80-
88+
8189
/**
82-
* Remove common comment characters from either a template or license text strings
90+
* Remove common comment characters from either a template or license text
91+
* strings
92+
*
8393
* @param s string source
8494
* @return string without comment characters
8595
*/
8696
public static String removeCommentChars(String s) {
87-
StringBuilder sb = new StringBuilder();
88-
BufferedReader reader = null;
89-
try {
90-
reader = new BufferedReader(new StringReader(s));
91-
String line = reader.readLine();
92-
while (line != null) {
93-
line = line.replaceAll("(\\*/|-->|-}|\\*\\)|\\s\\*)\\s*$", ""); // remove end of line comments
94-
line = line.replaceAll("^\\s*" + START_COMMENT_CHAR_PATTERN, ""); // remove start of line comments
95-
line = line.replaceAll("^\\s*<<beginOptional>>\\s*" + START_COMMENT_CHAR_PATTERN, "<<beginOptional>>");
96-
sb.append(line);
97-
sb.append("\n");
98-
line = reader.readLine();
99-
}
100-
return sb.toString();
101-
} catch (IOException e) {
102-
logger.warn("IO error reading strings?!?", e);
103-
return s;
104-
} finally {
105-
if (Objects.nonNull(reader)) {
106-
try {
107-
reader.close();
108-
} catch (IOException e) {
109-
logger.warn("IO error closing a string reader?!?", e);
110-
}
111-
}
112-
}
97+
if (s == null) {
98+
return "";
99+
}
100+
StringBuilder sb = new StringBuilder();
101+
BufferedReader reader = null;
102+
try {
103+
reader = new BufferedReader(new StringReader(s));
104+
String line = reader.readLine();
105+
while (line != null) {
106+
line = END_COMMENT_PATTERN.matcher(line).replaceAll("");
107+
line = START_COMMENT_PATTERN.matcher(line).replaceAll("");
108+
line = BEGIN_OPTIONAL_COMMENT_PATTERN.matcher(line).replaceAll("<<beginOptional>>");
109+
sb.append(line);
110+
sb.append("\n");
111+
line = reader.readLine();
112+
}
113+
return sb.toString();
114+
} catch (IOException e) {
115+
logger.warn("IO error reading strings?!?", e);
116+
return s;
117+
} finally {
118+
if (Objects.nonNull(reader)) {
119+
try {
120+
reader.close();
121+
} catch (IOException e) {
122+
logger.warn("IO error closing a string reader?!?", e);
123+
}
124+
}
125+
}
113126
}
114-
127+
115128
/**
116129
* Locate the original text starting with the start token and ending with the end token
117130
* @param fullLicenseText entire license text

0 commit comments

Comments
 (0)