Skip to content

Commit acfa734

Browse files
Display.withCrLf() is producing wrong line breaks on Windows
Replacing all line breaks on windows with CrLf using regex, which is much safer. Fixes #1557
1 parent 6104592 commit acfa734

File tree

2 files changed

+16
-74
lines changed

2 files changed

+16
-74
lines changed

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 5 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5152,85 +5152,16 @@ String wrapText (String text, long handle, int width) {
51525152
}
51535153

51545154
static String withCrLf (String string) {
5155-
5156-
/* If the string is empty, return the string. */
5157-
int length = string.length ();
5158-
if (length == 0) return string;
5159-
5160-
/*
5161-
* Check for an LF or CR/LF and assume the rest of
5162-
* the string is formated that way. This will not
5163-
* work if the string contains mixed delimiters.
5164-
*/
5165-
int i = string.indexOf ('\n', 0);
5166-
if (i == -1) return string;
5167-
if (i > 0 && string.charAt (i - 1) == '\r') {
5155+
if (string == null) {
51685156
return string;
51695157
}
5170-
5171-
/*
5172-
* The string is formatted with LF. Compute the
5173-
* number of lines and the size of the buffer
5174-
* needed to hold the result
5175-
*/
5176-
i++;
5177-
int count = 1;
5178-
while (i < length) {
5179-
if ((i = string.indexOf ('\n', i)) == -1) break;
5180-
count++; i++;
5181-
}
5182-
count += length;
5183-
5184-
/* Create a new string with the CR/LF line terminator. */
5185-
i = 0;
5186-
StringBuilder result = new StringBuilder (count);
5187-
while (i < length) {
5188-
int j = string.indexOf ('\n', i);
5189-
if (j == -1) j = length;
5190-
result.append (string.substring (i, j));
5191-
if ((i = j) < length) {
5192-
result.append ("\r\n"); //$NON-NLS-1$
5193-
i++;
5194-
}
5195-
}
5196-
return result.toString ();
5158+
// Replace \r\n, \r, or \n with \r\n return input.replaceAll("(\r\n|\r|\n)", "\r\n");
5159+
return string.replaceAll("(\r\n|\r|\n)", "\r\n");
51975160
}
51985161

51995162
static char [] withCrLf (char [] string) {
5200-
/* If the string is empty, return the string. */
5201-
int length = string.length;
5202-
if (length == 0) return string;
5203-
5204-
/*
5205-
* Check for an LF or CR/LF and assume the rest of
5206-
* the string is formated that way. This will not
5207-
* work if the string contains mixed delimiters.
5208-
* Also, compute the number of lines.
5209-
*/
5210-
int count = 0;
5211-
for (int i = 0; i < string.length; i++) {
5212-
if (string [i] == '\n') {
5213-
count++;
5214-
if (count == 1 && i > 0 && string [i - 1] == '\r') return string;
5215-
}
5216-
}
5217-
if (count == 0) return string;
5218-
5219-
/*
5220-
* The string is formatted with LF.
5221-
*/
5222-
count += length;
5223-
5224-
/* Create a new string with the CR/LF line terminator. */
5225-
char [] result = new char [count];
5226-
for (int i = 0, j = 0; i < length && j < count; i++) {
5227-
if (string [i] == '\n') {
5228-
result [j++] = '\r';
5229-
}
5230-
result [j++] = string [i];
5231-
}
5232-
5233-
return result;
5163+
String withCrLf = withCrLf(new String(string));
5164+
return withCrLf.toCharArray();
52345165
}
52355166

52365167
static boolean isActivateShellOnForceFocus() {

tests/org.eclipse.swt.tests.win32/JUnit Tests/org/eclipse/swt/tests/win32/widgets/Test_org_eclipse_swt_widgets_Display.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.swt.tests.win32.widgets;
1515

16+
import static org.junit.Assert.assertEquals;
17+
1618
import java.lang.reflect.InvocationTargetException;
1719
import java.lang.reflect.Method;
1820

@@ -35,4 +37,13 @@ public void test_isXMouseActive() throws NoSuchMethodException, SecurityExceptio
3537
display.dispose();
3638
}
3739
}
40+
41+
@Test
42+
public void test_mixedLfAndCrfl() {
43+
String actString = Display.withCrLf("First Line \n second line \r\n third line");
44+
assertEquals("First Line \r\n second line \r\n third line", actString);
45+
46+
char[] actCharArray = Display.withCrLf("First Line \n second line \r\n third line".toCharArray());
47+
assertEquals("First Line \r\n second line \r\n third line", new String(actCharArray));
48+
}
3849
}

0 commit comments

Comments
 (0)