Add mpz_inp_str to mini-gmp

Austyn Krutsinger akrutsinger at gmail.com
Wed Jul 6 16:58:27 UTC 2016


On Wed, Jul 6, 2016 at 8:16 PM, Torbjörn Granlund <tg at gmplib.org> wrote:

> Austyn Krutsinger <akrutsinger at gmail.com> writes:
>
>   Where does it make the most sense to check an input string coming from a
>   file stream? I think we just read in the stream into a buffer skipping
>   spaces and tabs then stop reading the string on the End Of File (for file
>   streams) or a newline '\n' (for stdin). This way the only duplicate code
> is
>   the isspace() check between mpz_set_str and mpz_inp_str.
>
> I'm afraid I don't understand much of your message.
>
> What do you mean with "check an input string".  Which checks?
>

Sorry for my confusing inability to articulate thoughts. I now understand
from your previous reply that mpz_inp_str() reads a string in from a FILE
stream and will stop reading when it comes to EOF or any 'space' character
such as '\t', '\n', ' '.


> The behaviour of our I/O functions should not behave differently for
> stdin than for othwer streams.
>
> Please read my text about (embedded) space in my last reply.
>

I guess I was just looking at the fact that mpz_set_str already checks for
space characters, so we might as well skip checking for space characters in
the mpz_inp_str. Makes sense to not even allow bad input strings to be read
in from any FILE streams.

Perhaps something like this simple loop would suffice in reading in the
string from within the mpz_inp_str function.

  /* Read input until end of file or a space character */
  c = getc (stream);
  while ((c != EOF) && (isspace(c) == 0))
    {
      if (nread >= size - 1)
        {
          /* increase input buffer size */
          size_t old_size = size;
          size += 100;
          buf = (char *) gmp_xrealloc (buf, old_size, size);
        }

        buf[nread++] = c;

      c = getc (stream);
    }



-- 

--Austyn


More information about the gmp-devel mailing list