From b8505a0be94ec21ee660da9eb4b75a3d40531696 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Wed, 17 Aug 2016 11:57:28 +0200 Subject: [PATCH] minor --- .../s4ke/moar/strings/EfficientString.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/engine/src/main/java/com/github/s4ke/moar/strings/EfficientString.java b/engine/src/main/java/com/github/s4ke/moar/strings/EfficientString.java index 788edc8..a979747 100644 --- a/engine/src/main/java/com/github/s4ke/moar/strings/EfficientString.java +++ b/engine/src/main/java/com/github/s4ke/moar/strings/EfficientString.java @@ -17,7 +17,7 @@ * * @author Martin Braun */ -public class EfficientString { +public class EfficientString implements Comparable { private CharSeq underlying; private int start; @@ -147,4 +147,23 @@ public String toString() { return this.underlying.subSequence( this.start, this.end ); } + @Override + public int compareTo(EfficientString o) { + //similar to Java's String comparison fn + + int len1 = this.codePointLength(); + int len2 = o.codePointLength(); + int lim = Math.min( len1, len2 ); + + int k = 0; + while ( k < lim ) { + int c1 = this.codePoint( k ); + int c2 = o.codePoint( k ); + if ( c1 != c2 ) { + return c1 - c2; + } + k++; + } + return len1 - len2; + } }