Core Function StrVersCmp

From Sputnik Wiki
Jump to: navigation, search
StrVersCmp( <str1>, <str2>, <ignoreCase> )

Contents

Description

String comparison holding name and indices/version numbers.

The strversmp() function shall compare two strings in a similar manner to strcmp(). If str1 and str2 contain no digits, strversmp() shall behave as strcmp().

The strings are compared by scanning from left to right. If a digit or sequence of digits is encountered in both strings at the same position, the digit sequence is specially compared, as described below. If the digit sequences compared equal, the string comparison resumes in both s1 and s2 after the digit sequence.

Digit sequences are classified as either "integral" or "fractional". A fractional digit sequence begins with a '0'; otherwise the digit sequence shall be treated as an integral digit sequence.

If two integral digit sequences are encountered, they shall be compared as integers for equality. A fractional digit sequence shall always compare less than an integral digit sequence. If two fractional digit sequences are being compared, then if the common prefix contains only leading zeroes, the longer part shall compare less than the shorter; otherwise the comparison shall be strictly numeric.

Thus, the task of strverscmp is to compare two strings and find the "right" order, while strcmp only finds the lexicographic order.

What this function does is the following. If both strings are equal, return 0. Otherwise find the position between two chars with the property that before it both strings are equal, while directly after it there is a difference. Find the largest consecutive digit strings containing (or starting at, or ending at) this position. If one or both of these is empty, then return what strcmp would have returned (numerical ordering of char values). Otherwise, compare both digit strings numerically, where digit strings with one or more leading zeroes are interpreted as if they have a decimal point in front (so that in particular digit strings with more leading zeroes come before digit strings with fewer leading zeroes). Thus, the ordering is 000, 00, 01, 010, 09, 0, 1, 9, 10.

Parameters

str1

The first string.

str2

The second string.

ignoreCase

Optional; Do case insensitive match

true = Ignore case and match case insensitively

false = Case must be an exact match

Return Value

Returns an integer less than, equal to, or greater than zero if str1 is found, respectively, to be earlier than, equal to, or later than str2.

Remarks

None.

Example

say strverscmp( "no digit", "no digit");       // 0 /* same behavior as strcmp */
say strverscmp( "item#99", "item#100");        // < 0 /* same prefix, but 99 < 100 */
say strverscmp( "alpha1", "alpha001");         // > 0 /* fractional part inferior to integral */
say strverscmp( "part1_f012", "part1_f01");	   // > 0 /* two fractional parts */
say strverscmp( "foo.009", "foo.0");           // < 0 /* two fractional parts but with leading zeroes only */
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox