provide CharSequence operations as well;
authorwenzelm
Thu, 30 Jul 2015 11:32:58 +0200
changeset 60833 d201996f72a8
parent 60832 1cdc63224ed3
child 60834 781f1168d31e
provide CharSequence operations as well;
src/Pure/General/bytes.scala
--- a/src/Pure/General/bytes.scala	Wed Jul 29 15:52:57 2015 +0200
+++ b/src/Pure/General/bytes.scala	Thu Jul 30 11:32:58 2015 +0200
@@ -59,7 +59,7 @@
 final class Bytes private(
   protected val bytes: Array[Byte],
   protected val offset: Int,
-  val length: Int)
+  val length: Int) extends CharSequence
 {
   /* equality */
 
@@ -107,6 +107,19 @@
     }
 
 
+  /* CharSequence operations */
+
+  def charAt(i: Int): Char =
+    if (0 <= i && i < length) (bytes(offset + i).asInstanceOf[Int] & 0xFF).asInstanceOf[Char]
+    else throw new IndexOutOfBoundsException
+
+  def subSequence(i: Int, j: Int): Bytes =
+  {
+    if (0 <= i && i <= j && j <= length) new Bytes(bytes, offset + i, j - i)
+    else throw new IndexOutOfBoundsException
+  }
+
+
   /* write */
 
   def write(stream: OutputStream): Unit = stream.write(bytes, offset, length)