summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-09-08 13:07:31 +0000
committerGeoffrey Washburn <geoffrey.washburn@epfl.ch>2008-09-08 13:07:31 +0000
commit2bd07f72643799391e38805da617565a82f02b53 (patch)
tree88958b73711873298415a1e350b0a66e28e1a39f /src/library
parentab1c93a7bd2748c702137836be1bc0ad1be867c0 (diff)
downloadscala-2bd07f72643799391e38805da617565a82f02b53.tar.gz
scala-2bd07f72643799391e38805da617565a82f02b53.tar.bz2
scala-2bd07f72643799391e38805da617565a82f02b53.zip
Bugfix and tests for #1323.
Diffstat (limited to 'src/library')
-rw-r--r--src/library/scala/Seq.scala72
1 files changed, 52 insertions, 20 deletions
diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala
index dcfc2ad451..211b737c04 100644
--- a/src/library/scala/Seq.scala
+++ b/src/library/scala/Seq.scala
@@ -434,11 +434,21 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
true
}
- /** @return true if this sequence start with that sequences
- * @see String.startsWith
+ /**
+ * Checks whether the argument sequence is contained at the
+ * specified index within the receiver object.
+ *
+ * If the both the receiver object, <code>this</code> and
+ * the argument, <code>that</code> are infinite sequences
+ * this method may not terminate.
+ *
+ * @return true if <code>that</code> is contained in
+ * <code>this</code>, at the specified index, otherwise false
+ *
+ * @see String.startsWith
*/
- def startsWith[B](that: Seq[B]): Boolean = {
- val i = elements
+ def startsWith[B](that: Seq[B], offset: Int): Boolean = {
+ val i = elements.drop(offset)
val j = that.elements
var result = true
while (j.hasNext && i.hasNext && result)
@@ -446,6 +456,17 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
result && !j.hasNext
}
+ /**
+ * Check whether the receiver object starts with the argument sequence.
+ *
+ * @return true if <code>that</code> is a prefix of <code>this</code>,
+ * otherwise false
+ *
+ * @see Seq.startsWith
+ */
+ def startsWith[B](that: Seq[B]): Boolean = startsWith(that, 0)
+
+
/** @return true if this sequence end with that sequence
* @see String.endsWith
*/
@@ -459,25 +480,36 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Collection[A] {
result && !j.hasNext
}
- /** @return -1 if <code>that</code> not contained in this, otherwise the
- * index where <code>that</code> is contained
- * @see String.indexOf
+ /**
+ * Searches for the argument sequence in the receiver object, returning
+ * the smallest index where a match occurs.
+ *
+ * If the receiver object, <code>this</code>, is an infinite sequence
+ * this method will not terminate if there is no match. Similarly, if
+ * the both the receiver object and the argument, <code>that</code> are
+ * infinite sequences this method will not terminate.
+ *
+ * Because both the receiver object and the argument can both potentially
+ * be an infinite sequences, we do not attempt to use an optimized
+ * searching algorithm. Therefore, the running time will be proportional
+ * to the length of the receiver object and the argument. Subclasses and
+ * traits can potentially provide an optimized implementation.
+ *
+ * @return -1 if <code>that</code> not contained in <code>this</code>,
+ * otherwise the smallest index where <code>that</code> is found.
+ *
+ * @see String.indexOf
*/
def indexOf[B >: A](that: Seq[B]): Int = {
- val i = this.elements
- var idx = 0
- var j = that.elements
- var jdx = -1
- while (i.hasNext && j.hasNext) {
- idx = idx + 1
- if (i.next != j.next) {
- j = that.elements
- jdx = -1
- } else if (jdx == -1) {
- jdx = idx - 1
- }
+ val e = this.elements
+ // Offset into e
+ var i = 0
+ if (that.isEmpty) return 0
+ while (e.hasNext) {
+ if (this.startsWith(that, i)) return i
+ e.next; i += 1
}
- jdx
+ -1
}
/** Is <code>that</code> a slice in this?