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

Int Literal: encode long integers #70

Merged
merged 1 commit into from
Dec 28, 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
20 changes: 18 additions & 2 deletions selfie-lib/src/commonMain/kotlin/com/diffplug/selfie/Literals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.diffplug.selfie

import kotlin.math.abs

enum class Language {
JAVA,
JAVA_PRE15,
Expand Down Expand Up @@ -49,9 +51,23 @@ interface LiteralFormat<T : Any> {
}

object LiteralInt : LiteralFormat<Int> {
const val MAX_RAW_NUMBER = 1000
const val PADDING_SIZE = MAX_RAW_NUMBER.toString().length - 1
override fun encode(value: Int, language: Language): String {
// TODO: 1000000 is hard to read, 1_000_000 is much much better
return value.toString()
return if (value >= MAX_RAW_NUMBER) {
val mod = value % MAX_RAW_NUMBER
val leftPadding = PADDING_SIZE - mod.toString().length
val buffer = StringBuilder()
buffer.append(encode(value / MAX_RAW_NUMBER, language))
buffer.append("_")
for (i in leftPadding downTo 1) buffer.append('0')
buffer.append(mod)
buffer.toString()
} else if (value < 0) {
"-" + encode(abs(value), language)
} else {
value.toString()
}
}
override fun parse(str: String, language: Language): Int {
return str.replace("_", "").toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ class LiteralIntTest {
encode(-1, "-1")
encode(999, "999")
encode(-999, "-999")
// TODO: add underscores
encode(1_000, "1000")
encode(-1_000, "-1000")
encode(1_000_000, "1000000")
encode(-1_000_000, "-1000000")
encode(1_000, "1_000")
encode(-1_000, "-1_000")
encode(1_000_000, "1_000_000")
encode(-1_000_000, "-1_000_000")
encode(2400500, "2_400_500")
encode(2400501, "2_400_501")
encode(200, "200")
encode(1001, "1_001")
encode(1010, "1_010")
encode(10010, "10_010")
}
private fun encode(value: Int, expected: String) {
val actual = LiteralInt.encode(value, Language.JAVA)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ class InlineIntTest : Harness("undertest-junit5") {
ut_mirror().lineWith("expectSelfie").setContent(" expectSelfie(1234).toBe_TODO()")
gradleReadSSFail()
gradleWriteSS()
ut_mirror().lineWith("expectSelfie").content() shouldBe " expectSelfie(1234).toBe(1234)"
ut_mirror().lineWith("expectSelfie").content() shouldBe " expectSelfie(1234).toBe(1_234)"
gradleReadSS()
}

@Test @Order(3)
fun toBe_writeLiteral() {
ut_mirror().lineWith("expectSelfie").setContent(" expectSelfie(7777).toBe(1234)")
ut_mirror().lineWith("expectSelfie").setContent(" expectSelfie(7777).toBe(1_234)")
gradleReadSSFail()
gradleWriteSS()
ut_mirror().lineWith("expectSelfie").content() shouldBe " expectSelfie(7777).toBe(7777)"
ut_mirror().lineWith("expectSelfie").content() shouldBe " expectSelfie(7777).toBe(7_777)"
gradleReadSS()
}
}