Skip to content

Commit d04d5a4

Browse files
Christopher-Hermannjukzi
authored andcommitted
[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 60ef7e9 commit d04d5a4

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
@@ -5145,16 +5145,65 @@ String wrapText (String text, long handle, int width) {
51455145
}
51465146

51475147
static String withCrLf (String string) {
5148-
if (string == null) {
5149-
return string;
5148+
/* Create a new string with the CR/LF line terminator. */
5149+
int i = 0;
5150+
int length = string.length();
5151+
StringBuilder result = new StringBuilder (length);
5152+
while (i < length) {
5153+
int j = string.indexOf ('\n', i);
5154+
if (j > 0 && string.charAt(j - 1) == '\r') {
5155+
result.append(string.substring(i, j + 1));
5156+
i = j + 1;
5157+
} else {
5158+
if (j == -1) j = length;
5159+
result.append (string.substring (i, j));
5160+
if ((i = j) < length) {
5161+
result.append ("\r\n"); //$NON-NLS-1$
5162+
i++;
5163+
}
5164+
}
51505165
}
5151-
// Replace \r\n, \r, or \n with \r\n
5152-
return string.replaceAll("(\r\n|\r|\n)", "\r\n");
5166+
5167+
/* Avoid creating a copy of the string if it has not changed */
5168+
if (string.length()== result.length()) return string;
5169+
return result.toString ();
51535170
}
51545171

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

51605209
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)