Add mpz_inp_str to mini-gmp

Austyn Krutsinger akrutsinger at gmail.com
Thu Jul 7 10:45:31 UTC 2016


According to current documentation mpz_set_str
<https://gmplib.org/manual/Assigning-Integers.html#index-mpz_005fset_005fstr>
allows spaces within the string and mpz_inp_str
<https://gmplib.org/manual/I_002fO-of-Integers.html#I_002fO-of-Integers>
only allows preceding white space. Torbjörn, had mentioned whitespace in an
inputted string is a mis-feature in the main GMP. Just curious what the
intended function of GMP actually is with respect to strings.

Additionally, the 6.1.1 documentation says that the mpz_str_ functions
should allow for bases from 2 to 62. Currently mini-gmp only allows for
bases from 2 to 36. If there is any intent on allowing mini-gmp to support
strings with base from 2 to 62, I will write up a patch and submit for
review.

--Austyn

On Wed, Jul 6, 2016 at 9:28 PM, Austyn Krutsinger <akrutsinger at gmail.com>
wrote:

>
>
> 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
>



-- 

--Austyn


More information about the gmp-devel mailing list