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
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions core/shared/src/main/scala-2/scodec/bits/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ package object bits extends ScalaVersionSpecific {
*
* Named arguments are supported in the same manner as the standard `s` interpolator.
*/
def ascii(args: String*): ByteVector = macro LiteralSyntaxMacros.asciiStringInterpolator
def asciiBytes(args: String*): ByteVector = macro LiteralSyntaxMacros.asciiStringInterpolator
}

/** Provides the `utf8` string interpolator, which returns `ByteVector` instances from UTF8
Expand All @@ -85,7 +85,7 @@ package object bits extends ScalaVersionSpecific {
*
* Named arguments are supported in the same manner as the standard `s` interpolator.
*/
def utf8(args: String*): ByteVector = macro LiteralSyntaxMacros.utf8StringInterpolator
def utf8Bytes(args: String*): ByteVector = macro LiteralSyntaxMacros.utf8StringInterpolator
}

private[bits] implicit class EitherOps[L, R](val self: Either[L, R]) extends AnyVal {
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala-3/scodec/bits/Interpolators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension (inline ctx: StringContext)
* }}}
*/
extension (inline ctx: StringContext)
inline def ascii(inline args: Any*): ByteVector =
inline def asciiBytes(inline args: Any*): ByteVector =
${ Literals.Ascii('ctx, 'args) }

/** Provides the `utf8` string interpolator, which returns `ByteVector` instances from utf8 strings.
Expand All @@ -73,7 +73,7 @@ extension (inline ctx: StringContext)
* }}}
*/
extension (inline ctx: StringContext)
inline def utf8(inline args: Any*): ByteVector =
inline def utf8Bytes(inline args: Any*): ByteVector =
${ Literals.Utf8('ctx, 'args) }

object Literals:
Expand Down
6 changes: 3 additions & 3 deletions core/shared/src/test/scala/scodec/bits/ByteVectorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -775,11 +775,11 @@ class ByteVectorTest extends BitsSuite {
}

test("ascii interpolator") {
assertEquals(ascii"deadbeef", ByteVector.encodeAscii(s"deadbeef").toOption.get)
assert(compileErrors("""ascii"ɟǝǝqpɐǝp"""").contains("error"))
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

}
}