-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use same "new line" char sequence when comparing the license agreement
- Loading branch information
1 parent
aba04af
commit 7378424
Showing
3 changed files
with
37 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,45 @@ | ||
package org.hibernate.infra.bot.util; | ||
|
||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
|
||
public class Patterns { | ||
private static final Set<Character> REGEX_ESCAPE_CHARS = Set.of( '(', ')', '[', ']', '{', '}', '\\', '.', '?', '*', '+' ); | ||
|
||
public static Pattern compile(String pattern) { | ||
return Pattern.compile(pattern, Pattern.DOTALL | Pattern.CASE_INSENSITIVE); | ||
} | ||
|
||
/** | ||
* We want to replace any new line character sequences ({@code \n \r \n\r}) with a more predictable one | ||
* which should allow us to use {@link String#contains(CharSequence)} in our text-based checks. | ||
* <p> | ||
* Apparently GitHub body/config files may be returned containing various combinations of these new line character sequences, | ||
* and we may end up failing the checks when we shouldn't. | ||
*/ | ||
public static String sanitizeNewLines(String value) { | ||
return value == null ? null : value.replaceAll("\\R", "\n"); | ||
} | ||
|
||
public static String escapeSpecialCharacters(String value) { | ||
if ( value == null ) { | ||
return null; | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
for ( char c : value.toCharArray() ) { | ||
if ( REGEX_ESCAPE_CHARS.contains( c ) ) { | ||
sb.append( '\\' ); | ||
} | ||
if ( c == '\n' ) { | ||
sb.append( '\\' ).append( 'n' ); | ||
} | ||
else { | ||
sb.append( c ); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private Patterns() { | ||
} | ||
} |