Skip to content

Commit

Permalink
Unify Number.niceStr implementations and move to korlibs.number (#1858)
Browse files Browse the repository at this point in the history
* Unify Number.niceStr implementations and move to korlibs.number

* Remove Klock's internal niceStr
  • Loading branch information
soywiz authored Sep 18, 2023
1 parent fd3d05d commit 5163e7d
Show file tree
Hide file tree
Showing 56 changed files with 130 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import korlibs.datastructure.*
import korlibs.io.file.*
import korlibs.io.lang.*
import korlibs.io.stream.*
import korlibs.number.*
import korlibs.time.*
import kotlinx.coroutines.*
import kotlin.coroutines.cancellation.CancellationException
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package korlibs.image.color

import korlibs.memory.clamp
import korlibs.io.util.niceStr
import korlibs.math.interpolation.*
import korlibs.encoding.shex
import korlibs.number.*

data class ColorTransformMul(
private var _r: Float = 1f,
Expand Down
24 changes: 9 additions & 15 deletions korge-core/src/commonMain/kotlin/korlibs/image/color/RGBA.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package korlibs.image.color

import korlibs.datastructure.GenericListIterator
import korlibs.datastructure.GenericSubList
import korlibs.memory.*
import korlibs.image.internal.d2i
import korlibs.image.internal.f2i
import korlibs.image.internal.packIntClamped
import korlibs.image.internal.packIntUnchecked
import korlibs.image.internal.sumPacked4MulR
import korlibs.image.paint.Paint
import korlibs.io.util.niceStr
import korlibs.math.interpolation.*
import korlibs.encoding.appendHexByte
import korlibs.datastructure.*
import korlibs.encoding.*
import korlibs.image.internal.*
import korlibs.image.paint.*
import korlibs.math.geom.*
import kotlin.jvm.JvmName
import kotlin.math.pow
import kotlin.math.roundToInt
import korlibs.math.interpolation.*
import korlibs.memory.*
import korlibs.number.*
import kotlin.jvm.*
import kotlin.math.*

inline class RGBA(val value: Int) : Comparable<RGBA>, Interpolable<RGBA>, Paint {
override fun clone(): Paint = this
Expand Down
4 changes: 2 additions & 2 deletions korge-core/src/commonMain/kotlin/korlibs/image/color/RGBAf.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package korlibs.image.color

import korlibs.memory.clamp01
import korlibs.io.util.niceStr
import korlibs.math.geom.*
import korlibs.memory.*
import korlibs.number.*

//inline class RGBAf private constructor(
inline class RGBAf(val data: FloatArray) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package korlibs.image.font

import korlibs.io.util.*
import korlibs.math.geom.*
import korlibs.number.*
import kotlin.math.*

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import korlibs.io.util.*
import korlibs.math.geom.*
import korlibs.math.geom.bezier.*
import korlibs.math.geom.vector.*
import korlibs.number.*
import kotlin.math.*
import kotlin.native.concurrent.*

Expand Down
17 changes: 2 additions & 15 deletions korge-core/src/commonMain/kotlin/korlibs/image/vector/Shape.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,11 @@ import korlibs.image.paint.*
import korlibs.image.text.*
import korlibs.image.vector.format.*
import korlibs.io.serialization.xml.*
import korlibs.io.util.niceStr
import korlibs.math.geom.*
import korlibs.math.geom.bezier.*
import korlibs.math.geom.vector.*
import kotlin.collections.List
import kotlin.collections.Map
import kotlin.collections.any
import kotlin.collections.arrayListOf
import kotlin.collections.buildList
import kotlin.collections.filter
import kotlin.collections.first
import kotlin.collections.joinToString
import kotlin.collections.linkedMapOf
import kotlin.collections.listOf
import kotlin.collections.map
import kotlin.collections.mapOf
import kotlin.collections.plus
import kotlin.collections.plusAssign
import korlibs.number.*
import kotlin.collections.*
import kotlin.math.*

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package korlibs.io.lang
import korlibs.memory.ByteArrayBuilder
import korlibs.memory.clamp
import korlibs.io.util.quote
import korlibs.io.util.toStringUnsigned
import korlibs.util.format as format2

operator fun String.Companion.invoke(arrays: IntArray, offset: Int = 0, size: Int = arrays.size - offset): String {
Expand Down
62 changes: 2 additions & 60 deletions korge-core/src/commonMain/kotlin/korlibs/io/util/NumberExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,17 @@

package korlibs.io.util

import korlibs.math.*
import korlibs.memory.isNanOrInfinite
import kotlin.math.absoluteValue
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.round

fun Int.toStringUnsigned(radix: Int): String = this.toUInt().toString(radix)
fun Long.toStringUnsigned(radix: Int): String = this.toULong().toString(radix)

val Double.niceStr: String get() = niceStr(-1, zeroSuffix = false)
fun Double.niceStr(decimalPlaces: Int): String = niceStr(decimalPlaces, zeroSuffix = false)
fun Double.niceStr(decimalPlaces: Int, zeroSuffix: Boolean): String = buildString { appendNice(this@niceStr.roundDecimalPlaces(decimalPlaces), zeroSuffix = zeroSuffix) }

val Float.niceStr: String get() = niceStr(-1, zeroSuffix = false)
fun Float.niceStr(decimalPlaces: Int): String = niceStr(decimalPlaces, zeroSuffix = false)
fun Float.niceStr(decimalPlaces: Int, zeroSuffix: Boolean): String = buildString { appendNice(this@niceStr.roundDecimalPlaces(decimalPlaces), zeroSuffix = zeroSuffix) }

//val Float.niceStr: String get() = buildString { appendNice(this@niceStr) }
//fun Float.niceStr(decimalPlaces: Int): String = roundDecimalPlaces(decimalPlaces).niceStr
import kotlin.math.*

private fun Double.isAlmostEquals(other: Double, epsilon: Double = 0.000001): Boolean = (this - other).absoluteValue < epsilon
private fun Float.isAlmostEquals(other: Float, epsilon: Float = 0.000001f): Boolean = (this - other).absoluteValue < epsilon

fun StringBuilder.appendNice(value: Double, zeroSuffix: Boolean): Unit {
when {
round(value).isAlmostEquals(value) -> when {
value >= Int.MIN_VALUE.toDouble() && value <= Int.MAX_VALUE.toDouble() -> append(value.toInt())
else -> append(value.toLong())
}
else -> {
append(value)
return
}
}
if (zeroSuffix) append(".0")
}
fun StringBuilder.appendNice(value: Float, zeroSuffix: Boolean): Unit {
when {
round(value).isAlmostEquals(value) -> when {
value >= Int.MIN_VALUE.toFloat() && value <= Int.MAX_VALUE.toFloat() -> append(value.toInt())
else -> append(value.toLong())
}
else -> {
append(value)
return
}
}
if (zeroSuffix) append(".0")
}

fun StringBuilder.appendNice(value: Double): Unit = appendNice(value, zeroSuffix = false)
fun StringBuilder.appendNice(value: Float): Unit = appendNice(value, zeroSuffix = false)

//private fun Double.normalizeZero(): Double = if (this.isAlmostZero()) 0.0 else this
private val MINUS_ZERO_D = -0.0
private fun Double.normalizeZero(): Double = if (this == MINUS_ZERO_D) 0.0 else this

private fun Float.roundDecimalPlaces(places: Int): Float {
if (places < 0) return this
val placesFactor: Float = 10f.pow(places.toFloat())
return round(this * placesFactor) / placesFactor
}

private fun Double.roundDecimalPlaces(places: Int): Double {
if (places < 0) return this
val placesFactor: Double = 10.0.pow(places.toDouble())
return round(this * placesFactor) / placesFactor
}

fun Double.toStringDecimal(decimalPlaces: Int, skipTrailingZeros: Boolean = false): String {
if (this.isNanOrInfinite()) return this.toString()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package korlibs.image

import korlibs.io.util.*
import korlibs.number.*
import kotlin.test.*

class QualityTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import korlibs.io.util.*
import korlibs.math.geom.*
import korlibs.math.geom.shape.*
import korlibs.math.geom.vector.*
import korlibs.number.*
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package korlibs.image.vector

import korlibs.io.util.niceStr
import korlibs.math.roundDecimalPlaces
import korlibs.number.*
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import korlibs.io.util.*
import korlibs.math.geom.*
import korlibs.math.geom.vector.*
import korlibs.math.interpolation.*
import korlibs.number.*
import kotlin.test.*

class RasterizerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ package korlibs.datastructure.ds

import korlibs.datastructure.*
import korlibs.datastructure.internal.*
import korlibs.math.*
import korlibs.number.*
import kotlin.collections.HashMap
import kotlin.collections.List
import kotlin.collections.contains
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package korlibs.math.geom

import korlibs.math.geom.range.*
import korlibs.math.internal.*
import korlibs.math.interpolation.*
import korlibs.math.isAlmostEquals
import korlibs.math.roundDecimalPlaces
import korlibs.memory.*
import korlibs.number.*
import kotlin.math.*

@PublishedApi internal const val PIF = (PI).toFloat()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package korlibs.math.geom

import korlibs.datastructure.*
import korlibs.math.*
import korlibs.math.annotations.*
import korlibs.math.internal.*
import korlibs.math.interpolation.*
import korlibs.math.isAlmostEquals
import korlibs.math.isAlmostZero
import korlibs.math.roundDecimalPlaces
import korlibs.memory.*
import korlibs.number.*
import kotlin.math.*

typealias MVector2D = MPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package korlibs.math.geom

import korlibs.datastructure.*
import korlibs.math.annotations.*
import korlibs.math.internal.*
import korlibs.math.interpolation.*
import korlibs.math.isAlmostEquals
import korlibs.math.roundDecimalPlaces
import korlibs.number.*

@KormaMutableApi
@Deprecated("Use Rectangle")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package korlibs.math.geom

import korlibs.math.annotations.KormaMutableApi
import korlibs.math.internal.niceStr
import korlibs.math.interpolation.Interpolable
import korlibs.math.interpolation.MutableInterpolable
import korlibs.math.interpolation.Ratio
import korlibs.math.interpolation.interpolate
import korlibs.number.*

@KormaMutableApi
@Deprecated("Use Size instead")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package korlibs.math.geom

import korlibs.math.annotations.KormaMutableApi
import korlibs.math.internal.niceStr
import korlibs.math.interpolation.interpolate
import korlibs.math.interpolation.toRatio
import korlibs.math.almostEquals
import korlibs.number.*
import kotlin.math.sqrt

@KormaMutableApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package korlibs.math.geom

import korlibs.math.almostEquals
import korlibs.math.annotations.*
import korlibs.math.internal.*
import korlibs.math.interpolation.*
import korlibs.number.*
import kotlin.math.*

@Deprecated("")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package korlibs.math.geom

import korlibs.math.internal.*
import korlibs.number.*

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

package korlibs.math.geom

import korlibs.math.internal.*
import korlibs.math.*
import korlibs.math.interpolation.*
import korlibs.math.isAlmostEquals
import korlibs.number.*
import kotlin.math.*

//@KormaValueApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package korlibs.math.geom
import korlibs.math.annotations.*
import korlibs.math.geom.shape.*
import korlibs.math.geom.vector.*
import korlibs.math.internal.*
import korlibs.math.interpolation.*
import korlibs.math.isAlmostEquals
import korlibs.math.roundDecimalPlaces
import korlibs.memory.*
import korlibs.number.*
import kotlin.math.*

//@KormaValueApi
Expand Down
Loading

0 comments on commit 5163e7d

Please sign in to comment.