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

[lang] replace() length calculation improvement

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.1
    • 2.2
    • None
    • None
    • Operating System: other
      Platform: Other

    • 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

        1. ASF.LICENSE.NOT.GRANTED--PerfTest.java
          0.6 kB
          Henri Yandell
        2. ASF.LICENSE.NOT.GRANTED--PerfTest.java
          0.5 kB
          Henri Yandell

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: