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 ascii and utf8 interpolators #381

Merged
merged 3 commits into from
Jul 28, 2022

Conversation

armanbilge
Copy link
Contributor

Wanted these at least a few times :)

scala> val b = ascii"deadbeef"
val b: scodec.bits.ByteVector = ByteVector(8 bytes, 0x6465616462656566)
scala> val b = utf8"ɟǝǝqpɐǝp"
val b: scodec.bits.ByteVector = ByteVector(13 bytes, 0xc99fc79dc79d7170c990c79d70)

@armanbilge
Copy link
Contributor Author

Hmm, I suppose the naming is perhaps a bit overloaded. An ascii interpolator that enforces asciiness but returns a String could be another useful implementation I suppose (outside of scodec-bits of course).

@mpilquist
Copy link
Contributor

We'd have to watch out for name conflicts at the very least. E.g., it's common to import both scodec.bits.* and scodec.codecs.*, the latter of which has ascii and utf8 defined as Codec[String].

@armanbilge
Copy link
Contributor Author

I think the Scala compiler is capable of disambiguating that case actually.

//> using scala "2.12.16"
//> using lib "org.scodec::scodec-bits::1.1.34"

package object foo {
  def hex(args: String): Unit = ???
}

import scodec.bits._
import foo._

object Test {
  hex"deadbeef"
}

In any case, maybe asciiBytes and utf8Bytes would be more appropriate?

Comment on lines 778 to 783
assertEquals(asciiBytes"deadbeef", ByteVector.encodeAscii(s"deadbeef").toOption.get)
assert(compileErrors("""asciiBytes"ɟǝǝqpɐǝp"""").contains("error"))
}

test("utf8 interpolator") {
assertEquals(utf8"ɟǝǝqpɐǝp", ByteVector.encodeUtf8(s"ɟǝǝqpɐǝp").toOption.get)
assertEquals(utf8Bytes"ɟǝǝqpɐǝp", ByteVector.encodeUtf8(s"ɟǝǝqpɐǝp").toOption.get)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure about this /shrug

@ChristopherDavenport
Copy link

Love this. Have this in a lot of cases and have wanted precisely this.

@mpilquist mpilquist merged commit 15c14b8 into scodec:main Jul 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants