Skip to content

Commit 1f6d70c

Browse files
[FIX] Change to old withCrLf for better performance
Changing back to the old withCrLf coding. Regex is causing performance issues. Furthermore, handling of mixed \n and \r\n is improved. Fixes: #1718
1 parent c49cee7 commit 1f6d70c

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

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

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

51545154
static String withCrLf (String string) {
5155-
if (string == null) {
5156-
return string;
5155+
/* Create a new string with the CR/LF line terminator. */
5156+
int i = 0;
5157+
int length = string.length();
5158+
StringBuilder result = new StringBuilder (length);
5159+
while (i < length) {
5160+
int j = string.indexOf ('\n', i);
5161+
if (j > 0 && string.charAt(j - 1) == '\r') {
5162+
result.append(string.substring(i, j + 1));
5163+
i = j + 1;
5164+
} else {
5165+
if (j == -1) j = length;
5166+
result.append (string.substring (i, j));
5167+
if ((i = j) < length) {
5168+
result.append ("\r\n"); //$NON-NLS-1$
5169+
i++;
5170+
}
5171+
}
51575172
}
5158-
// Replace \r\n, \r, or \n with \r\n
5159-
return string.replaceAll("(\r\n|\r|\n)", "\r\n");
5173+
5174+
/* Avoid creating a copy of the string if it has not changed */
5175+
if (string.length()== result.length()) return string;
5176+
return result.toString ();
51605177
}
51615178

51625179
static char [] withCrLf (char [] string) {
5163-
String withCrLf = withCrLf(new String(string));
5164-
return withCrLf.toCharArray();
5180+
/* If the string is empty, return the string. */
5181+
int length = string.length;
5182+
if (length == 0) return string;
5183+
5184+
/*
5185+
* Check for an LF or CR/LF and assume the rest of
5186+
* the string is formated that way. This will not
5187+
* work if the string contains mixed delimiters.
5188+
* Also, compute the number of lines.
5189+
*/
5190+
int count = 0;
5191+
for (int i = 0; i < string.length; i++) {
5192+
if (string [i] == '\n') {
5193+
count++;
5194+
if (count == 1 && i > 0 && string [i - 1] == '\r') return string;
5195+
}
5196+
}
5197+
if (count == 0) return string;
5198+
5199+
/*
5200+
* The string is formatted with LF.
5201+
*/
5202+
count += length;
5203+
5204+
/* Create a new string with the CR/LF line terminator. */
5205+
char [] result = new char [count];
5206+
for (int i = 0, j = 0; i < length && j < count; i++) {
5207+
if (string [i] == '\n') {
5208+
result [j++] = '\r';
5209+
}
5210+
result [j++] = string [i];
5211+
}
5212+
5213+
return result;
51655214
}
51665215

51675216
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,8 @@ public void test_mixedLfAndCrfl() {
6060

6161
text.setText("First Line \n second line \r\n third line");
6262
assertEquals("First Line \r\n second line \r\n third line", text.getText());
63+
64+
text.setText("First Line \n second line \r\n third line\n");
65+
assertEquals("First Line \r\n second line \r\n third line\r\n", text.getText());
6366
}
6467
}

0 commit comments

Comments
 (0)