Skip to content

Commit

Permalink
Bring back helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhbd committed Aug 17, 2024
1 parent 7c8afdb commit e103b2c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 71 deletions.
85 changes: 29 additions & 56 deletions detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,19 @@
<SmellBaseline>
<ManuallySuppressedIssues/>
<CurrentIssues>
<ID>FunctionNaming:Dsl.kt$@UUIDExperimentalAPI public fun UUIDv7(timeStamp: Instant = Clock.System.now(), random: Random = SecureRandom): UUID</ID>
<ID>FunctionNaming:UUID7.kt$@UUIDExperimentalAPI public fun UUIDv7(timeStamp: Long, random: Random = SecureRandom): UUID</ID>
<ID>MagicNumber:Encoding.kt$0xff</ID>
<ID>MagicNumber:Encoding.kt$56</ID>
<ID>MagicNumber:Encoding.kt$7</ID>
<ID>MagicNumber:Encoding.kt$8</ID>
<ID>MagicNumber:Formatter.kt$0xffff</ID>
<ID>MagicNumber:Formatter.kt$0xffffffffffffL</ID>
<ID>MagicNumber:Formatter.kt$10</ID>
<ID>MagicNumber:Formatter.kt$16</ID>
<ID>MagicNumber:Formatter.kt$32</ID>
<ID>MagicNumber:Formatter.kt$38</ID>
<ID>MagicNumber:Formatter.kt$4</ID>
<ID>MagicNumber:Formatter.kt$48</ID>
<ID>MagicNumber:Formatter.kt$6</ID>
<ID>MagicNumber:Formatter.kt$8</ID>
<ID>MagicNumber:Formatter.kt$9</ID>
<ID>FunctionNaming:Dsl.kt$public fun UUIDv7(timeStamp: Instant = Clock.System.now(), random: Random): Uuid</ID>
<ID>FunctionNaming:UUID7.kt$public fun UUIDv7(timeStamp: Long, random: Random): Uuid</ID>
<ID>MagicNumber:KotlinConfig.kt$8</ID>
<ID>MagicNumber:NameBasedGenerator.kt$0x3f</ID>
<ID>MagicNumber:NameBasedGenerator.kt$4</ID>
<ID>MagicNumber:NameBasedGenerator.kt$6</ID>
<ID>MagicNumber:NameBasedGenerator.kt$8</ID>
<ID>MagicNumber:Parser.kt$10</ID>
<ID>MagicNumber:Parser.kt$12</ID>
<ID>MagicNumber:Parser.kt$16</ID>
<ID>MagicNumber:Parser.kt$32</ID>
<ID>MagicNumber:Parser.kt$4</ID>
<ID>MagicNumber:Parser.kt$48</ID>
<ID>MagicNumber:Parser.kt$6</ID>
<ID>MagicNumber:Parser.kt$8</ID>
<ID>MagicNumber:RandomGenerator.kt$0x38</ID>
<ID>MagicNumber:RandomGenerator.kt$0x3fffffffffffffffL</ID>
<ID>MagicNumber:RandomGenerator.kt$0x4000L</ID>
<ID>MagicNumber:RandomGenerator.kt$0x80L</ID>
<ID>MagicNumber:RandomGenerator.kt$0x3f</ID>
<ID>MagicNumber:RandomGenerator.kt$0x40</ID>
<ID>MagicNumber:RandomGenerator.kt$0x80</ID>
<ID>MagicNumber:RandomGenerator.kt$16</ID>
<ID>MagicNumber:RandomGenerator.kt$6</ID>
<ID>MagicNumber:RandomGenerator.kt$8</ID>
<ID>MagicNumber:SHA1.kt$SHA1$0X67452301</ID>
<ID>MagicNumber:SHA1.kt$SHA1$0x5a827999</ID>
<ID>MagicNumber:SHA1.kt$SHA1$0x6ed9eba1</ID>
Expand Down Expand Up @@ -65,37 +44,31 @@
<ID>MagicNumber:SHA1.kt$SHA1.IntArrayView$3</ID>
<ID>MagicNumber:SHA1.kt$SHA1.IntArrayView$4</ID>
<ID>MagicNumber:SHA1.kt$SHA1.IntArrayView$8</ID>
<ID>MagicNumber:SecureRandom.js.kt$SecureRandomBrowser$31</ID>
<ID>MagicNumber:SecureRandom.js.kt$SecureRandomBrowser$32</ID>
<ID>MagicNumber:UUID.kt$UUID$0x0fffL</ID>
<ID>MagicNumber:UUID.kt$UUID$0x1fff</ID>
<ID>MagicNumber:UUID.kt$UUID$0xf000L</ID>
<ID>MagicNumber:UUID.kt$UUID$0xffff0000L</ID>
<ID>MagicNumber:UUID.kt$UUID$0xffffffffffffL</ID>
<ID>MagicNumber:UUID.kt$UUID$12</ID>
<ID>MagicNumber:UUID.kt$UUID$13</ID>
<ID>MagicNumber:UUID.kt$UUID$15</ID>
<ID>MagicNumber:UUID.kt$UUID$16</ID>
<ID>MagicNumber:UUID.kt$UUID$32</ID>
<ID>MagicNumber:UUID.kt$UUID$4</ID>
<ID>MagicNumber:UUID.kt$UUID$48</ID>
<ID>MagicNumber:UUID.kt$UUID$5</ID>
<ID>MagicNumber:UUID.kt$UUID$60</ID>
<ID>MagicNumber:UUID.kt$UUID$61</ID>
<ID>MagicNumber:UUID.kt$UUID$7</ID>
<ID>MagicNumber:UUID.kt$UUID.Version.NAME_BASED_MD5$3</ID>
<ID>MagicNumber:UUID.kt$UUID.Version.NAME_BASED_SHA1$5</ID>
<ID>MagicNumber:UUID.kt$UUID.Version.RANDOM_BASED$4</ID>
<ID>MagicNumber:UUID.kt$0x0fffL</ID>
<ID>MagicNumber:UUID.kt$0x1fff</ID>
<ID>MagicNumber:UUID.kt$0xf000L</ID>
<ID>MagicNumber:UUID.kt$0xffff00000000L</ID>
<ID>MagicNumber:UUID.kt$0xffff0000L</ID>
<ID>MagicNumber:UUID.kt$0xffffffffffffL</ID>
<ID>MagicNumber:UUID.kt$12</ID>
<ID>MagicNumber:UUID.kt$13</ID>
<ID>MagicNumber:UUID.kt$15</ID>
<ID>MagicNumber:UUID.kt$16</ID>
<ID>MagicNumber:UUID.kt$32</ID>
<ID>MagicNumber:UUID.kt$4</ID>
<ID>MagicNumber:UUID.kt$48</ID>
<ID>MagicNumber:UUID.kt$5</ID>
<ID>MagicNumber:UUID.kt$60</ID>
<ID>MagicNumber:UUID.kt$61</ID>
<ID>MagicNumber:UUID.kt$7</ID>
<ID>MagicNumber:UUID7.kt$0x0fffL</ID>
<ID>MagicNumber:UUID7.kt$0xffff0000L</ID>
<ID>MagicNumber:UUID7.kt$16</ID>
<ID>MagicNumber:UUID7.kt$28672</ID>
<ID>MagicNumber:UUID7.kt$32</ID>
<ID>MagicNumber:UUID7.kt$4095</ID>
<ID>MagicNumber:UUID7.kt$48</ID>
<ID>MagicNumber:UUID7.kt$62</ID>
<ID>MatchingDeclarationName:CommonParcelable.apple.kt$CommonParcelable</ID>
<ID>MatchingDeclarationName:CommonParcelable.js.kt$CommonParcelable</ID>
<ID>MatchingDeclarationName:CommonParcelable.jvm.kt$CommonParcelable</ID>
<ID>MatchingDeclarationName:CommonParcelable.linux.kt$CommonParcelable</ID>
<ID>MatchingDeclarationName:CommonParcelable.ming.kt$CommonParcelable</ID>
<ID>TooManyFunctions:SHA1.kt$SHA1</ID>
<ID>VariableNaming:UUID7.kt$val rand_a = helper.timeStamp and 4095</ID>
</CurrentIssues>
</SmellBaseline>
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public fun Uuid.Companion.random(random: Random): Uuid {

// Copied from stdlib
private fun uuidFromRandomBytes(randomBytes: ByteArray): Uuid {
randomBytes[6] = (randomBytes[6].toInt() and 0x0f).toByte() /* clear version */
randomBytes[6] = (randomBytes[6].toInt() or 0x40).toByte() /* set to version 4 */
randomBytes[8] = (randomBytes[8].toInt() and 0x3f).toByte() /* clear variant */
randomBytes[8] = (randomBytes[8].toInt() or 0x80).toByte() /* set to IETF variant */
// clear version
randomBytes[6] = (randomBytes[6].toInt() and 0x0f).toByte()
// set to version 4
randomBytes[6] = (randomBytes[6].toInt() or 0x40).toByte()
// clear variant
randomBytes[8] = (randomBytes[8].toInt() and 0x3f).toByte()
// set to IETF variant
randomBytes[8] = (randomBytes[8].toInt() or 0x80).toByte()
return Uuid.fromByteArray(randomBytes)
}

Expand Down
18 changes: 12 additions & 6 deletions kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public fun Uuid.Companion.from(
require(clockSequence in 0 until (1 shl 13))

val timeStampAndVersionRaw = (timeStamp shl 32) or (timeStamp and 0xffff00000000L shr 16) or
(timeStamp shr 48) or (versionNumber.toLong() shl 12)
(timeStamp shr 48) or (versionNumber.toLong() shl 12)
val clockSequenceVariantAndNodeRaw = (clockSequence.toLong() shl 48) or
(variant.toLong() shl 61) or node
(variant.toLong() shl 61) or node

return fromLongs(timeStampAndVersionRaw, clockSequenceVariantAndNodeRaw)
}
Expand All @@ -39,8 +39,8 @@ public fun Uuid.Companion.from(
public val Uuid.timeStamp: Long
get() = toLongs { timeStampAndVersionRaw, _ ->
(timeStampAndVersionRaw ushr 32) or
(timeStampAndVersionRaw and 0xffff0000L shl 16) or
(timeStampAndVersionRaw and 0x0fffL shl 48)
(timeStampAndVersionRaw and 0xffff0000L shl 16) or
(timeStampAndVersionRaw and 0x0fffL shl 48)
}

/**
Expand All @@ -53,12 +53,18 @@ public val Uuid.clockSequence: Int
/**
* UUID [numeric version](https://tools.ietf.org/html/rfc4122#section-4.1.3) in range `[0..15]
*/
public val Uuid.versionNumber: Int get() = toLongs { timeStampAndVersionRaw, _ -> (timeStampAndVersionRaw and 0xf000L shr 12).toInt() }
public val Uuid.versionNumber: Int
get() = toLongs { timeStampAndVersionRaw, _ ->
(timeStampAndVersionRaw and 0xf000L shr 12).toInt()
}

/**
* UUID variant in range `[0..7]`, similar to version
*/
public val Uuid.variant: Int get() = toLongs { _, clockSequenceVariantAndNodeRaw -> (clockSequenceVariantAndNodeRaw ushr 61).toInt() }
public val Uuid.variant: Int
get() = toLongs { _, clockSequenceVariantAndNodeRaw ->
(clockSequenceVariantAndNodeRaw ushr 61).toInt()
}

/**
* UUID variant specified and documented by the RFC
Expand Down
11 changes: 8 additions & 3 deletions kotlinx-uuid-core/src/commonMain/kotlin/kotlinx/uuid/UUID7.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ public fun UUIDv7(timeStamp: Long, random: Random): Uuid {
require(timeStamp <= UNIX_48_TIMESTAMP) {
"timeStamp $timeStamp must be <= 48 bits, was $timeStamp."
}
val (helperTimeStampAndVersionRaw, helperClockSequenceVariantAndNodeRaw) = random.nextUuid().toLongs { mostSignificantBits, leastSignificantBits -> mostSignificantBits to leastSignificantBits }
val (
helperTimeStampAndVersionRaw,
helperClockSequenceVariantAndNodeRaw
) = random.nextUuid().toLongs { mostSignificantBits, leastSignificantBits ->
mostSignificantBits to leastSignificantBits
}
val leftTimeStamp = timeStamp shl 16
// set version to 0b0111
val leftTimeStampAndVersion = leftTimeStamp or 28672
val rand_a = helperTimeStampAndVersionRaw.let { timeStampAndVersionRaw ->
val randA = helperTimeStampAndVersionRaw.let { timeStampAndVersionRaw ->
(timeStampAndVersionRaw ushr 32) or
(timeStampAndVersionRaw and 0xffff0000L shl 16) or
(timeStampAndVersionRaw and 0x0fffL shl 48)
} and 4095
val timeStampAndVersionRaw = leftTimeStampAndVersion or rand_a
val timeStampAndVersionRaw = leftTimeStampAndVersion or randA
// set variant to 0b10
val clockSequenceVariantAndNodeRaw = (2L shl 62) or (helperClockSequenceVariantAndNodeRaw ushr 2)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*
import kotlinx.serialization.modules.*
import kotlin.uuid.Uuid
import kotlin.test.*
import kotlin.uuid.Uuid

@OptIn(ExperimentalSerializationApi::class)
class BinarySerializationTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ private const val UUID_STRING_ALL_FF: String = "ffffffff-ffff-ffff-ffff-ffffffff
private const val UUID_STRING: String = "1b3e4567-e99b-13d3-a476-446657420000"
internal const val SOME_UUID_STRING: String = "1b3e4567-e99b-13d3-a476-446657420000"


class UUIDTest {
@Test
fun testZero() {
Expand Down

0 comments on commit e103b2c

Please sign in to comment.