(patch) add direct support for vector encoding #3

Merged
pfm merged 1 commit from vector-support into main 2026-05-12 02:56:27 +00:00
4 changed files with 22 additions and 3 deletions
Showing only changes of commit fbd0b73be3 - Show all commits

View file

@ -9,6 +9,7 @@ import java.util.Base64
* Supports base64-url encoding as well.
*/
object Base64Encoder extends Encoder[B64]:
private lazy val e: Base64.Encoder = Base64.getEncoder()
private lazy val eu: Base64.Encoder = Base64.getUrlEncoder()
@ -17,6 +18,11 @@ object Base64Encoder extends Encoder[B64]:
override def encode(input: Array[Byte]): B64 =
B64(e.encodeToString(input))
/** @inheritDocs
*/
override def encode(input: Vector[Byte]): B64 =
B64(e.encodeToString(input.toArray))
/** Encode the given bytes using base64-url.
*
* @param input

View file

@ -1,7 +1,5 @@
package gs.std.v0.core
import java.util.Base64
/** Represents a blob -- some array of bytes.
*
* @param data
@ -46,4 +44,4 @@ final class Blob(private val data: Array[Byte]) extends IndexedSeq[Byte]:
/** @return
* This byte array, encoded as a base64 string.
*/
def base64(): String = Base64.getEncoder().encodeToString(data)
def base64(): B64 = Base64Encoder.encode(data)

View file

@ -15,6 +15,15 @@ trait Encoder[+A <: EncodedString]:
*/
def encode(input: Array[Byte]): A
/** Encode an immutable array of bytes as a string.
*
* @param input
* The bytes to encode.
* @return
* The encoded string.
*/
def encode(input: Vector[Byte]): A
/** Encode a string as a string.
*
* @param input

View file

@ -5,9 +5,15 @@ import java.util.HexFormat
/** Implementation of [[Encoder]] for Hexadecimal strings.
*/
object HexEncoder extends Encoder[Hex]:
private lazy val h: HexFormat = HexFormat.of()
/** @inheritDocs
*/
override def encode(input: Array[Byte]): Hex =
Hex(h.formatHex(input))
/** @inheritDocs
*/
override def encode(input: Vector[Byte]): Hex =
Hex(h.formatHex(input.toArray))