Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codecs.nulTerminatedString codec #500

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ ThisBuild / scmInfo := Some(
ScmInfo(url("https://github.com/scodec/scodec"), "git@github.com:scodec/scodec.git")
)

// Disabled due to erroneous unused import warnings on scala.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Falsely attributed to Scala.js (I know, I know @armanbilge)

ThisBuild / tlFatalWarnings := false

ThisBuild / licenses := List(
("BSD-3-Clause", url("https://github.com/scodec/scodec/blob/main/LICENSE"))
)
Expand Down
1 change: 0 additions & 1 deletion shared/src/main/scala/scodec/Attempt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
package scodec

import scala.util.Try
import scala.util.control.NonFatal

/** Right biased `Either[Err, A]`.
*
Expand Down
1 change: 0 additions & 1 deletion shared/src/main/scala/scodec/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import scala.deriving.*
import scala.compiletime.*

import scodec.bits.{BitVector, ByteVector}
import scala.collection.mutable

/** Supports encoding a value of type `A` to a `BitVector` and decoding a `BitVector` to a value of `A`.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
package scodec
package codecs

import scodec.{Codec, Err}
import scodec.bits.BitVector

private class PaddedVarAlignedCodec[A](
Expand Down
31 changes: 18 additions & 13 deletions shared/src/main/scala/scodec/codecs/codecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -461,19 +461,24 @@ val utf8 = string(Platform.utf8)
* and decodes a string up to the next `NUL` termination byte.
* It fails to decode if the bit vector ends before a `NUL` termination byte can be found.
*/
val cstring: Codec[String] = filtered(
ascii,
new Codec[BitVector] {
val nul = BitVector.lowByte
override def sizeBound: SizeBound = SizeBound.unknown
override def encode(bits: BitVector): Attempt[BitVector] = Attempt.successful(bits ++ nul)
override def decode(bits: BitVector): Attempt[DecodeResult[BitVector]] =
bits.bytes.indexOfSlice(nul.bytes) match {
case -1 => Attempt.failure(Err("Does not contain a 'NUL' termination byte."))
case i => Attempt.successful(DecodeResult(bits.take(i * 8L), bits.drop(i * 8L + 8L)))
}
}
).withToString("cstring")
val cstring: Codec[String] = nulTerminatedString(ascii)

/** String codec that encodes strings with a trailing `NUL` termination byte and decodes strings up to the next
* `NUL` termination byte.
* It fails to decode if the bit vector ends before a `NUL` termination byte can be found.
*/
def nulTerminatedString(stringCodec: Codec[String]): Codec[String] =
filtered(
stringCodec,
new Codec[BitVector]:
val nul = BitVector.lowByte
override def sizeBound: SizeBound = SizeBound.unknown
override def encode(bits: BitVector): Attempt[BitVector] = Attempt.successful(bits ++ nul)
override def decode(bits: BitVector): Attempt[DecodeResult[BitVector]] =
bits.bytes.indexOfSlice(nul.bytes) match
case -1 => Attempt.failure(Err("Does not contain a 'NUL' termination byte."))
case i => Attempt.successful(DecodeResult(bits.take(i * 8L), bits.drop(i * 8L + 8L)))
).withToString(s"nulTerminatedString($stringCodec)")

/** String codec that uses the given `Charset` and prefixes the encoded string by the byte size
* in a 32-bit 2s complement big endian field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class StringCodecTest extends CodecSuite:
)
}

test("nulTerminatedString - roundtrip") {
roundtripAll(nulTerminatedString(utf8), Seq("test", "", "withλ"))
}

test("utf8 - roundtrip") {
roundtripAll(utf8, Seq("test", "", "with\ttabs", "withλ"))
}
Expand Down