Browse Source

Implement new SplitLine method in WritableFrameContainer

Fixes issue 2071, fixes issue 2070
tags/0.6.3m1rc1
Chris Smith 15 years ago
parent
commit
e20b9f68f6

+ 4
- 15
src/com/dmdirc/Channel.java View File

@@ -163,30 +163,19 @@ public final class Channel extends MessageTarget
163 163
             return;
164 164
         }
165 165
 
166
-        if (line.indexOf('\n') > -1) {
167
-            for (String part : line.split("\n")) {
168
-                sendLine(part);
169
-            }
170
-
171
-            return;
172
-        }
173
-
174 166
         final ClientInfo me = server.getParser().getMyself();
175 167
         final String[] details = getDetails(channelInfo.getUser(me), showColours);
176 168
 
177
-        if (line.length() <= getMaxLineLength()) {
169
+        for (String part : splitLine(window.getTranscoder().encode(line))) {
178 170
             final StringBuffer buff = new StringBuffer("channelSelfMessage");
179 171
 
180 172
             ActionManager.processEvent(CoreActionType.CHANNEL_SELF_MESSAGE, buff,
181
-                    this, channelInfo.getUser(me), line);
173
+                    this, channelInfo.getUser(me), part);
182 174
 
183 175
             addLine(buff, details[0], details[1], details[2], details[3],
184
-                    window.getTranscoder().encode(line), channelInfo);
176
+                    part, channelInfo);
185 177
 
186
-            channelInfo.sendMessage(window.getTranscoder().encode(line));
187
-        } else {
188
-            sendLine(line.substring(0, getMaxLineLength()));
189
-            sendLine(line.substring(getMaxLineLength()));
178
+            channelInfo.sendMessage(part);
190 179
         }
191 180
     }
192 181
 

+ 5
- 15
src/com/dmdirc/Query.java View File

@@ -136,28 +136,18 @@ public final class Query extends MessageTarget implements
136 136
             return;
137 137
         }
138 138
 
139
-        if (line.indexOf('\n') > -1) {
140
-            for (String part : line.split("\n")) {
141
-                sendLine(part);
142
-            }
143
-
144
-            return;
145
-        }
146
-
147 139
         final ClientInfo client = server.getParser().getMyself();
148 140
 
149
-        if (line.length() <= getMaxLineLength()) {
150
-            server.getParser().sendMessage(ClientInfo.parseHost(host), window.getTranscoder().encode(line));
141
+        for (String part : splitLine(window.getTranscoder().encode(line))) {
142
+            server.getParser().sendMessage(ClientInfo.parseHost(host),
143
+                    part);
151 144
 
152 145
             final StringBuffer buff = new StringBuffer("querySelfMessage");
153 146
 
154
-            ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, line);
147
+            ActionManager.processEvent(CoreActionType.QUERY_SELF_MESSAGE, buff, this, part);
155 148
 
156 149
             addLine(buff, client.getNickname(), client.getIdent(),
157
-                    client.getHost(), window.getTranscoder().encode(line));
158
-        } else {
159
-            sendLine(line.substring(0, getMaxLineLength()));
160
-            sendLine(line.substring(getMaxLineLength()));
150
+                    client.getHost(), part);
161 151
         }
162 152
     }
163 153
 

+ 34
- 0
src/com/dmdirc/WritableFrameContainer.java View File

@@ -81,6 +81,40 @@ public abstract class WritableFrameContainer extends FrameContainer {
81 81
      * @return The maximum line length for this container
82 82
      */
83 83
     public abstract int getMaxLineLength();
84
+
85
+    /**
86
+     * Splits the specified line into chunks that contain a number of bytes
87
+     * less than or equal to the value returned by {@link #getMaxLineLength()}.
88
+     *
89
+     * @param line The line to be split
90
+     * @return An ordered list of chunks of the desired length
91
+     */
92
+    protected List<String> splitLine(final String line) {
93
+        final List<String> result = new ArrayList<String>();
94
+
95
+        if (line.indexOf('\n') > -1) {
96
+            for (String part : line.split("\n")) {
97
+                result.addAll(splitLine(part));
98
+            }
99
+        } else {
100
+            final StringBuilder remaining = new StringBuilder(line);
101
+
102
+            while (remaining.toString().getBytes().length > getMaxLineLength()) {
103
+                int number = Math.min(remaining.length(), getMaxLineLength());
104
+
105
+                while (remaining.substring(0, number).getBytes().length > getMaxLineLength()) {
106
+                    number--;
107
+                }
108
+
109
+                result.add(remaining.substring(0, number));
110
+                remaining.delete(0, number);
111
+            }
112
+
113
+            result.add(remaining.toString());
114
+        }
115
+
116
+        return result;
117
+    }
84 118
     
85 119
     /**
86 120
      * Returns the number of lines that the specified string would be sent as.

+ 24
- 3
test/com/dmdirc/WritableFrameContainerTest.java View File

@@ -23,10 +23,8 @@
23 23
 package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.harness.TestWritableFrameContainer;
26
-import com.dmdirc.config.ConfigManager;
27
-import com.dmdirc.config.IdentityManager;
28
-import com.dmdirc.ui.interfaces.InputWindow;
29 26
 
27
+import java.util.Arrays;
30 28
 import org.junit.Test;
31 29
 import static org.junit.Assert.*;
32 30
 
@@ -52,4 +50,27 @@ public class WritableFrameContainerTest extends junit.framework.TestCase {
52 50
         assertEquals(2, res2c);        
53 51
     }
54 52
 
53
+    @Test
54
+    public void testSplitLine() {
55
+        final WritableFrameContainer container10 = new TestWritableFrameContainer(10);
56
+        final String[][][] tests = new String[][][]{
57
+            {{""}, {""}},
58
+            {{"0123456789"}, {"0123456789"}},
59
+            {{"01234567890"}, {"0123456789", "0"}},
60
+            {{"012345678→"}, {"012345678","→"}},
61
+            {{"0123456→"}, {"0123456→"}},
62
+            {{"01→2345678"}, {"01→23456","78"}},
63
+            {{"01→23456\n78"}, {"01→23456","78"}},
64
+            {{"01\n→2345678"}, {"01","→2345678"}},
65
+            {{"→→→00"}, {"→→→0", "0"}},
66
+        };
67
+
68
+        for (String[][] test : tests) {
69
+            final String[] res = container10.splitLine(test[0][0]).toArray(new String[0]);
70
+            assertTrue("'" + test[0][0] + "' → "
71
+                    + Arrays.toString(res) + " (expected: " + Arrays.toString(test[1]) + ")",
72
+                    Arrays.equals(res, test[1]));
73
+        }
74
+    }
75
+
55 76
 }

Loading…
Cancel
Save