@@ -17,19 +17,53 @@ import scala.language.`2.13`
1717
1818// Still need this one since the implicit class ArrayCharSequence only converts
1919// a single argument.
20+ /** A `CharSequence` view of a slice of an `Array[Char]`.
21+ *
22+ * The sequence consists of the characters of `xs` from index `start` until
23+ * `end`. Characters are read from the array on demand, so later writes to
24+ * the array are visible through this sequence. The bounds are not validated
25+ * on construction: `end <= start` yields an empty sequence, and out-of-range
26+ * bounds only surface when characters are accessed.
27+ *
28+ * @param xs the underlying character array
29+ * @param start the index in `xs` of the first character of the sequence
30+ * @param end the index in `xs` one past the last character of the sequence
31+ */
2032final class ArrayCharSequence (val xs : Array [Char ], start : Int , end : Int ) extends CharSequence {
2133 // yikes
2234 // java.lang.VerifyError: (class: scala/runtime/ArrayCharSequence, method: <init> signature: ([C)V)
2335 // Constructor must call super() or this()
2436 //
2537 // def this(xs: Array[Char]) = this(xs, 0, xs.length)
2638
39+ /** Returns the number of characters in this sequence: `end - start`, or `0` if `end <= start`. */
2740 def length : Int = math.max(0 , end - start)
41+ /** Returns the character at the given index of this sequence, that is, the
42+ * character at index `start + index` of the underlying array.
43+ *
44+ * @param index the index of the character to return, from `0` to `length - 1`
45+ * @throws ArrayIndexOutOfBoundsException if `index` is negative or not less
46+ * than `length` (the exception message reports the bounds of the
47+ * underlying array, not of this sequence)
48+ */
2849 def charAt (index : Int ): Char = {
2950 if (0 <= index && index < length)
3051 xs(start + index)
3152 else throw new ArrayIndexOutOfBoundsException (s " $index is out of bounds (min 0, max ${xs.length - 1 }) " )
3253 }
54+ /** Returns a new `ArrayCharSequence` over the characters of this sequence
55+ * from index `start0` until `end0`.
56+ *
57+ * The result is a view over the same underlying array; no characters are
58+ * copied.
59+ *
60+ * @param start0 the index in this sequence of the first character of the subsequence
61+ * @param end0 the index in this sequence one past the last character of the subsequence
62+ * @return the subsequence view; empty if `end0 <= start0` (no exception is
63+ * thrown for an inverted range, unlike the `CharSequence` contract)
64+ * @throws ArrayIndexOutOfBoundsException if `start0` is negative or `end0`
65+ * is greater than `length`
66+ */
3367 def subSequence (start0 : Int , end0 : Int ): CharSequence = {
3468 if (start0 < 0 ) throw new ArrayIndexOutOfBoundsException (s " $start0 is out of bounds (min 0, max ${length - 1 }) " )
3569 else if (end0 > length) throw new ArrayIndexOutOfBoundsException (s " $end0 is out of bounds (min 0, max ${xs.length - 1 }) " )
@@ -40,6 +74,13 @@ final class ArrayCharSequence(val xs: Array[Char], start: Int, end: Int) extends
4074 new ArrayCharSequence (xs, start1, start1 + newlen)
4175 }
4276 }
77+ /** Returns the characters of this sequence as a `String`.
78+ *
79+ * The bounds are clamped to the underlying array before copying: a
80+ * negative `start` is treated as `0` and the end is capped at the array's
81+ * length, so a sequence constructed with out-of-range bounds yields its
82+ * in-range characters (or the empty string) rather than throwing.
83+ */
4384 override def toString () = {
4485 val start = math.max(this .start, 0 )
4586 val end = math.min(xs.length, start + length)
0 commit comments