Skip to content

Commit

Permalink
Display.withCrLf() is producing wrong line breaks on Windows
Browse files Browse the repository at this point in the history
Replacing all line breaks on windows with CrLf using regex, which is much safer.

Fixes eclipse-platform#1557
  • Loading branch information
Christopher-Hermann committed Jan 8, 2025
1 parent 6104592 commit acfa734
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5152,85 +5152,16 @@ String wrapText (String text, long handle, int width) {
}

static String withCrLf (String string) {

/* If the string is empty, return the string. */
int length = string.length ();
if (length == 0) return string;

/*
* Check for an LF or CR/LF and assume the rest of
* the string is formated that way. This will not
* work if the string contains mixed delimiters.
*/
int i = string.indexOf ('\n', 0);
if (i == -1) return string;
if (i > 0 && string.charAt (i - 1) == '\r') {
if (string == null) {
return string;
}

/*
* The string is formatted with LF. Compute the
* number of lines and the size of the buffer
* needed to hold the result
*/
i++;
int count = 1;
while (i < length) {
if ((i = string.indexOf ('\n', i)) == -1) break;
count++; i++;
}
count += length;

/* Create a new string with the CR/LF line terminator. */
i = 0;
StringBuilder result = new StringBuilder (count);
while (i < length) {
int j = string.indexOf ('\n', i);
if (j == -1) j = length;
result.append (string.substring (i, j));
if ((i = j) < length) {
result.append ("\r\n"); //$NON-NLS-1$
i++;
}
}
return result.toString ();
// Replace \r\n, \r, or \n with \r\n return input.replaceAll("(\r\n|\r|\n)", "\r\n");
return string.replaceAll("(\r\n|\r|\n)", "\r\n");
}

static char [] withCrLf (char [] string) {
/* If the string is empty, return the string. */
int length = string.length;
if (length == 0) return string;

/*
* Check for an LF or CR/LF and assume the rest of
* the string is formated that way. This will not
* work if the string contains mixed delimiters.
* Also, compute the number of lines.
*/
int count = 0;
for (int i = 0; i < string.length; i++) {
if (string [i] == '\n') {
count++;
if (count == 1 && i > 0 && string [i - 1] == '\r') return string;
}
}
if (count == 0) return string;

/*
* The string is formatted with LF.
*/
count += length;

/* Create a new string with the CR/LF line terminator. */
char [] result = new char [count];
for (int i = 0, j = 0; i < length && j < count; i++) {
if (string [i] == '\n') {
result [j++] = '\r';
}
result [j++] = string [i];
}

return result;
String withCrLf = withCrLf(new String(string));
return withCrLf.toCharArray();
}

static boolean isActivateShellOnForceFocus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.swt.tests.win32.widgets;

import static org.junit.Assert.assertEquals;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

Expand All @@ -35,4 +37,13 @@ public void test_isXMouseActive() throws NoSuchMethodException, SecurityExceptio
display.dispose();
}
}

@Test
public void test_mixedLfAndCrfl() {
String actString = Display.withCrLf("First Line \n second line \r\n third line");
assertEquals("First Line \r\n second line \r\n third line", actString);

char[] actCharArray = Display.withCrLf("First Line \n second line \r\n third line".toCharArray());
assertEquals("First Line \r\n second line \r\n third line", new String(actCharArray));
}
}

0 comments on commit acfa734

Please sign in to comment.