cpp-semver icon indicating copy to clipboard operation
cpp-semver copied to clipboard

It's wrong when i test a semver 2.0 string

Open copperyp opened this issue 4 years ago • 4 comments
trafficstars

I using below code to test a semver 2.0 string. code:

        {
		const std::string ver1 = "1.2.3-rc.4";
		const std::string ver2 = ">1.1 <2.0";

		const bool intersected = semver::intersects(ver1, ver2);

		std::cout << "\"" << ver1
			<< "\" and \"" << ver2
			<< "\" are " << (intersected ? "" : "not ")
			<< "intersected." << std::endl;
	}
	{
		const std::string ver = "1.2.3-rc.4";
		const std::string range = "1.x || >=2.5.0 || 5.0.0 - 7.2.3";

		const bool satisfied = semver::satisfies(ver, range);

		std::cout << "\"" << ver
			<< "\" is " << (satisfied ? "" : "not ")
			<< "satisfied by "
			<< "\"" << range << "\"." << std::endl;
	}

result:

"1.2.3-rc.4" and ">1.1 <2.0" are not intersected.
"1.2.3-rc.4" is not satisfied by "1.x || >=2.5.0 || 5.0.0 - 7.2.3".

copperyp avatar Aug 06 '21 03:08 copperyp

I will take a look. I would say the "pre-release" tag like 1.2.3-rc does not work well.

easz avatar Aug 07 '21 06:08 easz

@copperyp Hi, actually I somehow follow the "convention" from npm and a pre-realse is handled differently.

you can try here https://semver.npmjs.com/ for example: Screenshot 2021-12-10 at 12 14 17

easz avatar Dec 10 '21 11:12 easz

Hi @easz . thanks you for u reply. it's maybe own misinterpretive. I check the rule from "semver.org". it's below... ` Precedence refers to how versions are compared to each other when ordered.

Precedence MUST be calculated by separating the version into major, minor, patch and pre-release identifiers in that order (Build metadata does not figure into precedence).

Precedence is determined by the first difference when comparing each of these identifiers from left to right as follows: Major, minor, and patch versions are always compared numerically.

Example: 1.0.0 < 2.0.0 < 2.1.0 < 2.1.1.

When major, minor, and patch are equal, a pre-release version has lower precedence than a normal version:

Example: 1.0.0-alpha < 1.0.0.

Precedence for two pre-release versions with the same major, minor, and patch version MUST be determined by comparing each dot separated identifier from left to right until a difference is found as follows:

    Identifiers consisting of only digits are compared numerically.

    Identifiers with letters or hyphens are compared lexically in ASCII sort order.

    Numeric identifiers always have lower precedence than non-numeric identifiers.

    A larger set of pre-release fields has a higher precedence than a smaller set, if all of the preceding identifiers are equal.

Example: 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-alpha.beta < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0.

` I don't know how to understand this correctly:(

copperyp avatar Jan 13 '22 04:01 copperyp

I have two interpretations:

  1. always compare major.minor.patch part at fist, then compare pre-release fields if major.minor.patch are the same
  2. pre-release version can only compared with each other or its target version

Apparently you want the option (1) and the library supports only the option (2) now :p

I run two examples on https://semver.npmjs.com/ as reference

Screenshot 2022-01-13 at 09 25 42

Screenshot 2022-01-13 at 09 29 26

To be honest I think that it is not really consistent and some how mixes two interpretations.

I would improve it as a feature request :p

easz avatar Jan 13 '22 08:01 easz