]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] tools: add hex2i() function to convert hex char to int
authorWilly Tarreau <w@1wt.eu>
Mon, 2 Nov 2009 19:12:52 +0000 (20:12 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 2 Nov 2009 19:12:52 +0000 (20:12 +0100)
include/common/standard.h
src/standard.c

index 544a5c31511112db7ae692bcd3592fc1227f7995..ce3c6b85b0c96e90799c8821472f4284180faa7c 100644 (file)
@@ -117,6 +117,12 @@ extern const char *limit_r(unsigned long n, char *buffer, int size, const char *
  */
 extern int ishex(char s);
 
+/*
+ * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
+ * otherwise -1.
+ */
+extern int hex2i(int c);
+
 /*
  * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
  * invalid character is found, a pointer to it is returned. If everything is
index 6a8724a86c4c853ab68870775c9f17d3bc92ed50..aab318bb36c894e85363b70287261f2688a05dda 100644 (file)
@@ -158,6 +158,21 @@ int ishex(char s)
        return 0;
 }
 
+/*
+ * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
+ * otherwise -1. This compact form helps gcc produce efficient code.
+ */
+int hex2i(int c)
+{
+       if ((unsigned char)(c -= '0') > 9) {
+               if ((unsigned char)(c -= 'A' - '0') > 5 &&
+                   (unsigned char)(c -= 'a' - 'A') > 5)
+                       c = -11;
+               c += 10;
+       }
+       return c;
+}
+
 /*
  * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
  * invalid character is found, a pointer to it is returned. If everything is