-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
StringUtils.java
50 lines (41 loc) · 1.95 KB
/
StringUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package io.cucumber.gherkin;
import java.util.regex.Pattern;
class StringUtils {
private static final Pattern LTRIM = Pattern.compile("^[ \\t\\n\\x0B\\f\\r\\x85\\xA0]+");
private static final Pattern LTRIM_KEEP_NEW_LINES = Pattern.compile("^[ \\t\\x0B\\f\\r\\x85\\xA0]+");
private static final Pattern RTRIM_KEEP_NEW_LINES = Pattern.compile("[ \\t\\x0B\\f\\r\\x85\\xA0]+$");
private static final Pattern RTRIM = Pattern.compile("[ \\t\\n\\x0B\\f\\r\\x85\\xA0]+$");
private static final Pattern TRIM = Pattern.compile("^[ \\t\\n\\x0B\\f\\r\\x85\\xA0]+|[ \\t\\n\\x0B\\f\\r\\x85\\xA0]+$");
static String ltrim(String s) {
// https://stackoverflow.com/questions/1060570/why-is-non-breaking-space-not-a-whitespace-character-in-java
return LTRIM.matcher(s).replaceAll("");
}
/**
* Trims whitespace on the left-hand side up to the first
* non-whitespace character and exclude new lines from the
* usual definition of whitespace.
*/
static String ltrimKeepNewLines(String s) {
// https://stackoverflow.com/questions/1060570/why-is-non-breaking-space-not-a-whitespace-character-in-java
return LTRIM_KEEP_NEW_LINES.matcher(s).replaceAll("");
}
/**
* Trims whitespace on the right-hand side up to the first
* non-whitespace character and exclude new lines from the
* usual definition of whitespace.
*/
static String rtrimKeepNewLines(String s) {
// https://stackoverflow.com/questions/1060570/why-is-non-breaking-space-not-a-whitespace-character-in-java
return RTRIM_KEEP_NEW_LINES.matcher(s).replaceAll("");
}
static String rtrim(String s) {
return RTRIM.matcher(s).replaceAll("");
}
static String trim(String s) {
return TRIM.matcher(s).replaceAll("");
}
static int symbolCount(String string) {
// http://rosettacode.org/wiki/String_length#Java
return string.codePointCount(0, string.length());
}
}