classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cp-patches] Patch: 2 API methods implemented in java.lang.String


From: Tom Tromey
Subject: Re: [cp-patches] Patch: 2 API methods implemented in java.lang.String
Date: 07 Nov 2005 12:39:48 -0700
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

>>>>> "Tony" == Anthony Balkissoon <address@hidden> writes:

Tony> +  public boolean contains (CharSequence s)
Tony> +  {
Tony> +    return this.indexOf(s.toString()) != -1;
Tony> +  }

Generally when working with CharSequence I've been assuming that
converting one to a String is a relatively expensive operation, and
that this is why it is a separate interface.

Tony> +  public String replace (CharSequence target, CharSequence replacement)
Tony> +  {
Tony> +    String result = this;
Tony> +    int pos = result.indexOf(target.toString());
Tony> +    while (pos != -1)
Tony> +      {
Tony> +        result = result.substring(0, pos) + replacement.toString()
Tony> +                 + result.substring(pos + target.length(), 
result.length());
Tony> +        pos = result.indexOf(target.toString());
Tony> +      }
Tony> +    return result;
Tony> +  }
Tony>  }

This is inefficient.  For one thing, target is converted to a String
twice.  For another, it creates a lot of garbage if there is more than
one match.

It is also incorrect, consider what happens if the replacement string
contains the target string.

Tom




reply via email to

[Prev in Thread] Current Thread [Next in Thread]