Skip to content

Instantly share code, notes, and snippets.

@Sciss
Last active April 26, 2017 18:43
Show Gist options
  • Select an option

  • Save Sciss/28249b2c4515af191197 to your computer and use it in GitHub Desktop.

Select an option

Save Sciss/28249b2c4515af191197 to your computer and use it in GitHub Desktop.
def compareName(s1: String, s2: String): Int = {
val n1 = s1.length
val n2 = s2.length
val min = math.min(n1, n2)
var i = 0; while (i < min) {
var c1 = s1.charAt(i)
var c2 = s2.charAt(i)
var d1 = Character.isDigit(c1)
var d2 = Character.isDigit(c2)
if (d1 && d2) {
// Enter numerical comparison
var c3, c4: Char = 'x'
do {
i += 1
c3 = if (i < n1) s1.charAt(i) else 'x'
c4 = if (i < n2) s2.charAt(i) else 'x'
d1 = Character.isDigit(c3);
d2 = Character.isDigit(c4);
} while ( d1 && d2 && c3 == c4 )
if (d1 != d2) return if (d1) 1 else -1
if (c1 != c2) return c1 - c2
if (c3 != c4) return c3 - c4
i -= 1
} else if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2
}
}
}
i += 1
}
n1 - n2
}
assert(compareName("5", "5") == 0)
assert(compareName("5", "6") == -1)
assert(compareName("6", "5") == +1)
assert(compareName("x5", "x5") == 0)
assert(compareName("x5", "x6") == -1)
assert(compareName("x6", "x5") == +1)
assert(compareName("x5y", "x5y") == 0)
assert(compareName("x5y", "x6y") == -1)
assert(compareName("x6y", "x5y") == +1)
assert(compareName("5y", "5y") == 0)
assert(compareName("5y", "6y") == -1)
assert(compareName("6y", "5y") == +1)
assert(compareName("5", "51") == -1)
assert(compareName("51", "5") == +1)
assert(compareName("6", "51") == -1)
assert(compareName("51", "6") == +1)
assert(compareName("x5", "x51") == -1)
assert(compareName("x51", "x5") == +1)
assert(compareName("x6", "x51") == -1)
assert(compareName("x51", "x6") == +1)
assert(compareName("x5y", "x51y") == -1)
assert(compareName("x51y", "x5y") == +1)
assert(compareName("x6y", "x51y") == -1)
assert(compareName("x51y", "x6y") == +1)
@Sciss

Sciss commented Apr 26, 2017

Copy link
Copy Markdown
Author

this has a bug; fixed in ImperfectReconstruction > DifferenceProcess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment