Skip to content

Commit

Permalink
ByteSequence toHex
Browse files Browse the repository at this point in the history
  • Loading branch information
Zschimmer committed Sep 22, 2023
1 parent 34be720 commit 269489c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ with Monoid[ByteSeq] with Eq[ByteSeq] with Show[ByteSeq]
iterator(byteSeq).take(n).grouped(4).map(_.map(o => f"$o%02x").mkString).mkString(" ") +
((withEllipsis || lengthIs(byteSeq) > n) ?? "...")

def toHex(byteSeq: ByteSeq): String =
iterator(byteSeq).map(o => f"$o%02x").mkString

def nonEmpty(byteSeq: ByteSeq): Boolean =
!isEmpty(byteSeq)

Expand Down Expand Up @@ -270,6 +273,9 @@ object ByteSequence
def toHexRaw(n: Int = Int.MaxValue, withEllipsis: Boolean = false) =
typeClassInstance.toHexRaw(self, n, withEllipsis)

def toHex: String =
typeClassInstance.toHex(self)

def nonEmpty: Boolean =
typeClassInstance.nonEmpty(self)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ extends OurTestSuite
"61626364 65666768 696a6b6c 6d6e6f70 71727374 75767778 797a4142 43444546 4748494a 4b4c4d4e 4f505152 53545556 5758595a")
}

"toHex" in {
assert(ByteSeq("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toHex ==
"6162636465666768696a6b6c6d6e6f707172737475767778797a4142434445464748494a4b4c4d4e4f505152535455565758595a")
}

"headOption" in {
val byteSeq = ByteSeq("ab")
assert(byteSeq.headOption == Some('a'.toByte))
Expand Down

0 comments on commit 269489c

Please sign in to comment.