Add typed Kotlin byte hex conversion
ober
435e375cf4d3120e3f3c85b70ffd104cf3fd967e
--- a/lib/jerboa/typed/checker.ss +++ b/lib/jerboa/typed/checker.ss @@ -393,6 +393,9 @@ (cons 'utf8->string (make-typed-call-sig (list 'Bytes) 'String '() 'utf8->string '())) + (cons 'bytes->lower-hex + (make-typed-call-sig (list 'Bytes) 'String '() + 'jvm-bytes-lower-hex '())) ;; concatenate two byte strings into a fresh owned buffer. (cons 'bytevector-append (make-typed-call-sig (list 'Bytes 'Bytes) 'Bytes '() --- a/lib/jerboa/typed/core.ss +++ b/lib/jerboa/typed/core.ss @@ -128,6 +128,7 @@ bytevector-append integer->le-bytes make-bytevector + jvm-bytes-lower-hex exact->inexact log2 debug-string --- a/lib/jerboa/typed/kotlin/lower.ss +++ b/lib/jerboa/typed/kotlin/lower.ss @@ -565,6 +565,20 @@ (make-kt-member-get (car args) 'size) 'toULong '())] + [(jvm-bytes-lower-hex) + (make-kt-member-call + (make-kt-member-call + (make-kt-new + (make-kt-type '(java math BigInteger) #f '()) + (list (make-kt-lit 'Int32 1) (car args))) + 'toString + (list (make-kt-lit 'Int32 16))) + 'padStart + (list + (make-kt-binary "*" + (make-kt-member-get (car args) 'size) + (make-kt-lit 'Int32 2)) + (make-kt-lit 'Char #\0)))] [(kotlin-call) (make-kt-call (make-kt-name (info-ref info 'path (list operator))) args)] [(record-ctor) --- a/tests/test-typed-kotlin.ss +++ b/tests/test-typed-kotlin.ss @@ -558,7 +558,7 @@ (define bytes-build-form '(typed-library (sample typed bytesbuild) - (export lowBytes) + (export lowBytes hexBytes) (type Int32) (type IntArray) (def (lowBytes (items : IntArray)) : Bytes @@ -569,7 +569,9 @@ (bitwise-arithmetic-shift-right (int-array-ref items (/ (int32 index) (int32 4))) (* (mod (int32 index) (int32 4)) (int32 8))) - (int32 255))))))) + (int32 255))))) + (def (hexBytes (items : Bytes)) : String + (bytes->lower-hex items)))) (define bytes-build-kotlin (typed-library-form->kotlin-string bytes-build-form)) @@ -580,6 +582,9 @@ (test-contains "Bytes build lowers body to Byte" bytes-build-kotlin ".toByte()") +(test-contains "Bytes lower hex lowers through BigInteger" + bytes-build-kotlin + "java.math.BigInteger(1, items).toString(16).padStart((items.size * 2), '0')") (define list-form '(typed-library (sample typed lists)