beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / mpz / get_str.c
blob08c7cd7371018f04cfc9a3e52ed713f7267298bb
1 /* mpz_get_str (string, base, mp_src) -- Convert the multiple precision
2 number MP_SRC to a string STRING of base BASE. If STRING is NULL
3 allocate space for the result. In any case, return a pointer to the
4 result. If STRING is not NULL, the caller must ensure enough space is
5 available to store the result.
7 Copyright 1991, 1993, 1994, 1996, 2000-2002, 2005, 2012 Free Software
8 Foundation, Inc.
10 This file is part of the GNU MP Library.
12 The GNU MP Library is free software; you can redistribute it and/or modify
13 it under the terms of either:
15 * the GNU Lesser General Public License as published by the Free
16 Software Foundation; either version 3 of the License, or (at your
17 option) any later version.
21 * the GNU General Public License as published by the Free Software
22 Foundation; either version 2 of the License, or (at your option) any
23 later version.
25 or both in parallel, as here.
27 The GNU MP Library is distributed in the hope that it will be useful, but
28 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
29 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
30 for more details.
32 You should have received copies of the GNU General Public License and the
33 GNU Lesser General Public License along with the GNU MP Library. If not,
34 see https://www.gnu.org/licenses/. */
36 #include <string.h> /* for strlen */
37 #include "gmp.h"
38 #include "gmp-impl.h"
39 #include "longlong.h"
41 char *
42 mpz_get_str (char *res_str, int base, mpz_srcptr x)
44 mp_ptr xp;
45 mp_size_t x_size = SIZ (x);
46 char *return_str;
47 size_t str_size;
48 size_t alloc_size = 0;
49 const char *num_to_text;
50 int i;
51 TMP_DECL;
53 if (base >= 0)
55 num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
56 if (base <= 1)
57 base = 10;
58 else if (base > 36)
60 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
61 if (base > 62)
62 return NULL;
65 else
67 base = -base;
68 if (base <= 1)
69 base = 10;
70 else if (base > 36)
71 return NULL;
72 num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
75 /* allocate string for the user if necessary */
76 if (res_str == NULL)
78 /* digits, null terminator, possible minus sign */
79 MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base);
80 alloc_size += 1 + (x_size<0);
81 res_str = (char *) (*__gmp_allocate_func) (alloc_size);
83 return_str = res_str;
85 if (x_size < 0)
87 *res_str++ = '-';
88 x_size = -x_size;
91 /* mpn_get_str clobbers its input on non power-of-2 bases */
92 TMP_MARK;
93 xp = PTR (x);
94 if (! POW2_P (base))
96 xp = TMP_ALLOC_LIMBS (x_size | 1); /* |1 in case x_size==0 */
97 MPN_COPY (xp, PTR (x), x_size);
100 str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
101 ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
103 /* Convert result to printable chars. */
104 for (i = 0; i < str_size; i++)
105 res_str[i] = num_to_text[(int) res_str[i]];
106 res_str[str_size] = 0;
108 TMP_FREE;
110 /* if allocated then resize down to the actual space required */
111 if (alloc_size != 0)
113 size_t actual_size = str_size + 1 + (res_str - return_str);
114 ASSERT (actual_size == strlen (return_str) + 1);
115 __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size,
116 char);
118 return return_str;