From ca4e5d91a96387bc270692f520b5a30c09856c16 Mon Sep 17 00:00:00 2001 From: giraffedata Date: Thu, 25 Dec 2014 20:40:38 +0000 Subject: Update Advanced series to 10.69 git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@2351 9d0c8265-081b-0410-96cb-a4ca84ce46f8 --- lib/util/matrix.c | 12 ++++--- lib/util/nstring.c | 93 ++++++++++++++++++++++++++++------------------------ lib/util/nstring.h | 3 ++ lib/util/vasprintf.c | 19 +++++++++++ 4 files changed, 81 insertions(+), 46 deletions(-) (limited to 'lib/util') diff --git a/lib/util/matrix.c b/lib/util/matrix.c index 5101f2c3..e9456e93 100644 --- a/lib/util/matrix.c +++ b/lib/util/matrix.c @@ -106,10 +106,14 @@ findLargestIthCoeff(unsigned int const n, maxSoFar = thisA; } } - if (maxSoFar < epsilon) - pm_asprintf(errorP, "Matrix equation has no unique solution. " - "(debug: coeff %u %e < %e)", i, maxSoFar, epsilon); - else { + if (maxSoFar < epsilon) { + const char * const baseMsg = "Matrix equation has no unique solution"; + if (pm_have_float_format()) + pm_asprintf(errorP, "%s. (debug: coeff %u %e < %e)", + baseMsg, i, maxSoFar, epsilon); + else + pm_asprintf(errorP, "%s", baseMsg); + } else { *istarP = maxIdx; *errorP = NULL; } diff --git a/lib/util/nstring.c b/lib/util/nstring.c index 1fe66a27..74618422 100644 --- a/lib/util/nstring.c +++ b/lib/util/nstring.c @@ -221,8 +221,8 @@ pm_vsnprintf(char * const str, const char *q = strchr(p + 1,'%'); size_t n = !q ? strlen(p) : (q - p); if (str_l < str_m) { - size_t avail = str_m - str_l; - fast_memcpy(str + str_l, p, (n > avail ? avail : n)); + size_t const avail = str_m - str_l; + fast_memcpy(str + str_l, p, (MIN(n, avail))); } p += n; str_l += n; } else { @@ -231,7 +231,6 @@ pm_vsnprintf(char * const str, size_t precision = 0; bool precision_specified; bool justify_left; - bool zero_padding; bool alternate_form; bool force_sign; bool space_for_positive; @@ -252,16 +251,18 @@ pm_vsnprintf(char * const str, argument for the c conversion is unsigned. */ - size_t number_of_zeros_to_pad = 0; + bool zero_padding; + + size_t number_of_zeros_to_pad; /* number of zeros to be inserted for numeric conversions as required by the precision or minimal field width */ - size_t zero_padding_insertion_ind = 0; + size_t zero_padding_insertion_ind; /* index into tmp where zero padding is to be inserted */ - char fmt_spec = '\0'; + char fmt_spec; /* current conversion specifier character */ str_arg = credits; @@ -272,10 +273,14 @@ pm_vsnprintf(char * const str, /* parse flags */ justify_left = false; /* initial value */ - zero_padding = false; /* initial value */ alternate_form = false; /* initial value */ force_sign = false; /* initial value */ space_for_positive = false; /* initial value */ + zero_padding = false; /* initial value */ + number_of_zeros_to_pad = 0; /* initial value */ + zero_padding_insertion_ind = 0; /* initial value */ + fmt_spec = '\0'; /* initial value */ + while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' || *p == '#' || *p == '\'') { switch (*p) { @@ -408,9 +413,7 @@ pm_vsnprintf(char * const str, else { /* memchr on HP does not like n > 2^31 !!! */ const char * q = - memchr(str_arg, '\0', - precision <= 0x7fffffff ? - precision : 0x7fffffff); + memchr(str_arg, '\0', MIN(precision, 0x7fffffff)); str_arg_l = !q ? precision : (q-str_arg); } break; @@ -570,9 +573,9 @@ pm_vsnprintf(char * const str, */ if (zero_padding_insertion_ind < str_arg_l && tmp[zero_padding_insertion_ind] == '-') { - zero_padding_insertion_ind++; + zero_padding_insertion_ind += 1; } - if (zero_padding_insertion_ind+1 < str_arg_l && + if (zero_padding_insertion_ind + 1 < str_arg_l && tmp[zero_padding_insertion_ind] == '0' && (tmp[zero_padding_insertion_ind+1] == 'x' || tmp[zero_padding_insertion_ind+1] == 'X') ) { @@ -580,7 +583,7 @@ pm_vsnprintf(char * const str, } } { - size_t num_of_digits = + size_t const num_of_digits = str_arg_l - zero_padding_insertion_ind; if (alternate_form && fmt_spec == 'o' /* unless zero is already the first character */ @@ -606,9 +609,10 @@ pm_vsnprintf(char * const str, } /* zero padding to specified minimal field width? */ if (!justify_left && zero_padding) { - int n = + int const n = min_field_width - (str_arg_l+number_of_zeros_to_pad); - if (n > 0) number_of_zeros_to_pad += n; + if (n > 0) + number_of_zeros_to_pad += n; } } break; case 'f': { @@ -654,12 +658,12 @@ pm_vsnprintf(char * const str, if (!justify_left) { /* left padding with blank or zero */ - int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); + int n = min_field_width - (str_arg_l + number_of_zeros_to_pad); if (n > 0) { if (str_l < str_m) { - size_t avail = str_m-str_l; - fast_memset(str+str_l, (zero_padding ? '0' : ' '), - (n > avail ? avail : n)); + size_t const avail = str_m - str_l; + fast_memset(str + str_l, (zero_padding ? '0' : ' '), + (MIN(n, avail))); } str_l += n; } @@ -673,27 +677,31 @@ pm_vsnprintf(char * const str, */ zero_padding_insertion_ind = 0; } else { - /* insert first part of numerics (sign or '0x') before - zero padding - */ - int n = zero_padding_insertion_ind; - if (n > 0) { - if (str_l < str_m) { - size_t avail = str_m-str_l; - fast_memcpy(str+str_l, str_arg, (n>avail?avail:n)); + { + /* insert first part of numerics (sign or '0x') before + zero padding + */ + int const n = zero_padding_insertion_ind; + if (n > 0) { + if (str_l < str_m) { + size_t const avail = str_m - str_l; + fast_memcpy(str + str_l, str_arg, (MIN(n, avail))); + } + str_l += n; } - str_l += n; } - /* insert zero padding as requested by the precision - or min field width - */ - n = number_of_zeros_to_pad; - if (n > 0) { - if (str_l < str_m) { - size_t avail = str_m - str_l; - fast_memset(str + str_l, '0', (n > avail ? avail : n)); + { + /* insert zero padding as requested by the precision + or min field width + */ + int const n = number_of_zeros_to_pad; + if (n > 0) { + if (str_l < str_m) { + size_t const avail = str_m - str_l; + fast_memset(str + str_l, '0', (MIN(n, avail))); + } + str_l += n; } - str_l += n; } } /* insert formatted string (or as-is conversion specifier @@ -706,7 +714,7 @@ pm_vsnprintf(char * const str, size_t const avail = str_m-str_l; fast_memcpy(str + str_l, str_arg + zero_padding_insertion_ind, - (n > avail ? avail : n)); + MIN(n, avail)); } str_l += n; } @@ -714,11 +722,12 @@ pm_vsnprintf(char * const str, /* insert right padding */ if (justify_left) { /* right blank padding to the field width */ - int n = min_field_width - (str_arg_l+number_of_zeros_to_pad); + int const n = + min_field_width - (str_arg_l + number_of_zeros_to_pad); if (n > 0) { if (str_l < str_m) { - size_t avail = str_m-str_l; - fast_memset(str+str_l, ' ', (n>avail?avail:n)); + size_t const avail = str_m - str_l; + fast_memset(str+str_l, ' ', (MIN(n, avail))); } str_l += n; } @@ -730,7 +739,7 @@ pm_vsnprintf(char * const str, of overwriting the last character (shouldn't happen, but just in case) */ - str[str_l <= str_m-1 ? str_l : str_m-1] = '\0'; + str[MIN(str_l, str_m - 1)] = '\0'; } *sizeP = str_l; } diff --git a/lib/util/nstring.h b/lib/util/nstring.h index 28adda94..7238a76e 100644 --- a/lib/util/nstring.h +++ b/lib/util/nstring.h @@ -175,6 +175,9 @@ pm_vasprintf(const char ** const resultP, const char * const format, va_list args); +bool +pm_vasprintf_knows_float(void); + void pm_strfree(const char * const string); diff --git a/lib/util/vasprintf.c b/lib/util/vasprintf.c index e38252fa..a947f763 100644 --- a/lib/util/vasprintf.c +++ b/lib/util/vasprintf.c @@ -6,6 +6,8 @@ #include #include "pm_config.h" +#include "pm_c_util.h" + #include "nstring.h" @@ -38,6 +40,12 @@ pm_vasprintf(const char ** const resultP, So instead, we just allocate 4K and truncate or waste as necessary. + + Note that we don't recognize the floating point specifiers (%f, %e, %g) + - we render them as 'f', 'e', and 'g'. It would be too much work to + make this code handle those, just for the few systems on which it runs. + Instead, we have pm_vasprintf_knows_float(), and any caller that cares + enough can avoid using these specifiers where they don't work. */ size_t const allocSize = 4096; result = malloc(allocSize); @@ -56,3 +64,14 @@ pm_vasprintf(const char ** const resultP, } #endif } + + + +bool +pm_vasprintf_knows_float(void) { +#if HAVE_VASPRINTF + return true; +#else + return false; +#endif +} -- cgit 1.4.1