Skip to content

Commit

Permalink
Add Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
noonmaru committed Sep 20, 2020
1 parent 9cfeadc commit 755ebb2
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) 2020 Noonmaru
*
* Licensed under the General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/gpl-3.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.noonmaru.tap.util

import java.util.*

/**
* This is an updated version with enhancements made by Daniel Migowski,
* Andre Bogus, and David Koelle. Updated by David Koelle in 2017.
*
* To use this class:
* Use the static "sort" method from the java.util.Collections class:
* Collections.sort(your list, new AlphanumComparator());
*/
object AlphanumComparator : Comparator<String?> {
private fun isDigit(ch: Char): Boolean {
return ch.toInt() in 48..57
}

/** Length of string is passed in for improved efficiency (only need to calculate it once) */
private fun getChunk(s: String, slength: Int, marker: Int): String {
var m = marker
val chunk = StringBuilder()
var c = s[m]
chunk.append(c)
m++
if (isDigit(c)) {
while (m < slength) {
c = s[m]
if (!isDigit(c)) break
chunk.append(c)
m++
}
} else {
while (m < slength) {
c = s[m]
if (isDigit(c)) break
chunk.append(c)
m++
}
}
return chunk.toString()
}

override fun compare(s1: String?, s2: String?): Int {
if (s1 == null || s2 == null) {
return 0
}
var thisMarker = 0
var thatMarker = 0
val s1Length = s1.length
val s2Length = s2.length
while (thisMarker < s1Length && thatMarker < s2Length) {
val thisChunk = getChunk(s1, s1Length, thisMarker)
thisMarker += thisChunk.length
val thatChunk = getChunk(s2, s2Length, thatMarker)
thatMarker += thatChunk.length

// If both chunks contain numeric characters, sort them numerically
var result: Int
if (isDigit(thisChunk[0]) && isDigit(thatChunk[0])) {
// Simple chunk comparison by length.
val thisChunkLength = thisChunk.length
result = thisChunkLength - thatChunk.length
// If equal, the first different number counts
if (result == 0) {
for (i in 0 until thisChunkLength) {
result = thisChunk[i] - thatChunk[i]
if (result != 0) {
return result
}
}
}
} else {
result = thisChunk.compareTo(thatChunk)
}
if (result != 0) return result
}
return s1Length - s2Length
}
}
46 changes: 46 additions & 0 deletions api/src/main/kotlin/com/github/noonmaru/tap/util/VersionSupport.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2020 Noonmaru
*
* Licensed under the General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/gpl-3.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.noonmaru.tap.util

import kotlin.math.max

/**
* 버전을 비교합니다.
*
* 0.1 < 0.1.1
*
* 0.1 == 0.01
*/
infix fun String.compareVersion(other: String): Int {
val split = split('.')
val otherSplit = other.split('.')

loop@ for (i in 0 until max(split.count(), otherSplit.count())) {
val a = split.getOrNull(i) ?: "0"
val b = otherSplit.getOrNull(i) ?: "0"
var compare = 0

kotlin.runCatching {
compare = a.toLong(0x10).compareTo(b.toLong(0x10))
}.onFailure {
compare = AlphanumComparator.compare(a, b)
}
if (compare != 0) return compare
}

return 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2020 Noonmaru
*
* Licensed under the General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/gpl-3.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.github.noonmaru.tap.util

import org.junit.Assert.assertEquals
import org.junit.Test

class VersionSupportKtTest {
@Test
fun test() {
assertEquals(0, "1".compareVersion("1"))
assertEquals(0, "1.0".compareVersion("1.0"))
assertEquals(0, "v1.0.0".compareVersion("v1.0.0"))
assertEquals(0, "0.01.0".compareVersion("0.1.0"))
assertEquals(-1, "0".compareVersion("1"))
assertEquals(-1, "0".compareVersion("0.1"))
assertEquals(-1, "0.1".compareVersion("0.2"))
assertEquals(-1, "0.0.1".compareVersion("0.1"))
assertEquals(-1, "0.1.0".compareVersion("0.012.0"))
assertEquals(1, "1".compareVersion("0"))
assertEquals(1, "1".compareVersion("0.1"))
assertEquals(1, "0.1".compareVersion("0.0.1"))
assertEquals(1, "0.2.0".compareVersion("0.01.0"))
}
}

0 comments on commit 755ebb2

Please sign in to comment.