3

I have a function that fixed non-printable characters in C# for JavaScript. But it works very slow! How to increase speed of this function?

private static string JsStringFixNonPrintable(string Source)
    {
        string Result = "";
        for (int Position = 0; Position < Source.Length; ++Position)
        {
            int i = Position;
            var CharCat = char.GetUnicodeCategory(Source, i);
            if (Char.IsWhiteSpace(Source[i]) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator ||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { Result += " "; continue; }
            if (Char.IsControl(Source[i]) && Source[i] != 10 && Source[i] != 13) continue;
            Result += Source[i];
        }
        return Result;
    }
bobbymcr
  • 23,769
  • 3
  • 56
  • 67
Vadim
  • 31
  • 1
  • I'd check out http://stackoverflow.com/questions/1120198/most-efficient-way-to-remove-special-characters-from-string/1120248#1120248 for a regexp method as an alternative. – James Khoury Jun 17 '11 at 03:59

3 Answers3

5

I have recoded your snippet of code using StringBuilder class, with predefined buffer size... that is much faster than your sample.

    private static string JsStringFixNonPrintable(string Source)
    {
        StringBuilder builder = new StringBuilder(Source.Length); // predefine size to be the same as input
        for (int it = 0; it < Source.Length; ++it)
        {
            var ch = Source[it];
            var CharCat = char.GetUnicodeCategory(Source, it);
            if (Char.IsWhiteSpace(ch) ||
                CharCat == System.Globalization.UnicodeCategory.LineSeparator ||
                CharCat == System.Globalization.UnicodeCategory.SpaceSeparator) { builder.Append(' '); continue; }
            if (Char.IsControl(ch) && ch != 10 && ch != 13) continue;
            builder.Append(ch);
        }
        return builder.ToString();
    }
Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
1

Instead of concatenating to the string, try using System.Text.StringBuilder which internally maintains a character buffer and does not create a new object every time you append.

Example:

StringBuilder sb = new StringBuilder();
sb.Append('a');
sb.Append('b');
sb.Append('c');
string result = sb.ToString();
Console.WriteLine(result); // prints 'abc'
bobbymcr
  • 23,769
  • 3
  • 56
  • 67
0

Use Stringbuilder

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

and replace characters in-place, that should speed up things

Adithya Surampudi
  • 4,354
  • 1
  • 17
  • 17