- // lhs = left hand side, rhs = right hand side
- const char *lhs = A;
- const char *rhs = B;
-
- /* Consider epochs. They need special handling because an epoch
- must not be compared against the first element of the real version.
- This works okay when both sides have an epoch but when only one
- does it must compare the missing epoch to 0 */
- for (;lhs != AEnd && *lhs != ':'; lhs++);
- for (;rhs != BEnd && *rhs != ':'; rhs++);
-
- // Parse the epoch out
- unsigned long lhsEpoch = 0;
- unsigned long rhsEpoch = 0;
- if (lhs != AEnd && *lhs == ':')
- lhsEpoch = StrToLong(A,lhs);
- if (rhs != BEnd && *rhs == ':')
- rhsEpoch = StrToLong(B,rhs);
- if (lhsEpoch != rhsEpoch)
- {
- if (lhsEpoch > rhsEpoch)
- return 1;
- return -1;
- }
-
- /* Iterate over the whole string
- What this does is to spilt the whole string into groups of
- numeric and non numeric portions. For instance:
- a67bhgs89
- Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
- 2.7.2-linux-1
- Has '2', '.', '7', '.' ,'-linux-','1' */
- lhs = A;
- rhs = B;
- while (lhs != AEnd && rhs != BEnd)
- {
- // Starting points
- const char *Slhs = lhs;
- const char *Srhs = rhs;
-
- // Compute ending points were we have passed over the portion
- bool Digit = (isdigit(*lhs) > 0?true:false);
- for (;lhs != AEnd && (isdigit(*lhs) > 0?true:false) == Digit; lhs++);
- for (;rhs != BEnd && (isdigit(*rhs) > 0?true:false) == Digit; rhs++);
-
- if (Digit == true)
- {
- // If the lhs has a digit and the rhs does not then true
- if (rhs - Srhs == 0)
- return -1;
-
- // Generate integers from the strings.
- unsigned long Ilhs = StrToLong(Slhs,lhs);
- unsigned long Irhs = StrToLong(Srhs,rhs);
- if (Ilhs != Irhs)
- {
- if (Ilhs > Irhs)
- return 1;
- return -1;
- }
- }
- else
- {
- // They are equal length so do a straight text compare
- for (;Slhs != lhs && Srhs != rhs; Slhs++, Srhs++)
- {
- if (*Slhs != *Srhs)
- {
- /* We need to compare non alpha chars as higher than alpha
- chars (a < !) This is so things like 7.6p2-4 and 7.6-0
- compare higher as well as . and -. I am not sure how
- the dpkg code manages to achive the != '-' test, but it
- is necessary. */
- int lc = *Slhs;
- int rc = *Srhs;
- if (isalpha(lc) == 0 && lc != '-') lc += 256;
- if (isalpha(rc) == 0 && rc != '-') rc += 256;
- if (lc > rc)
- return 1;
- return -1;
- }
- }
-
- // If the lhs is shorter than the right it is 'less'
- if (lhs - Slhs < rhs - Srhs)
- return -1;
-
- // If the lhs is longer than the right it is 'more'
- if (lhs - Slhs > rhs - Srhs)
- return 1;
- }
- }
-
- // The strings must be equal
- if (lhs == AEnd && rhs == BEnd)
- return 0;
-
- // lhs is shorter
- if (lhs == AEnd)
- return -1;
-
- // rhs is shorter
- if (rhs == BEnd)
- return 1;
-
- // Shouldnt happen
- return 1;