Commons Lang
  1. Commons Lang
  2. LANG-99

new StringUtils.replaceChars behaves differently from old CharSetUtils.translate

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0
    • Fix Version/s: 2.1
    • Component/s: lang.*
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

      Description

      The CharSetUtils.translate(String str, String searchChars, String replaceChars)
      method from Lang 1.0.1 is marked deprecated, pointing me at
      StringUtils.replaceChars(String str, String searchChars, String replaceChars)
      from Lang 2.0. The routines are coded quite differently, and in fact yield
      different outputs at times. StringUtils.replaceChars() does not translate some
      strings properly.

      String original="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
      String trans="nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM567891234";
      System.out.println(CharSetUtils.translate("d216.102oren", original, trans));
      System.out.println(StringUtils.replaceChars("d216.102oren", original, trans));

      Running this fragment gets me this:
      q651.506bera
      d141.401beea

      The first one is correct.

        Activity

        Hide
        ggregory@seagullsw.com added a comment -

        Well, this is a tricky one. I feel the basic problem is that the Javadoc
        underspecifies the behavior of both methods, so perhaps we could discuss first
        what the behavior /should be/.

        The heart of the problem is that the CharSetUtils method is a one pass map-style
        of method, while the StringUtils method is not, it uses a nested loop, which is
        somewhat contradictory to the Javadoc comment of doing the work "in one go".

        Show
        ggregory@seagullsw.com added a comment - Well, this is a tricky one. I feel the basic problem is that the Javadoc underspecifies the behavior of both methods, so perhaps we could discuss first what the behavior /should be/. The heart of the problem is that the CharSetUtils method is a one pass map-style of method, while the StringUtils method is not, it uses a nested loop, which is somewhat contradictory to the Javadoc comment of doing the work "in one go".
        Hide
        Loren Siebert added a comment -

        This sort of translate functionality exists in other languages, too. Perl and
        PHP both have them, and they work the way that translate() worked in 1.0.1.
        Regardless of what the new replaceChars() method is trying to do, it would be
        nice to have something backwards compatible with translate() that shares common
        behavior with similar methods in other languages.

        Show
        Loren Siebert added a comment - This sort of translate functionality exists in other languages, too. Perl and PHP both have them, and they work the way that translate() worked in 1.0.1. Regardless of what the new replaceChars() method is trying to do, it would be nice to have something backwards compatible with translate() that shares common behavior with similar methods in other languages.
        Hide
        Stephen Colebourne added a comment -

        CVS implementation now works for the supplied test case.
        ReplaceChars() differs from Translate() in the deletion behaviour when
        replaceChars is shorter than searchChars. This is documented in CharSetUtils.

        Show
        Stephen Colebourne added a comment - CVS implementation now works for the supplied test case. ReplaceChars() differs from Translate() in the deletion behaviour when replaceChars is shorter than searchChars. This is documented in CharSetUtils.
        Hide
        Henri Yandell added a comment -

        2.1 released, closing.

        Show
        Henri Yandell added a comment - 2.1 released, closing.

          People

          • Assignee:
            Unassigned
            Reporter:
            Loren Siebert
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development