Просмотр исходного кода

Add StringUtils class + tests.

These are from the TextPane, but seem fairly general purpose.

Change-Id: Ic119eb461a365b4f7050715c6bc0abd5c034e57c
Reviewed-on: http://gerrit.dmdirc.com/4081
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/81/4081/2
Chris Smith 9 лет назад
Родитель
Сommit
0e02c175f6
2 измененных файлов: 185 добавлений и 0 удалений
  1. 97
    0
      src/com/dmdirc/util/StringUtils.java
  2. 88
    0
      test/com/dmdirc/util/StringUtilsTest.java

+ 97
- 0
src/com/dmdirc/util/StringUtils.java Просмотреть файл

@@ -0,0 +1,97 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+/**
26
+ * Utilities for dealing with strings.
27
+ */
28
+public final class StringUtils {
29
+
30
+    private StringUtils() {
31
+        // Shouldn't be instansiated.
32
+    }
33
+
34
+    /**
35
+     * Returns the indexes for the word surrounding the index in the specified string.
36
+     *
37
+     * @param text  Text to get word from
38
+     * @param index Index to get surrounding word
39
+     *
40
+     * @return An array containing two elements: the index of the first character of the word, and
41
+     * the index of the first character beyond the end of the word. If the specified index is not
42
+     * contained within a word (i.e., is whitespace) then 0,0 is returned.
43
+     */
44
+    public static int[] indiciesOfWord(final CharSequence text, final int index) {
45
+        final int start = indexOfStartOfWord(text, index);
46
+        final int end = indexOfEndOfWord(text, index);
47
+
48
+        if (start > end) {
49
+            return new int[]{0, 0};
50
+        }
51
+
52
+        return new int[]{start, end};
53
+
54
+    }
55
+
56
+    /**
57
+     * Returns the start index for the word surrounding the index in the specified string.
58
+     *
59
+     * @param text  Text to get word from
60
+     * @param index Index to get surrounding word
61
+     *
62
+     * @return Start index of the word surrounding the index
63
+     */
64
+    public static int indexOfStartOfWord(final CharSequence text, final int index) {
65
+        int start = index;
66
+
67
+        // Traverse backwards
68
+        while (start > 0 && start < text.length() && text.charAt(start) != ' ') {
69
+            start--;
70
+        }
71
+        if (start + 1 < text.length() && text.charAt(start) == ' ') {
72
+            start++;
73
+        }
74
+
75
+        return start;
76
+    }
77
+
78
+    /**
79
+     * Returns the end index for the word surrounding the index in the specified string.
80
+     *
81
+     * @param text  Text to get word from
82
+     * @param index Index to get surrounding word
83
+     *
84
+     * @return End index of the word surrounding the index
85
+     */
86
+    public static int indexOfEndOfWord(final CharSequence text, final int index) {
87
+        int end = index;
88
+
89
+        // And forwards
90
+        while (end < text.length() && end >= 0 && text.charAt(end) != ' ') {
91
+            end++;
92
+        }
93
+
94
+        return end;
95
+    }
96
+
97
+}

+ 88
- 0
test/com/dmdirc/util/StringUtilsTest.java Просмотреть файл

@@ -0,0 +1,88 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.util;
24
+
25
+import org.junit.Test;
26
+
27
+import static org.junit.Assert.assertArrayEquals;
28
+import static org.junit.Assert.assertEquals;
29
+
30
+public class StringUtilsTest {
31
+
32
+    @Test
33
+    public void testIndexOfStartOfWord() {
34
+        final String input = "abc 123 def";
35
+        assertEquals(0, StringUtils.indexOfStartOfWord(input, 0));
36
+        assertEquals(0, StringUtils.indexOfStartOfWord(input, 1));
37
+        assertEquals(0, StringUtils.indexOfStartOfWord(input, 2));
38
+
39
+        assertEquals(4, StringUtils.indexOfStartOfWord(input, 3));
40
+        assertEquals(4, StringUtils.indexOfStartOfWord(input, 4));
41
+        assertEquals(4, StringUtils.indexOfStartOfWord(input, 5));
42
+        assertEquals(4, StringUtils.indexOfStartOfWord(input, 6));
43
+
44
+        assertEquals(8, StringUtils.indexOfStartOfWord(input, 7));
45
+        assertEquals(8, StringUtils.indexOfStartOfWord(input, 8));
46
+        assertEquals(8, StringUtils.indexOfStartOfWord(input, 9));
47
+        assertEquals(8, StringUtils.indexOfStartOfWord(input, 10));
48
+    }
49
+
50
+    @Test
51
+    public void testIndexOfEndOfWord() {
52
+        final String input = "abc 123 def";
53
+        assertEquals(3, StringUtils.indexOfEndOfWord(input, 0));
54
+        assertEquals(3, StringUtils.indexOfEndOfWord(input, 1));
55
+        assertEquals(3, StringUtils.indexOfEndOfWord(input, 2));
56
+        assertEquals(3, StringUtils.indexOfEndOfWord(input, 3));
57
+
58
+        assertEquals(7, StringUtils.indexOfEndOfWord(input, 4));
59
+        assertEquals(7, StringUtils.indexOfEndOfWord(input, 5));
60
+        assertEquals(7, StringUtils.indexOfEndOfWord(input, 6));
61
+        assertEquals(7, StringUtils.indexOfEndOfWord(input, 7));
62
+
63
+        assertEquals(11, StringUtils.indexOfEndOfWord(input, 8));
64
+        assertEquals(11, StringUtils.indexOfEndOfWord(input, 9));
65
+        assertEquals(11, StringUtils.indexOfEndOfWord(input, 10));
66
+    }
67
+
68
+    @Test
69
+    public void testIndicesOfWord() {
70
+        final String input = "abc 123 def";
71
+        assertArrayEquals(new int[]{0, 3}, StringUtils.indiciesOfWord(input, 0));
72
+        assertArrayEquals(new int[]{0, 3}, StringUtils.indiciesOfWord(input, 1));
73
+        assertArrayEquals(new int[]{0, 3}, StringUtils.indiciesOfWord(input, 2));
74
+
75
+        assertArrayEquals(new int[]{0, 0}, StringUtils.indiciesOfWord(input, 3));
76
+
77
+        assertArrayEquals(new int[]{4, 7}, StringUtils.indiciesOfWord(input, 4));
78
+        assertArrayEquals(new int[]{4, 7}, StringUtils.indiciesOfWord(input, 5));
79
+        assertArrayEquals(new int[]{4, 7}, StringUtils.indiciesOfWord(input, 6));
80
+
81
+        assertArrayEquals(new int[]{0, 0}, StringUtils.indiciesOfWord(input, 7));
82
+
83
+        assertArrayEquals(new int[]{8, 11}, StringUtils.indiciesOfWord(input, 8));
84
+        assertArrayEquals(new int[]{8, 11}, StringUtils.indiciesOfWord(input, 9));
85
+        assertArrayEquals(new int[]{8, 11}, StringUtils.indiciesOfWord(input, 10));
86
+    }
87
+
88
+}

Загрузка…
Отмена
Сохранить