From a77bb7eb41d34b19a672bb4ede038cc1b19a3945 Mon Sep 17 00:00:00 2001 From: Ebrahim Byagowi Date: Tue, 3 Sep 2019 14:49:14 +0430 Subject: [PATCH] Move hb_codepoint_parse to hb_parse_uint --- src/hb-algs.hh | 18 ++++++++---------- src/hb-buffer-serialize.cc | 1 - src/hb-common.cc | 1 - src/hb-number.cc | 26 +++++++++++++++++++++++--- src/hb-number.hh | 3 +++ src/hb.hh | 3 ++- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index 6b74a2532..96cc1573e 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -32,6 +32,7 @@ #include "hb.hh" #include "hb-meta.hh" #include "hb-null.hh" +#include "hb-number.hh" /* Encodes three unsigned integers in one 64-bit number. If the inputs have more than 21 bits, @@ -896,17 +897,14 @@ hb_stable_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) static inline hb_bool_t hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) { - /* Pain because we don't know whether s is nul-terminated. */ - char buf[64]; - len = hb_min (ARRAY_LENGTH (buf) - 1, len); - strncpy (buf, s, len); - buf[len] = '\0'; + unsigned int v; + const char *p = s; + const char *end = p + len; + if (!hb_parse_uint (&p, p + len, &v, base)) + return false; + + if (end != p && *p) return false; - char *end; - errno = 0; - unsigned long v = strtoul (buf, &end, base); - if (errno) return false; - if (*end) return false; *out = v; return true; } diff --git a/src/hb-buffer-serialize.cc b/src/hb-buffer-serialize.cc index 518004ba9..a6e3bb81a 100644 --- a/src/hb-buffer-serialize.cc +++ b/src/hb-buffer-serialize.cc @@ -25,7 +25,6 @@ */ #include "hb.hh" -#include "hb-number.hh" #ifndef HB_NO_BUFFER_SERIALIZE diff --git a/src/hb-common.cc b/src/hb-common.cc index f5c18ec58..690c96f11 100644 --- a/src/hb-common.cc +++ b/src/hb-common.cc @@ -28,7 +28,6 @@ #include "hb.hh" #include "hb-machinery.hh" -#include "hb-number.hh" #include #ifdef HAVE_XLOCALE_H diff --git a/src/hb-number.cc b/src/hb-number.cc index d8291d96d..2a35ba198 100644 --- a/src/hb-number.cc +++ b/src/hb-number.cc @@ -25,7 +25,6 @@ #include "hb.hh" #include "hb-machinery.hh" -#include "hb-number.hh" #include #ifdef HAVE_XLOCALE_H @@ -46,14 +45,35 @@ hb_parse_int (const char **pp, const char *end, int *pv) errno = 0; v = strtol (p, &pend, 10); - if (errno || p == pend) - return false; + if (unlikely (errno || p == pend)) return false; *pv = v; *pp += pend - p; return true; } +bool +hb_parse_uint (const char **pp, const char *end, unsigned int *pv, int base) +{ + char buf[32]; + unsigned int len = hb_min (ARRAY_LENGTH (buf) - 1, (unsigned int) (end - *pp)); + strncpy (buf, *pp, len); + buf[len] = '\0'; + + char *p = buf; + char *pend = p; + int v; + + errno = 0; + v = strtol (p, &pend, 10); + if (unlikely (errno || p == pend)) return false; + + *pv = v; + *pp += pend - p; + return true; +} + + #if defined (HAVE_NEWLOCALE) && defined (HAVE_STRTOD_L) #define USE_XLOCALE 1 #define HB_LOCALE_T locale_t diff --git a/src/hb-number.hh b/src/hb-number.hh index 54679a79c..41a1d7d2e 100644 --- a/src/hb-number.hh +++ b/src/hb-number.hh @@ -29,6 +29,9 @@ HB_INTERNAL bool hb_parse_int (const char **pp, const char *end, int *pv); +HB_INTERNAL bool +hb_parse_uint (const char **pp, const char *end, unsigned int *pv, int base=10); + HB_INTERNAL bool hb_parse_float (const char **pp, const char *end, float *pv); diff --git a/src/hb.hh b/src/hb.hh index 3a824fdcd..f0942870f 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -594,9 +594,10 @@ struct BEInt * them directly.*/ #include "hb-meta.hh" #include "hb-mutex.hh" +#include "hb-number.hh" #include "hb-atomic.hh" // Requires: hb-meta #include "hb-null.hh" // Requires: hb-meta -#include "hb-algs.hh" // Requires: hb-meta hb-null +#include "hb-algs.hh" // Requires: hb-meta hb-null hb-number #include "hb-iter.hh" // Requires: hb-algs hb-meta #include "hb-debug.hh" // Requires: hb-algs hb-atomic #include "hb-array.hh" // Requires: hb-algs hb-iter hb-null