-
Notifications
You must be signed in to change notification settings - Fork 2
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
19 changed files
with
575 additions
and
136 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
common/src/androidMain/kotlin/com/cmgapps/common/curriculumvitae/format/SizeFormatter.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,29 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
import java.text.CharacterIterator | ||
import java.text.StringCharacterIterator | ||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
actual fun Long.humanReadableSize(): String { | ||
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this) | ||
if (absB < 1024) { | ||
return "$this B" | ||
} | ||
var value = absB | ||
val ci: CharacterIterator = StringCharacterIterator("KMGTPE") | ||
var i = 40 | ||
while (i >= 0 && absB > 0xfffccccccccccccL shr i) { | ||
value = value shr 10 | ||
ci.next() | ||
i -= 10 | ||
} | ||
value *= sign(this.toDouble()).toLong() | ||
return String.format("%.2f %cB", value / 1024.0, ci.current()) | ||
} |
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
9 changes: 9 additions & 0 deletions
9
common/src/commonMain/kotlin/com/cmgapps/common/curriculumvitae/format/SizeFormatter.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 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
expect fun Long.humanReadableSize(): String |
84 changes: 84 additions & 0 deletions
84
...rc/commonTest/kotlin/com/cmgapps/common/curriculumvitae/format/HumanReadableSizeShould.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,84 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
import kotlin.math.pow | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class HumanReadableSizeShould { | ||
|
||
@Test | ||
fun get_zero() { | ||
assertEquals("0 B", 0L.humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_negative() { | ||
assertEquals("-54 B", (-54L).humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_bytes() { | ||
assertEquals("1000 B", 1000L.humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_kilobytes() { | ||
assertEquals("1.00 KB", 2f.pow(10).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_arbitrary_kilobytes() { | ||
assertEquals("4.88 KB", 5000L.humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_megabytes() { | ||
assertEquals("1.00 MB", 2f.pow(20).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_gigabytes() { | ||
assertEquals("1.00 GB", 2f.pow(30).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_terabytes() { | ||
assertEquals("1.00 TB", 2f.pow(40).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_petabytes() { | ||
assertEquals("1.00 PB", 2F.pow(50).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_exabytes() { | ||
assertEquals("1.00 EB", 2F.pow(60).toLong().humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_max() { | ||
assertEquals("8.00 EB", Long.MAX_VALUE.humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_near_max() { | ||
assertEquals("8.00 EB", (Long.MAX_VALUE - 1).humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_min() { | ||
assertEquals("-8.00 EB", Long.MIN_VALUE.humanReadableSize()) | ||
} | ||
|
||
@Test | ||
fun get_near_minimum() { | ||
assertEquals("-8.00 EB", (Long.MIN_VALUE + 2).humanReadableSize()) | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
common/src/iosMain/kotlin/com/cmgapps/common/curriculumvitae/format/SizeFormatter.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,38 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
import platform.Foundation.NSNumber | ||
import platform.Foundation.NSNumberFormatter | ||
import platform.Foundation.NSNumberFormatterDecimalStyle | ||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
actual fun Long.humanReadableSize(): String { | ||
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this) | ||
if (absB < 1024) { | ||
return "$this B" | ||
} | ||
var value = absB | ||
val ci = "KMGTPE" | ||
var ciIndex = 0 | ||
var i = 40 | ||
while (i >= 0 && absB > 0xfffccccccccccccL shr i) { | ||
value = value shr 10 | ||
if (++ciIndex > ci.lastIndex) { | ||
break | ||
} | ||
i -= 10 | ||
} | ||
value *= sign(this.toDouble()).toLong() | ||
val formatter = NSNumberFormatter().apply { | ||
numberStyle = NSNumberFormatterDecimalStyle | ||
maximumFractionDigits = 2u | ||
minimumFractionDigits = 2u | ||
} | ||
return "${formatter.stringFromNumber(NSNumber(value / 1024.0))} ${ci[ciIndex]}B" | ||
} |
31 changes: 31 additions & 0 deletions
31
common/src/jsMain/kotlin/com/cmgapps/common/curriculumvitae/format/SizeFormatter.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,31 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
actual fun Long.humanReadableSize(): String { | ||
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this) | ||
if (absB < 1024) { | ||
return "$this B" | ||
} | ||
var value = absB | ||
val ci = "KMGTPE" | ||
var ciIndex = 0 | ||
var i = 40 | ||
while (i >= 0 && absB > 0xfffccccccccccccL shr i) { | ||
value = value shr 10 | ||
|
||
if (++ciIndex > ci.length - 1) { | ||
break | ||
} | ||
i -= 10 | ||
} | ||
value *= sign(this.toDouble()).toLong() | ||
return "${(value / 1024.0).asDynamic().toFixed(2)} ${ci[ciIndex]}B" | ||
} |
29 changes: 29 additions & 0 deletions
29
common/src/jvmMain/kotlin/com/cmgapps/common/curriculumvitae/format/SizeFormatter.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,29 @@ | ||
/* | ||
* Copyright (c) 2023. Christian Grach <christian.grach@cmgapps.com> | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.cmgapps.common.curriculumvitae.format | ||
|
||
import java.text.CharacterIterator | ||
import java.text.StringCharacterIterator | ||
import kotlin.math.abs | ||
import kotlin.math.sign | ||
|
||
actual fun Long.humanReadableSize(): String { | ||
val absB = if (this == Long.MIN_VALUE) Long.MAX_VALUE else abs(this) | ||
if (absB < 1024) { | ||
return "$this B" | ||
} | ||
var value = absB | ||
val ci: CharacterIterator = StringCharacterIterator("KMGTPE") | ||
var i = 40 | ||
while (i >= 0 && absB > 0xfffccccccccccccL shr i) { | ||
value = value shr 10 | ||
ci.next() | ||
i -= 10 | ||
} | ||
value *= sign(this.toDouble()).toLong() | ||
return String.format("%.2f %cB", value / 1024.0, ci.current()) | ||
} |
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
Oops, something went wrong.