Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-162

[lang] replace() length calculation improvement

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.2
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

    • Bugzilla Id:
      39368

      Description

      static String replace(String text, String repl, String with, int max)

      PROBLEM:
      This method incurs the calculation of the search string ("repl") every time a
      match is found.

      int start = 0, end = 0;
      while ((end = text.indexOf(repl, start)) != -1)

      { buf.append(text.substring(start, end)).append(with); start = end + repl.length(); ... }

      PROPOSED SOLUTION:
      Move it out of the loop.

      int start = 0, end = 0, replLen = repl.length();
      while ((end = text.indexOf(repl, start)) != -1)

      { buf.append(text.substring(start, end)).append(with); start = end + replLen; ... }

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              pbenedict Paul Benedict
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: