wotakuro/StringBuilderTemporary

Temp Char allocations

Opened this issue · 1 comments

Currently in the ToUpper/ToLower/Trim functions there is a small char allocation for an array each time these functions are called, which is not required. The following code could be replaced:

from:

public Sbt ToLower()
{
            char[] tmp = new char[1];
            int length = sb.Length;
            for (int i = 0; i < length; ++i)
            {
                sb.CopyTo(i, tmp, 0, 1);
                if (char.IsUpper(tmp[0]))
                {
                    sb.Replace(tmp[0], char.ToLower(tmp[0]), i, 1);
                }
            }
            return this;
}

to:

public Sbt ToLower ()
{
      int length = sb.Length;
      for (int i = 0; i < length; ++i) {
        char x = sb[i];
        if (char.IsUpper (x)) {
          sb[i] = char.ToLower (x);
        }
      }
      return this;
}

This can be true of all these functions, and its slightly faster than the previous cases.

Thanks for your information.

I fixed on issue branch. I will merge to master, then close this issue.