int bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len) { wchar_t c1, c2; size_t i = 0; for (i = 0; i < len; ++i) { c1 = bws_get_iter_value(iter1); c2 = bws_get_iter_value(iter2); if (c1 != c2) return (c1 - c2); iter1 = bws_iterator_inc(iter1, 1); iter2 = bws_iterator_inc(iter2, 1); } return (0); }
/* * Read string s and parse the string into a fixed-decimal-point number. * sign equals -1 if the number is negative (explicit plus is not allowed, * according to GNU sort's "info sort". * The number part before decimal point is in the smain, after the decimal * point is in sfrac, tail is the pointer to the remainder of the string. */ static int read_number(struct bwstring *s0, int *sign, wchar_t *smain, size_t *main_len, wchar_t *sfrac, size_t *frac_len, unsigned char *si) { bwstring_iterator s; s = bws_begin(s0); /* always end the fraction with zero, even if we have no fraction */ sfrac[0] = 0; while (iswblank(bws_get_iter_value(s))) s = bws_iterator_inc(s, 1); if (bws_get_iter_value(s) == (wchar_t)symbol_negative_sign) { *sign = -1; s = bws_iterator_inc(s, 1); } // This is '0', not '\0', do not change this while (iswdigit(bws_get_iter_value(s)) && (bws_get_iter_value(s) == L'0')) s = bws_iterator_inc(s, 1); while (bws_get_iter_value(s) && *main_len < MAX_NUM_SIZE) { if (iswdigit(bws_get_iter_value(s))) { smain[*main_len] = bws_get_iter_value(s); s = bws_iterator_inc(s, 1); *main_len += 1; } else if (symbol_thousands_sep && (bws_get_iter_value(s) == (wchar_t)symbol_thousands_sep)) s = bws_iterator_inc(s, 1); else break; } smain[*main_len] = 0; if (bws_get_iter_value(s) == (wchar_t)symbol_decimal_point) { s = bws_iterator_inc(s, 1); while (iswdigit(bws_get_iter_value(s)) && *frac_len < MAX_NUM_SIZE) { sfrac[*frac_len] = bws_get_iter_value(s); s = bws_iterator_inc(s, 1); *frac_len += 1; } sfrac[*frac_len] = 0; while (*frac_len > 0 && sfrac[*frac_len - 1] == L'0') { --(*frac_len); sfrac[*frac_len] = L'\0'; } } setsuffix(bws_get_iter_value(s),si); if ((*main_len + *frac_len) == 0) *sign = 0; return (0); }
/* * Find string suffix of format: (\.[A-Za-z~][A-Za-z0-9~]*)*$ * Set length of string before suffix. */ static void find_suffix(bwstring_iterator si, bwstring_iterator se, size_t *len) { wchar_t c; size_t clen; bool expect_alpha, sfx; sfx = false; expect_alpha = false; *len = 0; clen = 0; while ((si < se) && (c = bws_get_iter_value(si))) { if (expect_alpha) { expect_alpha = false; if (!isalpha_clocale(c) && (c != L'~')) sfx = false; } else if (c == L'.') { expect_alpha = true; if (!sfx) { sfx = true; *len = clen; } } else if (!isalnum_clocale(c) && (c != L'~')) sfx = false; si = bws_iterator_inc(si, 1); ++clen; } /* This code must be here to make the implementation compatible * with WORDING of GNU sort documentation. * But the GNU sort implementation is not following its own * documentation. GNU sort allows empty file extensions * (just dot with nothing after); but the regular expression in * their documentation does not allow empty file extensions. * We chose to make our implementation compatible with GNU sort * implementation. If they will ever fix their bug, this code * must be uncommented. Or they may choose to fix the info page, * then the code stays commented. * if (expect_alpha) sfx = false; */ if (!sfx) *len = clen; }
static int cmpversions(bwstring_iterator si1, bwstring_iterator se1, bwstring_iterator si2, bwstring_iterator se2) { int cmp, diff; while ((si1 < se1) || (si2 < se2)) { diff = 0; while (((si1 < se1) && !isdigit_clocale(bws_get_iter_value(si1))) || ((si2 < se2) && !isdigit_clocale(bws_get_iter_value(si2)))) { wchar_t c1, c2; c1 = (si1 < se1) ? bws_get_iter_value(si1) : 0; c2 = (si2 < se2) ? bws_get_iter_value(si2) : 0; cmp = cmp_chars(c1, c2); if (cmp) return (cmp); if (si1 < se1) si1 = bws_iterator_inc(si1, 1); if (si2 < se2) si2 = bws_iterator_inc(si2, 1); } while (bws_get_iter_value(si1) == L'0') si1 = bws_iterator_inc(si1, 1); while (bws_get_iter_value(si2) == L'0') si2 = bws_iterator_inc(si2, 1); while (isdigit_clocale(bws_get_iter_value(si1)) && isdigit_clocale(bws_get_iter_value(si2))) { if (!diff) diff = ((int)bws_get_iter_value(si1) - (int)bws_get_iter_value(si2)); si1 = bws_iterator_inc(si1, 1); si2 = bws_iterator_inc(si2, 1); } if (isdigit_clocale(bws_get_iter_value(si1))) return (1); if (isdigit_clocale(bws_get_iter_value(si2))) return (-1); if (diff) return (diff); } return (0); }
/* * Compare two version strings */ int vcmp(struct bwstring *s1, struct bwstring *s2) { bwstring_iterator si1, si2; wchar_t c1, c2; size_t len1, len2, slen1, slen2; int cmp_bytes, cmp_res; if (s1 == s2) return (0); cmp_bytes = bwscmp(s1, s2, 0); if (cmp_bytes == 0) return (0); len1 = slen1 = BWSLEN(s1); len2 = slen2 = BWSLEN(s2); if (slen1 < 1) return (-1); if (slen2 < 1) return (+1); si1 = bws_begin(s1); si2 = bws_begin(s2); c1 = bws_get_iter_value(si1); c2 = bws_get_iter_value(si2); if (c1 == L'.' && (slen1 == 1)) return (-1); if (c2 == L'.' && (slen2 == 1)) return (+1); if (slen1 == 2 && c1 == L'.' && bws_get_iter_value(bws_iterator_inc(si1, 1)) == L'.') return (-1); if (slen2 == 2 && c2 == L'.' && bws_get_iter_value(bws_iterator_inc(si2, 1)) == L'.') return (+1); if (c1 == L'.' && c2 != L'.') return (-1); if (c1 != L'.' && c2 == L'.') return (+1); if (c1 == L'.' && c2 == L'.') { si1 = bws_iterator_inc(si1, 1); si2 = bws_iterator_inc(si2, 1); } find_suffix(si1, bws_end(s1), &len1); find_suffix(si2, bws_end(s2), &len2); if ((len1 == len2) && (bws_iterator_cmp(si1, si2, len1) == 0)) return (cmp_bytes); cmp_res = cmpversions(si1, bws_iterator_inc(si1, len1), si2, bws_iterator_inc(si2, len2)); if (cmp_res == 0) cmp_res = cmp_bytes; return (cmp_res); }