"newline to br" metode - forbedringer?
Jeg har lige lavet en metode der omsaetter newline (#13#10) til <br>, saa man kan faa de rigtige lineskift, hvis teksten bliver printet ud i en browser.Jeg ville vaere glad, hvis folk kunne komme med forslag til optimeringer, da det er meningen metoden skal performe saa hurtigt som muligt.
/**
* Converts all newlines (#13#10) in a text to <br> tags. This makes it
* possible to print the text in a HTML document with the actual line
* breaks.
*
* @param text String with text which should be <br>'arised.
* @return String with text containing <br> tags instead of actual new
* lines or null if something went wrong.
*
*/
public synchronized static String newLineToBR(String text)
{
if (text == null)
{
return null;
}
int len = text.length();
char[] buffer = new char[len];
text.getChars(0, len, buffer, 0);
int i = 0;
while (i < len-1)
{
if ( (buffer[i] == '\u0013') && (buffer[i+1] == '\u0010') )
{
// replace with '<br>' - push array chars a couple of chars
len += 2;
char[] tmp = new char[len];
// put in start of array
System.arraycopy(buffer, 0, tmp, 0, i);
// apply <br> text
tmp[i] = '<';
tmp[i+1] = 'b';
tmp[i+2] = 'r';
tmp[i+3] = '>';
// don't have to check our newly inserted text
i += 4;
// put in rest of array
System.arraycopy(buffer, i-2, tmp, i, len-i);
// use new array
buffer = tmp;
}
else
{
i++;
}
}
return new String(buffer);
} // > public synchronized static String newLineToBR(String text)
