1 /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2                  2016,2018 MariaDB Corporation AB
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public
15    License along with this library; if not, write to the Free
16    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17    MA 02111-1301, USA */
18 
19 #include <ma_global.h>
20 #include "ma_string.h"
21 #include <ctype.h>
22 
23 static char NEAR _dig_vec[] =
24   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
25 
ma_ll2str(long long val,char * dst,int radix)26 char *ma_ll2str(long long val,char *dst,int radix)
27 {
28   char buffer[65];
29   register char *p;
30   long long_val;
31 
32   if (radix < 0)
33   {
34     if (radix < -36 || radix > -2) return (char*) 0;
35     if (val < 0) {
36       *dst++ = '-';
37       val = 0ULL - val;
38     }
39     radix = -radix;
40   }
41   else
42   {
43     if (radix > 36 || radix < 2) return (char*) 0;
44   }
45   if (val == 0)
46   {
47     *dst++='0';
48     *dst='\0';
49     return dst;
50   }
51   p = &buffer[sizeof(buffer)-1];
52   *p = '\0';
53 
54   while ((ulonglong) val > (ulonglong) LONG_MAX)
55   {
56     ulonglong quo=(ulonglong) val/(uint) radix;
57     uint rem= (uint) (val- quo* (uint) radix);
58     *--p = _dig_vec[rem];
59     val= quo;
60   }
61   long_val= (long) val;
62   while (long_val != 0)
63   {
64     long quo= long_val/radix;
65     *--p = _dig_vec[(uchar) (long_val - quo*radix)];
66     long_val= quo;
67   }
68   while ((*dst++ = *p++) != 0) ;
69   return dst-1;
70 }
71