Skip to content

Commit

Permalink
[FIX] Change to old withCrLf for better performance
Browse files Browse the repository at this point in the history
Changing back to the old withCrLf coding. Regex is causing performance issues.

Furthermore, handling of mixed \n and \r\n is improved.

Fixes: #1718
  • Loading branch information
Christopher-Hermann committed Jan 14, 2025
1 parent c49cee7 commit 38cec61
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5152,16 +5152,90 @@ String wrapText (String text, long handle, int width) {
}

static String withCrLf (String string) {
if (string == null) {

/* 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') {
return string;
}
// Replace \r\n, \r, or \n with \r\n
return string.replaceAll("(\r\n|\r|\n)", "\r\n");

/*
* 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 > 0 && string.charAt(j - 1) == '\r') {
result.append(string.substring(i, j + 1));
i = j + 1;
} else {
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 ();
}

static char [] withCrLf (char [] string) {
String withCrLf = withCrLf(new String(string));
return withCrLf.toCharArray();
/* 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;
}

static boolean isActivateShellOnForceFocus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ public void test_mixedLfAndCrfl() {

text.setText("First Line \n second line \r\n third line");
assertEquals("First Line \r\n second line \r\n third line", text.getText());

text.setText("First Line \n second line \r\n third line\n");
assertEquals("First Line \r\n second line \r\n third line\r\n", text.getText());
}
}

0 comments on commit 38cec61

Please sign in to comment.