Fixed register sorting
authorChristian Femers <femers@in.tum.de>
Sun, 6 Oct 2019 19:23:09 +0000 (21:23 +0200)
committerChristian Femers <femers@in.tum.de>
Sun, 6 Oct 2019 19:23:09 +0000 (21:23 +0200)
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/launch/MachineRegisterGroup.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/NumberRespectingStringComparator.java [new file with mode: 0644]

index 081ec41..bb7ed26 100644 (file)
@@ -1,6 +1,5 @@
 package net.mograsim.plugin.launch;
 
-import java.util.Collections;
 import java.util.List;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -15,6 +14,7 @@ import org.eclipse.debug.core.model.IRegisterGroup;
 import net.mograsim.machine.Machine;
 import net.mograsim.machine.Register;
 import net.mograsim.plugin.MograsimActivator;
+import net.mograsim.plugin.util.NumberRespectingStringComparator;
 
 public class MachineRegisterGroup extends PlatformObject implements IRegisterGroup
 {
@@ -25,9 +25,9 @@ public class MachineRegisterGroup extends PlatformObject implements IRegisterGro
        {
                this.stackFrame = stackFrame;
                Set<Register> machineRegisters = getMachine().getDefinition().getRegisters();
-               List<MachineRegister> registersModifiable = machineRegisters.stream().map(r -> new MachineRegister(this, r))
-                               .collect(Collectors.toList());
-               this.registers = Collections.unmodifiableList(registersModifiable);
+               this.registers = machineRegisters.stream()
+                               .sorted((r1, r2) -> NumberRespectingStringComparator.CASE_SENSITIVE.compare(r1.id(), r2.id()))
+                               .map(r -> new MachineRegister(this, r)).collect(Collectors.toUnmodifiableList());
        }
 
        @Override
@@ -62,7 +62,6 @@ public class MachineRegisterGroup extends PlatformObject implements IRegisterGro
        @Override
        public IRegister[] getRegisters() throws DebugException
        {
-               // TODO sort
                return registers.toArray(IRegister[]::new);
        }
 
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/NumberRespectingStringComparator.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/util/NumberRespectingStringComparator.java
new file mode 100644 (file)
index 0000000..c2deaf7
--- /dev/null
@@ -0,0 +1,55 @@
+package net.mograsim.plugin.util;
+
+import java.math.BigInteger;
+import java.util.Comparator;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Compares Strings respecting integers that appear in the strings with positions in common.<br>
+ * Note that 0003 , 03 and 3 are considered to be at the same level; however if there is no further difference, lexicographic ordering is
+ * applied to ensure the comparator meets the {@link Comparator contract} (this will sort "foor_02" before "foo_2").
+ *
+ * @author Christian Femers
+ *
+ */
+public class NumberRespectingStringComparator implements Comparator<String>
+{
+       public static final NumberRespectingStringComparator CASE_SENSITIVE = new NumberRespectingStringComparator();
+
+       private static final Pattern PATTERN = Pattern.compile("\\d+", Pattern.DOTALL);
+
+       private final Comparator<CharSequence> comp = CharSequence::compare;
+
+       private NumberRespectingStringComparator()
+       {
+       }
+
+       @Override
+       public int compare(String o1, String o2)
+       {
+               Matcher m1 = PATTERN.matcher(o1);
+               Matcher m2 = PATTERN.matcher(o2);
+               int pos1 = 0;
+               int pos2 = 0;
+               while (m1.find() && m2.find() && m1.start() - pos1 == m2.start() - pos2)
+               {
+                       int charsCompRes = comp.compare(o1.subSequence(pos1, m1.start()), o2.subSequence(pos2, m2.start()));
+                       if (charsCompRes != 0)
+                               return charsCompRes;
+
+                       BigInteger num1 = new BigInteger(m1.group());
+                       BigInteger num2 = new BigInteger(m2.group());
+                       int compRes = num1.compareTo(num2);
+                       if (compRes != 0)
+                               return compRes;
+
+                       pos1 = m1.end();
+                       pos2 = m2.end();
+               }
+               int restCompRes = comp.compare(o1.substring(pos1), o2.substring(pos2));
+               if (restCompRes != 0)
+                       return restCompRes;
+               return comp.compare(o1, o2);
+       }
+}