|
|
@@ -0,0 +1,73 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira
|
|
|
+ */
|
|
|
+
|
|
|
+package net.ranides.assira.text;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public final class StringTraits {
|
|
|
+
|
|
|
+ private StringTraits() {
|
|
|
+ /* utility class */
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int length(String value) {
|
|
|
+ return null == value ? 0 : value.length();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isEmpty(String value) {
|
|
|
+ return null == value || value.isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean isBlank(String value) {
|
|
|
+ return isEmpty(value) || value.trim().isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int indexOf(CharSequence text, CharSequence pattern) {
|
|
|
+ int[] next = initKMP(pattern);
|
|
|
+
|
|
|
+ int M = pattern.length();
|
|
|
+ int N = text.length();
|
|
|
+ int i, j;
|
|
|
+ for (i = 0, j = 0; i < N && j < M; i++) {
|
|
|
+ while (j >= 0 && text.charAt(i) != pattern.charAt(j)) {
|
|
|
+ j = next[j];
|
|
|
+ }
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ return (j == M) ? i - M : -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean contains(CharSequence text, CharSequence pattern) {
|
|
|
+ return indexOf(text, pattern) >= 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int[] initKMP(CharSequence pattern) {
|
|
|
+ int M = pattern.length();
|
|
|
+ int[] next = new int[M];
|
|
|
+ int j = -1;
|
|
|
+ for (int i = 0; i < M; i++) {
|
|
|
+ if (i == 0) {
|
|
|
+ next[i] = -1;
|
|
|
+ }
|
|
|
+ else if (pattern.charAt(i) != pattern.charAt(j)) {
|
|
|
+ next[i] = j;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ next[i] = next[j];
|
|
|
+ }
|
|
|
+ while (j >= 0 && pattern.charAt(i) != pattern.charAt(j)) {
|
|
|
+ j = next[j];
|
|
|
+ }
|
|
|
+ j++;
|
|
|
+ }
|
|
|
+ return next;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|