-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
324 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Klog | ||
|
11 changes: 11 additions & 0 deletions
11
klog/src/main/kotlin/com/coditory/klog/format/BytesFormat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.coditory.klog.format | ||
|
||
object BytesFormat { | ||
fun formatBytesSI(bytes: Long): String { | ||
return QuantityFormat.formatQuantitySIWithSuffix(bytes, 'B') | ||
} | ||
|
||
fun formatBytesBin(bytes: Long): String { | ||
return QuantityFormat.formatQuantityBinWithSuffix(bytes, 'B') | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
klog/src/main/kotlin/com/coditory/klog/format/QuantityFormat.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.coditory.klog.format | ||
|
||
import java.lang.Long.signum | ||
import kotlin.math.abs | ||
|
||
object QuantityFormat { | ||
fun formatQuantitySI(bytes: Long): String { | ||
return formatQuantitySIWithSuffix(bytes, suffix = null) | ||
} | ||
|
||
fun formatQuantityBin(bytes: Long): String { | ||
return formatQuantityBinWithSuffix(bytes, suffix = null) | ||
} | ||
|
||
internal fun formatQuantitySIWithSuffix( | ||
bytes: Long, | ||
suffix: Char?, | ||
): String { | ||
if (-1000 < bytes && bytes < 1000) { | ||
return if (suffix != null) "$bytes $suffix" else "$bytes" | ||
} | ||
var significant = bytes | ||
val x = "kMGTPE" | ||
var xi = 0 | ||
while (xi < x.length - 1 && (significant <= -999_950 || significant >= 999_950)) { | ||
significant /= 1000 | ||
xi++ | ||
} | ||
return if (suffix != null) { | ||
String.format("%.1f %c$suffix", significant / 1000.0, x[xi]) | ||
} else { | ||
String.format("%.1f %c", significant / 1000.0, x[xi]) | ||
} | ||
} | ||
|
||
internal fun formatQuantityBinWithSuffix( | ||
bytes: Long, | ||
suffix: Char?, | ||
): String { | ||
val absB = | ||
if (bytes == Long.MIN_VALUE) { | ||
Long.MAX_VALUE | ||
} else { | ||
abs(bytes.toDouble()).toLong() | ||
} | ||
if (absB < 1024) { | ||
return if (suffix != null) "$bytes $suffix" else "$bytes" | ||
} | ||
var value = absB | ||
val x = "KMGTPE" | ||
var xi = 0 | ||
var i = 40 | ||
while (xi < x.length - 1 && (i >= 0 && absB > 0xfffccccccccccccL shr i)) { | ||
value = value shr 10 | ||
xi++ | ||
i -= 10 | ||
} | ||
value *= signum(bytes).toLong() | ||
return if (suffix != null) { | ||
String.format("%.1f %ci$suffix", value / 1024.0, x[xi]) | ||
} else { | ||
String.format("%.1f %ci", value / 1024.0, x[xi]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
klog/src/test/kotlin/com/coditory/klog/format/BytesFormatSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.coditory.klog.format | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.tuple | ||
import io.kotest.matchers.shouldBe | ||
|
||
class BytesFormatSpec : FunSpec({ | ||
listOf( | ||
tuple(1L, "1 B"), | ||
tuple(123L, "123 B"), | ||
tuple(999L, "999 B"), | ||
tuple(1000L, "1.0 kB"), | ||
tuple(1001L, "1.0 kB"), | ||
tuple(123456L, "123.5 kB"), | ||
tuple(123456L * 1000, "123.5 MB"), | ||
tuple(123456L * 1000 * 1000, "123.5 GB"), | ||
tuple(123456L * 1000 * 1000 * 1000, "123.5 TB"), | ||
tuple(123456L * 1000 * 1000 * 1000 * 1000, "123.5 PB"), | ||
tuple(123456L * 1000 * 1000 * 1000 * 1000 * 10, "1.2 EB"), | ||
).forEach { | ||
test("format SI: ${it.a} -> \"${it.b}\"") { | ||
BytesFormat.formatBytesSI(it.a) shouldBe it.b | ||
} | ||
} | ||
|
||
listOf( | ||
tuple(1L, "1 B"), | ||
tuple(123L, "123 B"), | ||
tuple(1023L, "1023 B"), | ||
tuple(1024L, "1.0 KiB"), | ||
tuple(1025L, "1.0 KiB"), | ||
tuple(123456L, "120.6 KiB"), | ||
tuple(123456L * 1024, "120.6 MiB"), | ||
tuple(123456L * 1024 * 1024, "120.6 GiB"), | ||
tuple(123456L * 1024 * 1024 * 1024, "120.6 TiB"), | ||
tuple(123456L * 1024 * 1024 * 1024 * 1024, "120.6 PiB"), | ||
tuple(123456L * 1024 * 1024 * 1024 * 1024 * 10, "1.2 EiB"), | ||
).forEach { | ||
test("format bin: ${it.a} -> \"${it.b}\"") { | ||
BytesFormat.formatBytesBin(it.a) shouldBe it.b | ||
} | ||
} | ||
}) |
45 changes: 45 additions & 0 deletions
45
klog/src/test/kotlin/com/coditory/klog/format/QuantityFormatSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.coditory.klog.format | ||
|
||
import com.coditory.klog.format.QuantityFormat.formatQuantityBin | ||
import com.coditory.klog.format.QuantityFormat.formatQuantitySI | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.tuple | ||
import io.kotest.matchers.shouldBe | ||
|
||
class QuantityFormatSpec : FunSpec({ | ||
listOf( | ||
tuple(1L, "1"), | ||
tuple(123L, "123"), | ||
tuple(999L, "999"), | ||
tuple(1000L, "1.0 k"), | ||
tuple(1001L, "1.0 k"), | ||
tuple(123456L, "123.5 k"), | ||
tuple(123456L * 1000, "123.5 M"), | ||
tuple(123456L * 1000 * 1000, "123.5 G"), | ||
tuple(123456L * 1000 * 1000 * 1000, "123.5 T"), | ||
tuple(123456L * 1000 * 1000 * 1000 * 1000, "123.5 P"), | ||
tuple(123456L * 1000 * 1000 * 1000 * 1000 * 10, "1.2 E"), | ||
).forEach { | ||
test("format SI: ${it.a} -> \"${it.b}\"") { | ||
formatQuantitySI(it.a) shouldBe it.b | ||
} | ||
} | ||
|
||
listOf( | ||
tuple(1L, "1"), | ||
tuple(123L, "123"), | ||
tuple(1023L, "1023"), | ||
tuple(1024L, "1.0 Ki"), | ||
tuple(1025L, "1.0 Ki"), | ||
tuple(123456L, "120.6 Ki"), | ||
tuple(123456L * 1024, "120.6 Mi"), | ||
tuple(123456L * 1024 * 1024, "120.6 Gi"), | ||
tuple(123456L * 1024 * 1024 * 1024, "120.6 Ti"), | ||
tuple(123456L * 1024 * 1024 * 1024 * 1024, "120.6 Pi"), | ||
tuple(123456L * 1024 * 1024 * 1024 * 1024 * 10, "1.2 Ei"), | ||
).forEach { | ||
test("format bin: ${it.a} -> \"${it.b}\"") { | ||
formatQuantityBin(it.a) shouldBe it.b | ||
} | ||
} | ||
}) |
9 changes: 9 additions & 0 deletions
9
klog/src/test/kotlin/com/coditory/klog/shared/PlainTextStringFormatterExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.coditory.klog.shared | ||
|
||
import com.coditory.klog.text.plain.PlainTextStringFormatter | ||
|
||
fun PlainTextStringFormatter.formatToString(text: String): String { | ||
val sb = StringBuilder() | ||
this.format(text, sb) | ||
return sb.toString() | ||
} |
56 changes: 56 additions & 0 deletions
56
klog/src/test/kotlin/com/coditory/klog/text/plain/CompactedPlainTextStringFormatterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.coditory.klog.text.plain | ||
|
||
import com.coditory.klog.shared.formatToString | ||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.core.tuple | ||
import io.kotest.matchers.shouldBe | ||
|
||
class CompactedPlainTextStringFormatterTest : FunSpec({ | ||
listOf( | ||
tuple("", " "), | ||
tuple("abcdef", " abcdef"), | ||
tuple("abcdefghi", "abcdefghi"), | ||
tuple("abcdefghijkl", "abcdefghi..."), | ||
tuple("x123.y123.z123.abcdef", " z.abcdef"), | ||
tuple("x123.y123.z123.abcde", "y.z.abcde"), | ||
tuple("x123.y123.z123.abcd", " y.z.abcd"), | ||
tuple("x123.y123.z123.abc", "x.y.z.abc"), | ||
tuple("x123.y123.z123.abcdefghijkl", "abcdefghi..."), | ||
).forEach { | ||
test("should pad left to 9 chars: \"${it.a}\" -> \"${it.b}\"") { | ||
val formatted = | ||
PlainTextStringFormatter.builder() | ||
.compactSections() | ||
.padLeft() | ||
.maxLength(9) | ||
.maxLengthMarker("...") | ||
.build() | ||
.formatToString(it.a) | ||
formatted shouldBe it.b | ||
} | ||
} | ||
|
||
listOf( | ||
tuple("", " "), | ||
tuple("abcdef", "abcdef "), | ||
tuple("abcdefghi", "abcdefghi"), | ||
tuple("abcdefghijkl", "abcdefghi..."), | ||
tuple("x123.y123.z123.abcdef", "z.abcdef "), | ||
tuple("x123.y123.z123.abcde", "y.z.abcde"), | ||
tuple("x123.y123.z123.abcd", "y.z.abcd "), | ||
tuple("x123.y123.z123.abc", "x.y.z.abc"), | ||
tuple("x123.y123.z123.abcdefghijkl", "abcdefghi..."), | ||
).forEach { | ||
test("should pad right to 9 chars: \"${it.a}\" -> \"${it.b}\"") { | ||
val formatted = | ||
PlainTextStringFormatter.builder() | ||
.compactSections() | ||
.padRight() | ||
.maxLength(9) | ||
.maxLengthMarker("...") | ||
.build() | ||
.formatToString(it.a) | ||
formatted shouldBe it.b | ||
} | ||
} | ||
}) |
Oops, something went wrong.