Skip to content

Temp Char allocations #1

@andrews-unity

Description

@andrews-unity

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions