瀏覽代碼

[libc] Add strtoull()

Don't implement strtoul() on top of strtoull() as strtoull() is much
bigger and only used on linux currently. Instead refactor most of the
logic out of strtoul() into static inlines and reuse that. Also put it
in a separate object so it won't get linked in.

Signed-off-by: Piotr Jaroszyński <p.jaroszynski@gmail.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Piotr Jaroszyński 14 年之前
父節點
當前提交
1822b1deb9
共有 3 個文件被更改,包括 90 次插入22 次删除
  1. 2
    22
      src/core/misc.c
  2. 48
    0
      src/core/strtoull.c
  3. 40
    0
      src/include/stdlib.h

+ 2
- 22
src/core/misc.c 查看文件

@@ -37,30 +37,10 @@ unsigned long strtoul ( const char *p, char **endp, int base ) {
37 37
 	unsigned long ret = 0;
38 38
 	unsigned int charval;
39 39
 
40
-	while ( isspace ( *p ) )
41
-		p++;
42
-
43
-	if ( base == 0 ) {
44
-		base = 10;
45
-		if ( *p == '0' ) {
46
-			p++;
47
-			base = 8;
48
-			if ( ( *p | 0x20 ) == 'x' ) {
49
-				p++;
50
-				base = 16;
51
-			}
52
-		}
53
-	}
40
+	base = strtoul_base ( &p, base );
54 41
 
55 42
 	while ( 1 ) {
56
-		charval = *p;
57
-		if ( charval >= 'a' ) {
58
-			charval = ( charval - 'a' + 10 );
59
-		} else if ( charval >= 'A' ) {
60
-			charval = ( charval - 'A' + 10 );
61
-		} else if ( charval <= '9' ) {
62
-			charval = ( charval - '0' );
63
-		}
43
+		charval = strtoul_charval ( *p );
64 44
 		if ( charval >= ( unsigned int ) base )
65 45
 			break;
66 46
 		ret = ( ( ret * base ) + charval );

+ 48
- 0
src/core/strtoull.c 查看文件

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>
3
+ * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
4
+ *
5
+ * This program is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU General Public License as
7
+ * published by the Free Software Foundation; either version 2 of the
8
+ * License, or any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful, but
11
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ */
19
+
20
+FILE_LICENCE ( GPL2_OR_LATER );
21
+
22
+#include <stdlib.h>
23
+#include <ctype.h>
24
+
25
+/*
26
+ * Despite being exactly the same as strtoul() except the long long instead of
27
+ * long it ends up being much bigger so provide a separate implementation in a
28
+ * separate object so that it won't be linked in if not used.
29
+ */
30
+unsigned long long strtoull ( const char *p, char **endp, int base ) {
31
+	unsigned long long ret = 0;
32
+	unsigned int charval;
33
+
34
+	base = strtoul_base ( &p, base );
35
+
36
+	while ( 1 ) {
37
+		charval = strtoul_charval ( *p );
38
+		if ( charval >= ( unsigned int ) base )
39
+			break;
40
+		ret = ( ( ret * base ) + charval );
41
+		p++;
42
+	}
43
+
44
+	if ( endp )
45
+		*endp = ( char * ) p;
46
+
47
+	return ( ret );
48
+}

+ 40
- 0
src/include/stdlib.h 查看文件

@@ -5,6 +5,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
5 5
 
6 6
 #include <stdint.h>
7 7
 #include <assert.h>
8
+#include <ctype.h>
8 9
 
9 10
 /*****************************************************************************
10 11
  *
@@ -13,7 +14,46 @@ FILE_LICENCE ( GPL2_OR_LATER );
13 14
  ****************************************************************************
14 15
  */
15 16
 
17
+static inline int strtoul_base ( const char **pp, int base )
18
+{
19
+	const char *p = *pp;
20
+
21
+	while ( isspace ( *p ) )
22
+		p++;
23
+
24
+	if ( base == 0 ) {
25
+		base = 10;
26
+		if ( *p == '0' ) {
27
+			p++;
28
+			base = 8;
29
+			if ( ( *p | 0x20 ) == 'x' ) {
30
+				p++;
31
+				base = 16;
32
+			}
33
+		}
34
+	}
35
+
36
+	*pp = p;
37
+
38
+	return base;
39
+}
40
+
41
+static inline unsigned int strtoul_charval ( unsigned int charval )
42
+{
43
+	if ( charval >= 'a' ) {
44
+		charval = ( charval - 'a' + 10 );
45
+	} else if ( charval >= 'A' ) {
46
+		charval = ( charval - 'A' + 10 );
47
+	} else if ( charval <= '9' ) {
48
+		charval = ( charval - '0' );
49
+	}
50
+
51
+	return charval;
52
+}
53
+
16 54
 extern unsigned long strtoul ( const char *p, char **endp, int base );
55
+extern unsigned long long strtoull ( const char *p, char **endp, int base );
56
+
17 57
 
18 58
 /*****************************************************************************
19 59
  *

Loading…
取消
儲存