diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java index 52ade05b7c6..87e8101017e 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java @@ -363,6 +363,70 @@ public class LaunchUtils { } } + /** + * Compares two version numbers. + * Returns -1, 0, or 1 if v1 is less than, equal to, or greater than v2 respectively + * @param v1 The first version + * @param v2 The second version + * @return -1, 0, or 1 if v1 is less than, equal to, or greater than v2 respectively + * @since 4.8 + */ + public static int compareVersions(String v1, String v2) { + if (v1 == null || v2 == null) throw new NullPointerException(); + + String[] v1Parts = v1.split("\\."); //$NON-NLS-1$ + String[] v2Parts = v2.split("\\."); //$NON-NLS-1$ + for (int i = 0; i < v1Parts.length && i < v2Parts.length; i++) { + try { + int v1PartValue = Integer.parseInt(v1Parts[i]); + int v2PartValue = Integer.parseInt(v2Parts[i]); + + if (v1PartValue > v2PartValue) { + return 1; + } else if (v1PartValue < v2PartValue) { + return -1; + } + } catch (NumberFormatException e) { + // Non-integer part, ignore it + continue; + } + } + + // If we get here is means the versions are still equal + // but there could be extra parts to examine + + if (v1Parts.length < v2Parts.length) { + // v2 has extra parts, which implies v1 is a lower version (e.g., v1 = 7.9 v2 = 7.9.1) + // unless each extra part is 0, in which case the two versions are equal (e.g., v1 = 7.9 v2 = 7.9.0) + for (int i = v1Parts.length; i < v2Parts.length; i++) { + try { + if (Integer.parseInt(v2Parts[i]) != 0) { + return -1; + } + } catch (NumberFormatException e) { + // Non-integer part, ignore it + continue; + } + } + } + if (v1Parts.length > v2Parts.length) { + // v1 has extra parts, which implies v1 is a higher version (e.g., v1 = 7.9.1 v2 = 7.9) + // unless each extra part is 0, in which case the two versions are equal (e.g., v1 = 7.9.0 v2 = 7.9) + for (int i = v2Parts.length; i < v1Parts.length; i++) { + try { + if (Integer.parseInt(v1Parts[i]) != 0) { + return 1; + } + } catch (NumberFormatException e) { + // Non-integer part, ignore it + continue; + } + } + } + + return 0; + } + /** * Read from the specified stream and return what was read. * diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GdbDebugServicesFactory.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GdbDebugServicesFactory.java index 67083aab438..871bd9aace9 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GdbDebugServicesFactory.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/service/GdbDebugServicesFactory.java @@ -38,6 +38,7 @@ import org.eclipse.cdt.dsf.debug.service.IStack; import org.eclipse.cdt.dsf.debug.service.command.ICommandControl; import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin; import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch; +import org.eclipse.cdt.dsf.gdb.launching.LaunchUtils; import org.eclipse.cdt.dsf.gdb.service.command.CommandFactory_6_8; import org.eclipse.cdt.dsf.gdb.service.command.GDBControl; import org.eclipse.cdt.dsf.gdb.service.command.GDBControl_7_0; @@ -319,71 +320,9 @@ public class GdbDebugServicesFactory extends AbstractDsfDebugServicesFactory { * @since 4.8 */ protected int compareVersionWith(String version) { - return compareVersions(getVersion(), version); + return LaunchUtils.compareVersions(getVersion(), version); } - /** - * Compares two version numbers. - * Returns -1, 0, or 1 if v1 is less than, equal to, or greater than v2 respectively - * @param v1 The first version - * @param v2 The second version - * @return -1, 0, or 1 if v1 is less than, equal to, or greater than v2 respectively - */ - private static int compareVersions(String v1, String v2) { - if (v1 == null || v2 == null) throw new NullPointerException(); - - String[] v1Parts = v1.split("\\."); //$NON-NLS-1$ - String[] v2Parts = v2.split("\\."); //$NON-NLS-1$ - for (int i = 0; i < v1Parts.length && i < v2Parts.length; i++) { - try { - int v1PartValue = Integer.parseInt(v1Parts[i]); - int v2PartValue = Integer.parseInt(v2Parts[i]); - - if (v1PartValue > v2PartValue) { - return 1; - } else if (v1PartValue < v2PartValue) { - return -1; - } - } catch (NumberFormatException e) { - // Non-integer part, ignore it - continue; - } - } - - // If we get here is means the versions are still equal - // but there could be extra parts to examine - - if (v1Parts.length < v2Parts.length) { - // v2 has extra parts, which implies v1 is a lower version (e.g., v1 = 7.9 v2 = 7.9.1) - // unless each extra part is 0, in which case the two versions are equal (e.g., v1 = 7.9 v2 = 7.9.0) - for (int i = v1Parts.length; i < v2Parts.length; i++) { - try { - if (Integer.parseInt(v2Parts[i]) != 0) { - return -1; - } - } catch (NumberFormatException e) { - // Non-integer part, ignore it - continue; - } - } - } - if (v1Parts.length > v2Parts.length) { - // v1 has extra parts, which implies v1 is a higher version (e.g., v1 = 7.9.1 v2 = 7.9) - // unless each extra part is 0, in which case the two versions are equal (e.g., v1 = 7.9.0 v2 = 7.9) - for (int i = v2Parts.length; i < v1Parts.length; i++) { - try { - if (Integer.parseInt(v1Parts[i]) != 0) { - return 1; - } - } catch (NumberFormatException e) { - // Non-integer part, ignore it - continue; - } - } - } - - return 0; - } /** * A static method that will compare the version of GDB for the specified session and @@ -402,7 +341,7 @@ public class GdbDebugServicesFactory extends AbstractDsfDebugServicesFactory { IDsfDebugServicesFactory servicesFactory = ((GdbLaunch)launch).getServiceFactory(); if (servicesFactory instanceof GdbDebugServicesFactory) { String version = ((GdbDebugServicesFactory)servicesFactory).getVersion(); - if (compareVersions(minVersion, version) > 0) { + if (LaunchUtils.compareVersions(minVersion, version) > 0) { assert false; GdbPlugin.log(