/* * 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); }
/* * 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); }