Improved ByteString.{head,tail,last, init} and added missing tests

This commit is contained in:
Oliver Schulz 2012-06-17 22:18:51 +02:00
parent 3b67a4fdef
commit b6b117cf54
2 changed files with 6 additions and 4 deletions

View file

@ -285,7 +285,9 @@ class ByteStringSpec extends WordSpec with MustMatchers with Checkers {
} }
"calling head" in { check { a: ByteString a.isEmpty || likeVector(a) { _.head } } } "calling head" in { check { a: ByteString a.isEmpty || likeVector(a) { _.head } } }
"calling tail" in { check { a: ByteString a.isEmpty || likeVector(a) { _.tail } } }
"calling last" in { check { a: ByteString a.isEmpty || likeVector(a) { _.last } } } "calling last" in { check { a: ByteString a.isEmpty || likeVector(a) { _.last } } }
"calling init" in { check { a: ByteString a.isEmpty || likeVector(a) { _.init } } }
"calling length" in { check { a: ByteString likeVector(a) { _.length } } } "calling length" in { check { a: ByteString likeVector(a) { _.length } } }
"calling span" in { check { (a: ByteString, b: Byte) likeVector(a)({ _.span(_ != b) match { case (a, b) (a, b) } }) } } "calling span" in { check { (a: ByteString, b: Byte) likeVector(a)({ _.span(_ != b) match { case (a, b) (a, b) } }) } }

View file

@ -257,10 +257,10 @@ sealed abstract class ByteString extends IndexedSeq[Byte] with IndexedSeqOptimiz
// a parent trait. // a parent trait.
override def iterator: ByteIterator = throw new UnsupportedOperationException("Method iterator is not implemented in ByteString") override def iterator: ByteIterator = throw new UnsupportedOperationException("Method iterator is not implemented in ByteString")
@inline final override def head: Byte = this(0) override def head: Byte = apply(0)
@inline final override def tail: ByteString = this.drop(1) override def tail: ByteString = drop(1)
@inline final override def last: Byte = this(this.length - 1) override def last: Byte = apply(length - 1)
override def init: ByteString = this.take(this.length - 1) override def init: ByteString = dropRight(1)
override def slice(from: Int, until: Int): ByteString = override def slice(from: Int, until: Int): ByteString =
if ((from == 0) && (until == length)) this if ((from == 0) && (until == length)) this