source: trunk/third/gmp/mpz/get_str.c @ 18191

Revision 18191, 3.3 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18190, which included commits to RCS files with non-trunk default branches.
Line 
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.
6
7Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation,
8Inc.
9
10This file is part of the GNU MP Library.
11
12The GNU MP Library is free software; you can redistribute it and/or modify
13it under the terms of the GNU Lesser General Public License as published by
14the Free Software Foundation; either version 2.1 of the License, or (at your
15option) any later version.
16
17The GNU MP Library is distributed in the hope that it will be useful, but
18WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
20License for more details.
21
22You should have received a copy of the GNU Lesser General Public License
23along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
24the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25MA 02111-1307, USA. */
26
27#include <string.h> /* for strlen */
28#include "gmp.h"
29#include "gmp-impl.h"
30#include "longlong.h"
31
32char *
33mpz_get_str (char *res_str, int base, mpz_srcptr x)
34{
35  mp_ptr xp;
36  mp_size_t x_size = x->_mp_size;
37  char *str;
38  char *return_str;
39  size_t str_size;
40  size_t alloc_size = 0;
41  char *num_to_text;
42  int i;
43  TMP_DECL (marker);
44
45  if (base >= 0)
46    {
47      if (base == 0)
48        base = 10;
49      num_to_text = "0123456789abcdefghijklmnopqrstuvwxyz";
50    }
51  else
52    {
53      base = -base;
54      num_to_text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
55    }
56
57  /* allocate string for the user if necessary */
58  if (res_str == NULL)
59    {
60      /* digits, null terminator, possible minus sign */
61      MPN_SIZEINBASE (alloc_size, PTR(x), ABS(x_size), base);
62      alloc_size += 1 + (x_size<0);
63      res_str = (char *) (*__gmp_allocate_func) (alloc_size);
64    }
65  return_str = res_str;
66
67  if (x_size < 0)
68    {
69      *res_str++ = '-';
70      x_size = -x_size;
71    }
72
73  /* mpn_get_str clobbers its input on non power-of-2 bases */
74  TMP_MARK (marker);
75  xp = x->_mp_d;
76  if (! POW2_P (base))
77    {
78      xp = TMP_ALLOC_LIMBS (x_size + 1);  /* +1 in case x_size==0 */
79      MPN_COPY (xp, x->_mp_d, x_size);
80    }
81
82  str_size = mpn_get_str ((unsigned char *) res_str, base, xp, x_size);
83  ASSERT (alloc_size == 0 || str_size <= alloc_size - (SIZ(x) < 0));
84
85  /* might have a leading zero, skip it */
86  str = res_str;
87  if (*res_str == 0 && str_size != 1)
88    {
89      str_size--;
90      str++;
91      ASSERT (*str != 0);  /* at most one leading zero */
92    }
93
94  /* Convert result to printable chars, and move down if there was a leading
95     zero.  */
96  for (i = 0; i < str_size; i++)
97    res_str[i] = num_to_text[str[i]];
98  res_str[str_size] = 0;
99
100  TMP_FREE (marker);
101
102  /* if allocated then resize down to the actual space required */
103  if (alloc_size != 0)
104    {
105      size_t  actual_size = str_size + 1 + (res_str - return_str);
106      ASSERT (actual_size == strlen (return_str) + 1);
107      __GMP_REALLOCATE_FUNC_MAYBE_TYPE (return_str, alloc_size, actual_size,
108                                        char);
109    }
110  return return_str;
111}
Note: See TracBrowser for help on using the repository browser.