Browse Source

Styliser method to get string between unstyled indicies

Fixes issue 2199
Unit test for issue 2199

Change-Id: I270fa7265766536f578d5a06c2e3fc274b61e37c
Reviewed-on: http://gerrit.dmdirc.com/753
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
tags/0.6.3b1
Chris Smith 14 years ago
parent
commit
5c3b562ea5

+ 38
- 0
src/com/dmdirc/ui/messages/Styliser.java View File

@@ -223,6 +223,44 @@ public class Styliser implements ConfigChangeListener {
223 223
         
224 224
         return styledDoc;
225 225
     }
226
+
227
+    /**
228
+     * Retrieves the styled String contained within the unstyled offsets
229
+     * specified. That is, the <code>from</code> and <code>to</code> arguments
230
+     * correspond to indexes in an unstyled version of the <code>styled</code>
231
+     * string. The unstyled indices are translated to offsets within the
232
+     * styled String, and the return value includes all text and control codes
233
+     * between those indices.
234
+     * <p>
235
+     * The index translation is left-biased; that is, the indices are translated
236
+     * to be as far left as they possibly can be. This means that the start of
237
+     * the string will include any control codes immediately preceeding the
238
+     * desired text, and the end will not include any trailing codes.
239
+     *
240
+     * @param styled The styled String to be operated on
241
+     * @param from The starting index in the unstyled string
242
+     * @param to The ending index in the unstyled string
243
+     * @return The corresponding text between the two indices
244
+     * @since 0.6.3
245
+     */
246
+    public static String getStyledText(final String styled, final int from, final int to) {
247
+        final String unstyled = stipControlCodes(styled);
248
+        final String startBit = unstyled.substring(0, from);
249
+        final String middleBit = unstyled.substring(from, to);
250
+        int start = from;
251
+
252
+        while (!stipControlCodes(styled.substring(0, start)).equals(startBit)) {
253
+            start++;
254
+        }
255
+
256
+        int end = to + start - from;
257
+
258
+        while (!stipControlCodes(styled.substring(start, end)).equals(middleBit)) {
259
+            end++;
260
+        }
261
+
262
+        return styled.substring(start, end);
263
+    }
226 264
     
227 265
     /**
228 266
      * Applies the hyperlink styles and intelligent linking regexps to the

+ 70
- 0
test/com/dmdirc/ui/messages/StyliserIndicesTest.java View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.ui.messages;
24
+
25
+
26
+import java.util.Arrays;
27
+import java.util.List;
28
+
29
+import org.junit.Test;
30
+import org.junit.runner.RunWith;
31
+import org.junit.runners.Parameterized;
32
+import static org.junit.Assert.*;
33
+
34
+@RunWith(Parameterized.class)
35
+public class StyliserIndicesTest {
36
+
37
+    protected String input, output;
38
+    protected int start, end;
39
+
40
+    public StyliserIndicesTest(String input, int start, int end, String output) {
41
+        this.input = input;
42
+        this.start = start;
43
+        this.end = end;
44
+        this.output = output;
45
+    }
46
+       
47
+    @Test
48
+    public void testStyle() {
49
+        assertEquals(output, Styliser.getStyledText(input, start, end));
50
+    }
51
+    
52
+    @Parameterized.Parameters
53
+    public static List<Object[]> data() {
54
+        final Object[][] tests = {
55
+            // No style
56
+            {"Blah blah blah", 0, 1, "B"},
57
+            {"Blah blah blah", 0, 4, "Blah"},
58
+            {Styliser.CODE_BOLD + "Blah blah blah", 0, 4, Styliser.CODE_BOLD + "Blah"},
59
+            {Styliser.CODE_BOLD + "Bl" + Styliser.CODE_BOLD + "ah blah blah",
60
+                     0, 4, Styliser.CODE_BOLD + "Bl" + Styliser.CODE_BOLD + "ah"},
61
+            {"Blah" + Styliser.CODE_BOLD + " blah blah",
62
+                     0, 4, "Blah"},
63
+            {"Blah" + Styliser.CODE_COLOUR + "4,0RED blah blah",
64
+                     4, 7, Styliser.CODE_COLOUR + "4,0RED"},
65
+        };
66
+
67
+        return Arrays.asList(tests);
68
+    } 
69
+
70
+}

Loading…
Cancel
Save