From 8a0354348d1f022908eaeecc162ea4aeb24ba910 Mon Sep 17 00:00:00 2001 From: iximeow Date: Fri, 21 Nov 2014 23:04:39 -0800 Subject: tests 'n stuff --- src/utils/ByteUtils.scala | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/utils/ByteUtils.scala b/src/utils/ByteUtils.scala index 344f6f7..85c6058 100644 --- a/src/utils/ByteUtils.scala +++ b/src/utils/ByteUtils.scala @@ -62,20 +62,30 @@ object ByteUtils { } } - implicit class RichArrayOfBytes(b: Array[Byte]) { + implicit class RichIterableByte(iter: Iterable[Byte]) { def to[T : SizedNumeric : BitOps]: T = { - val numeric = implicitly[SizedNumeric[T]] - if(b.length < numeric.byteSize) { - throw new RuntimeException("Byte input is not long enough") - } + // Need to know length, so we must force the iter + iter.hasDefiniteSize match { + case true => throw new IllegalArgumentException("Argument is not finite!") + case false => { + val numeric = implicitly[SizedNumeric[T]] + if(b.length < numeric.byteSize) { + throw new RuntimeException("Byte input is not long enough") + } - var out: Long = b(0) - for(i <- 1 until numeric.byteSize) { - out << 8 - out = b(i) | out + var out: Long = b(0) + for(i <- 1 until numeric.byteSize) { + out << 8 + out = b(i) | out + } + numeric.fromLong(out) + } } - numeric.fromLong(out) } + + def xor(other: Iterable[Byte]): Iterable[Byte] = + iter.zip(other).map(_ ^@ _) + } def sizedNumeric[T : Manifest](bytes: Int)(from: Long => T)(to: T => Long) = new SizedNumeric[T] { def manifest = implicitly[Manifest[T]] @@ -106,4 +116,8 @@ object ByteUtils { } buf.toString() } + + def toHexString[T : SizedNumeric : BitOps](x: Iterable[T]): String = { + + } } -- cgit v1.1