比较版本号字符串(主要,次要,修订版,测试版) [英] Comparing version number strings (major, minor, revision, beta)

查看:56
本文介绍了比较版本号字符串(主要,次要,修订版,测试版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与设备的固件通信的应用程序.由于固件的更改,其版本格式为 {major}.{minor}.{revision} [beta [{beta}]] .举几个例子,当前版本是 0.4.7beta ,其后是 0.4.7beta2 ,偶尔是 0.4.7 通过 0.4.8beta .不幸的是,该固件的版本控制格式不受我的控制,因此无法更改.

I have an application that communicates with the firmware of a device. As there are changes to the firmware, it is versioned with the format {major}.{minor}.{revision}[beta[{beta}]]. To give you a few examples, the current version is 0.4.7beta which will be followed by 0.4.7beta2 and occasionally 0.4.7, followed by 0.4.8beta. The versioning format of the firmware is unfortunately not under my control, so I can not change it.

我需要一种相互比较固件的方法.基本上,我需要一个功能

I need a way of comparing firmwares with each other. Basically, I need a function

boolean isFirmwareNewer(String testFW, String baseFW);

到目前为止,我所做的就是将这种格式转换为简单的 int .因此, 0.4.7beta2 将成为 00040702 (每个级别2位数字).问题是,

What I did so far was to convert this format to a simple int. So 0.4.7beta2 will become 00040702 (2 digits for each level). The problem is, that

  1. 我的代码难以阅读(> 40行和3种方法)
  2. 我敢肯定,对此有一个优雅的解决方案(也许使用正则表达式?)
  3. 我想要一个通配符 0.0.0 ,根据定义,它是较新的
  4. 这会处理错误的Beta版本( 0.4.7beta2 不比 0.4.7 更新).这很容易解决( if(testFW.contains("beta"))testFWValue-= 100; ,但是它也不是很优雅.
  1. My code is difficult to read (>40 lines and 3 methods)
  2. I am sure, there is a elegant solution to this (maybe using regex?)
  3. I want to have a wildcard 0.0.0 which is newer by definition
  4. This handles the beta versions incorrect (0.4.7beta2 is not newer than 0.4.7). That is easy to account for (if (testFW.contains("beta")) testFWValue -= 100;, but it's not really elegant as well.

你们通常如何做到这一点(或者您将如何做到)?

How do you guys do this normally (or how would you do it)?

如果您愿意,我可以附加当前正在使用的代码,但是正如我所说,它是40行以上的代码,而且不是真正可读的(这就是我正在寻找一种更好的方法来执行此操作的原因)

If you want, I can attach the code I am currently working with, but as I said, it's >40 lines of code and not really readable (which is the reason why I am looking for a better way to do this).

推荐答案

这里是一个建议:

static int[] getVersionNumbers(String ver) {
    Matcher m = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)(beta(\\d*))?")
                       .matcher(ver);
    if (!m.matches())
        throw new IllegalArgumentException("Malformed FW version");

    return new int[] { Integer.parseInt(m.group(1)),  // major
            Integer.parseInt(m.group(2)),             // minor
            Integer.parseInt(m.group(3)),             // rev.
            m.group(4) == null ? Integer.MAX_VALUE    // no beta suffix
                    : m.group(5).isEmpty() ? 1        // "beta"
                    : Integer.parseInt(m.group(5))    // "beta3"
    };
}

static boolean isFirmwareNewer(String testFW, String baseFW) {

    int[] testVer = getVersionNumbers(testFW);
    int[] baseVer = getVersionNumbers(baseFW);

    for (int i = 0; i < testVer.length; i++)
        if (testVer[i] != baseVer[i])
            return testVer[i] > baseVer[i];

    return true;
}

它使用了一些技巧,并按如下方式翻译了beta部分:

It uses a little trick and translates the beta-part as follows:

  • " (无beta后缀)→Beta MAX_INT
  • "beta" →Beta 1(因为它以"beta2"开头)
  • "betaX" →Beta X
  • "" (no beta suffix) → Beta MAX_INT
  • "beta" → Beta 1 (since it preceeds "beta2")
  • "betaX" → Beta X

请注意,如果两个版本相同,则返回 true .

Note that it return true if both versions are identical.

这篇关于比较版本号字符串(主要,次要,修订版,测试版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆