1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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.
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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
30 * "g_ascii_strtoull()" extracted from GLib 2.4.5, for use with GLibs
31 * that don't have it (e.g., GLib 1.2[.x]).
42 #include "g_ascii_strtoull.h"
45 #define G_MAXUINT64 ((guint64)G_GINT64_CONSTANT(0xFFFFFFFFFFFFFFFFU))
50 * @nptr: the string to convert to a numeric value.
51 * @endptr: if non-%NULL, it returns the character after
52 * the last character used in the conversion.
53 * @base: to be used for the conversion, 2..36 or 0
55 * Converts a string to a #guint64 value.
56 * This function behaves like the standard strtoull() function
57 * does in the C locale. It does this without actually
58 * changing the current locale, since that would not be
61 * This function is typically used when reading configuration
62 * files or other non-user input that should be locale independent.
63 * To handle input from the user you should normally use the
64 * locale-sensitive system strtoull() function.
66 * If the correct value would cause overflow, %G_MAXUINT64
67 * is returned, and %ERANGE is stored in %errno.
69 * Return value: the #guint64 value.
74 g_ascii_strtoull (const gchar *nptr,
78 /* this code is based on on the strtol(3) code from GNU libc released under
79 * the GNU Lesser General Public License.
81 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
82 * Free Software Foundation, Inc.
84 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
85 (c) == '\r' || (c) == '\t' || (c) == '\v')
86 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
87 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
88 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
89 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
90 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
91 gboolean negative, overflow;
95 const gchar *s, *save;
98 if (base == 1 || base > 36)
106 /* Skip white space. */
112 /* Check for a sign. */
122 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
125 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
136 /* Save the pointer so we can check later if anything happened. */
138 cutoff = G_MAXUINT64 / base;
139 cutlim = G_MAXUINT64 % base;
146 if (c >= '0' && c <= '9')
148 else if (ISALPHA (c))
149 c = TOUPPER (c) - 'A' + 10;
154 /* Check for overflow. */
155 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
164 /* Check if anything actually happened. */
168 /* Store in ENDPTR the address of one character
169 past the last character we converted. */
171 *endptr = (gchar*) s;
179 /* Return the result of the appropriate sign. */
180 return negative ? -ui64 : ui64;
183 /* We must handle a special case here: the base is 0 or 16 and the
184 first two characters are '0' and 'x', but the rest are no
185 hexadecimal digits. This is no error case. We return 0 and
186 ENDPTR points to the `x`. */
189 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
191 *endptr = (gchar*) &save[-1];
193 /* There was no number to convert. */
194 *endptr = (gchar*) nptr;