summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@nginx.com>2023-08-24 01:12:27 +0200
committerAlejandro Colomar <alx@kernel.org>2023-10-25 13:38:39 +0200
commite357f665abad29dfdf94103d0ad82774d2ec3d14 (patch)
tree850fc727a1a6f5becd009c6f6050085ec486855a
parent3630425f826c7334592c7d1f2527082cfe43f79d (diff)
downloadunit-e357f665abad29dfdf94103d0ad82774d2ec3d14.tar.gz
unit-e357f665abad29dfdf94103d0ad82774d2ec3d14.tar.bz2
String: added strto[u]l(3) variants for nxt_str_t.
They're really more inspired in the API of BSD's strto[iu](3), but use long just to keep it simple, instead of intmax_t, and since they wrap strtol(3), I called them like it. Signed-off-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--src/nxt_string.c36
-rw-r--r--src/nxt_string.h11
2 files changed, 47 insertions, 0 deletions
diff --git a/src/nxt_string.c b/src/nxt_string.c
index 1ca595a1..e660b367 100644
--- a/src/nxt_string.c
+++ b/src/nxt_string.c
@@ -1,11 +1,20 @@
/*
* Copyright (C) Igor Sysoev
+ * Copyright (C) Alejandro Colomar
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <nxt_unit_cdefs.h>
+
+#include "nxt_string.h"
+
nxt_str_t *
nxt_str_alloc(nxt_mp_t *mp, size_t length)
@@ -842,3 +851,30 @@ nxt_base64_decode(u_char *dst, u_char *src, size_t length)
return (p - dst);
}
+
+
+long
+nxt_ustrtol(const nxt_str_t *s, nxt_int_t *err)
+{
+ char *p, *endptr;
+ long l;
+ char buf[BUFSIZ];
+
+ if (s == NULL || s->length >= nxt_nitems(buf)) {
+ *err = NXT_ERROR;
+ return -1;
+ }
+
+ p = nxt_cpymem(buf, s->start, s->length);
+ *p = '\0';
+
+ errno = 0;
+ l = strtol(buf, &endptr, 0);
+
+ if (errno != 0 || endptr == buf) {
+ *err = NXT_ERROR;
+ return -1;
+ }
+
+ return l;
+}
diff --git a/src/nxt_string.h b/src/nxt_string.h
index 18ea5490..72e545ce 100644
--- a/src/nxt_string.h
+++ b/src/nxt_string.h
@@ -161,7 +161,18 @@ NXT_EXPORT nxt_bool_t nxt_is_complex_uri_encoded(u_char *s, size_t length);
NXT_EXPORT ssize_t nxt_base64_decode(u_char *dst, u_char *src, size_t length);
+NXT_EXPORT long nxt_ustrtol(const nxt_str_t *s, int *err);
+nxt_inline unsigned long nxt_ustrtoul(const nxt_str_t *s, int *err);
+
+
extern const uint8_t nxt_hex2int[256];
+nxt_inline unsigned long
+nxt_ustrtoul(const nxt_str_t *s, int *err)
+{
+ return nxt_ustrtol(s, err);
+}
+
+
#endif /* _NXT_STRING_H_INCLUDED_ */