zafarkhaja/jsemver

Support for partial version strings?

ilg-ul opened this issue · 3 comments

I did not check the specs, but in my application I must process partial version strings, like "1.2".

The current parser accepts only "1.2.0".

I patched the VersionParser.parseVersionCore() to:

    private NormalVersion parseVersionCore() {
        int major = Integer.parseInt(numericIdentifier());
        consumeNextCharacter(DOT);
        int minor = Integer.parseInt(numericIdentifier());
        int patch;
        try {
            consumeNextCharacter(DOT);
            patch = Integer.parseInt(numericIdentifier());
        } catch (UnexpectedCharacterException e) {
            patch = 0;
        }
        return new NormalVersion(major, minor, patch);
    }

but perhaps there are better solution.

A better solution would be not to use exceptions for flow control. Instead you could use lookahead() method on the chars stream to check for the EOI token, something along the line

int patch = EOI.isMatchedBy(chars.lookahead(1)) ? 0 : Integer.parseInt(numericIdentifier());

This is sort of a duplicate of the issue #15. I'll be providing the feature with the next release.

@zafarkhaja any ETA?