Skip to content

Commit

Permalink
Adapt CharEncoder and CharDecoder to be an Encodable and a `Dec…
Browse files Browse the repository at this point in the history
…odable`
  • Loading branch information
propensive committed Jun 15, 2024
1 parent eac100b commit f9660c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/core/hieroglyph.CharDecoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ object CharDecoder:
def unapply(name: Text)(using EncodingMitigation): Option[CharDecoder] =
Encoding.unapply(name).map(CharDecoder(_))

class CharDecoder(val encoding: Encoding)(using handler: EncodingMitigation):
def decode(bytes: Bytes): Text =
class CharDecoder(val encoding: Encoding)(using handler: EncodingMitigation)
extends Decodable:
type Self = Text
type Codec = Bytes

def decode(bytes: Bytes, omit: Boolean): Text =
val buf: StringBuilder = StringBuilder()
decode(LazyList(bytes)).each { text => buf.append(text.s) }
buf.toString.tt

def decode(bytes: Bytes): Text = decode(bytes, false)

def decode(stream: LazyList[Bytes]): LazyList[Text] =
val decoder = encoding.charset.newDecoder().nn
val out = jn.CharBuffer.allocate(4096).nn
Expand Down
5 changes: 4 additions & 1 deletion src/core/hieroglyph.CharEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ object CharEncoder:
def unapply(name: Text): Option[CharEncoder] =
Encoding.codecs.get(name.s.toLowerCase.nn.tt).map(CharEncoder(_))

class CharEncoder(val encoding: Encoding { type CanEncode = true }):
class CharEncoder(val encoding: Encoding { type CanEncode = true })
extends Encodable:
type Self = Text
type Codec = Bytes
def encode(text: Text): Bytes = text.s.getBytes(encoding.name.s).nn.immutable(using Unsafe)
def encode(stream: LazyList[Text]): LazyList[Bytes] = stream.map(encode)

0 comments on commit f9660c0

Please sign in to comment.