This page is no longer maintained — Please continue to the home page at www.scala-lang.org

2.8 RC7 RichString bug?

7 replies
Rodrigo Cano
Joined: 2009-03-22,
User offline. Last seen 42 years 45 weeks ago.
According to the documentation of drop in traversable:

Selects all elements except first n ones.

Note: might return different results for different runs, unless the underlying collection type is ordered.

n

the number of elements to drop from this collection.

returns

a collection consisting of all elements of this collection except the first n ones, or else the empty collection, if this collection has less than n elements.

definition classes: TraversableLike

The return is a collection with the first n ones, or else the empty collection. This holds true for example for list:

scala> List(1).drop(1)
res0: List[Int] = List()

scala> List(1).drop(1).drop(1)
res1: List[Int] = List()

scala> List(1).drop(1).drop(1).drop(1)
res2: List[Int] = List()

But when I tried it out on a string:

scala> "a".drop(1)
res3: String =   

scala> "a".drop(1).drop(1)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.substring(String.java:1937)                
        at scala.collection.immutable.StringOps.slice(StringOps.scala:40)
        at scala.collection.immutable.StringOps.slice(StringOps.scala:31)
        at scala.collection.IndexedSeqOptimized$class.drop(IndexedSeqOptimized.scala:145)
        at scala.collection.immutable.StringOps.drop(StringOps.scala:31)
        at .<init>(<console>:6)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:9)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$scala_repl_result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcc...


:( , this is a bug right?

Cheers.
Rodrigo Cano
Joined: 2009-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: 2.8 RC7 RichString bug?
Sorry, it's not RichString, it's StringOps bug.

On Mon, Jul 5, 2010 at 11:59 AM, Rodrigo Cano <ioniviil@gmail.com> wrote:
According to the documentation of drop in traversable:

Selects all elements except first n ones.

Note: might return different results for different runs, unless the underlying collection type is ordered.

n

the number of elements to drop from this collection.

returns

a collection consisting of all elements of this collection except the first n ones, or else the empty collection, if this collection has less than n elements.

definition classes: TraversableLike

The return is a collection with the first n ones, or else the empty collection. This holds true for example for list:

scala> List(1).drop(1)
res0: List[Int] = List()

scala> List(1).drop(1).drop(1)
res1: List[Int] = List()

scala> List(1).drop(1).drop(1).drop(1)
res2: List[Int] = List()

But when I tried it out on a string:

scala> "a".drop(1)
res3: String =   

scala> "a".drop(1).drop(1)
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.substring(String.java:1937)                
        at scala.collection.immutable.StringOps.slice(StringOps.scala:40)
        at scala.collection.immutable.StringOps.slice(StringOps.scala:31)
        at scala.collection.IndexedSeqOptimized$class.drop(IndexedSeqOptimized.scala:145)
        at scala.collection.immutable.StringOps.drop(StringOps.scala:31)
        at .<init>(<console>:6)
        at .<clinit>(<console>)
        at RequestResult$.<init>(<console>:9)
        at RequestResult$.<clinit>(<console>)
        at RequestResult$scala_repl_result(<console>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcc...


:( , this is a bug right?

Cheers.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: 2.8 RC7 RichString bug?

On Mon, Jul 05, 2010 at 12:04:57PM -0300, Rodrigo Cano wrote:
> Sorry, it's not RichString, it's StringOps bug.

I fixed it.

http://lampsvn.epfl.ch/trac/scala/changeset/22489

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: 2.8 RC7 RichString bug?

And if you happen to look at it and find something... disturbing... be
aware there's another patch right on its heels.

Rodrigo Cano
Joined: 2009-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: 2.8 RC7 RichString bug?
Awsome :D

Stefan Langer
Joined: 2009-10-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: 2.8 RC7 RichString bug?

You should become an exterminator ;)

2010/7/5 Paul Phillips :
> And if you happen to look at it and find something... disturbing... be
> aware there's another patch right on its heels.
>
> --
> Paul Phillips      | We must respect the other fellow's religion, but only
> Moral Alien        | in the sense and to the extent that we respect his
> Empiricist         | theory that his wife is beautiful and his children smart.
> all hip pupils!    |     -- H. L. Mencken
>

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Re: 2.8 RC7 RichString bug?

On Mon, Jul 5, 2010 at 6:01 PM, Paul Phillips wrote:
> On Mon, Jul 05, 2010 at 12:04:57PM -0300, Rodrigo Cano wrote:
>> Sorry, it's not RichString, it's StringOps bug.
>
> I fixed it.
>
> http://lampsvn.epfl.ch/trac/scala/changeset/22489
>
Actually, that's the wrong way. slice is defined to be forgiving for
bounds out of range. So the added tests are redundant and confusing
because they hide this design decision. What's wrong is
StringOps.slice, which needs to correct out of range bounds.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Re: 2.8 RC7 RichString bug?

On Wed, Jul 14, 2010 at 02:18:17AM +0200, martin odersky wrote:
> Actually, that's the wrong way. slice is defined to be forgiving for
> bounds out of range. So the added tests are redundant and confusing
> because they hide this design decision. What's wrong is
> StringOps.slice, which needs to correct out of range bounds.

Shoot, I wrote that specifically thinking that slice is unforgiving.
That's quite a handy thing to know, maybe I'll use it more. I'll
correct it.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland