2019-09-02 22:32:40 +02:00
|
|
|
/*
|
|
|
|
* Copyright © 2019 Ebrahim Byagowi
|
|
|
|
*
|
|
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, without written agreement and without
|
|
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
|
|
* software and its documentation for any purpose, provided that the
|
|
|
|
* above copyright notice and the following two paragraphs appear in
|
|
|
|
* all copies of this software.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
|
|
|
*
|
|
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "hb.hh"
|
2019-12-09 08:18:43 +01:00
|
|
|
#include "hb-number.hh"
|
2019-12-09 09:59:28 +01:00
|
|
|
#include "hb-number-parser.hh"
|
2019-09-02 22:32:40 +02:00
|
|
|
|
2019-09-03 12:44:21 +02:00
|
|
|
template<typename T, typename Func>
|
2019-09-03 12:53:40 +02:00
|
|
|
static bool
|
2019-09-03 20:39:47 +02:00
|
|
|
_parse_number (const char **pp, const char *end, T *pv,
|
|
|
|
bool whole_buffer, Func f)
|
2019-09-02 22:32:40 +02:00
|
|
|
{
|
|
|
|
char buf[32];
|
2020-05-10 14:27:45 +02:00
|
|
|
unsigned len = hb_min (ARRAY_LENGTH (buf) - 1, (unsigned) (end - *pp));
|
2019-09-02 22:32:40 +02:00
|
|
|
strncpy (buf, *pp, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
char *p = buf;
|
|
|
|
char *pend = p;
|
|
|
|
|
|
|
|
errno = 0;
|
2019-09-03 12:53:40 +02:00
|
|
|
*pv = f (p, &pend);
|
2019-09-03 20:39:47 +02:00
|
|
|
if (unlikely (errno || p == pend ||
|
|
|
|
/* Check if consumed whole buffer if is requested */
|
2020-05-10 14:27:45 +02:00
|
|
|
(whole_buffer && pend - p != end - *pp)))
|
|
|
|
return false;
|
2019-09-02 22:32:40 +02:00
|
|
|
|
|
|
|
*pp += pend - p;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-09-03 12:19:14 +02:00
|
|
|
bool
|
2019-09-03 20:39:47 +02:00
|
|
|
hb_parse_int (const char **pp, const char *end, int *pv, bool whole_buffer)
|
2019-09-03 12:19:14 +02:00
|
|
|
{
|
2019-09-03 20:39:47 +02:00
|
|
|
return _parse_number<int> (pp, end, pv, whole_buffer,
|
|
|
|
[] (const char *p, char **end)
|
|
|
|
{ return strtol (p, end, 10); });
|
2019-09-03 12:44:21 +02:00
|
|
|
}
|
2019-09-03 12:19:14 +02:00
|
|
|
|
2019-09-03 12:44:21 +02:00
|
|
|
bool
|
2020-05-10 14:27:45 +02:00
|
|
|
hb_parse_uint (const char **pp, const char *end, unsigned *pv,
|
2019-09-03 20:39:47 +02:00
|
|
|
bool whole_buffer, int base)
|
2019-09-03 12:44:21 +02:00
|
|
|
{
|
2020-05-10 14:27:45 +02:00
|
|
|
return _parse_number<unsigned> (pp, end, pv, whole_buffer,
|
|
|
|
[base] (const char *p, char **end)
|
|
|
|
{ return strtoul (p, end, base); });
|
2019-09-03 12:19:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-02 22:32:40 +02:00
|
|
|
bool
|
2020-05-10 14:27:45 +02:00
|
|
|
hb_parse_double (const char **pp, const char *end, double *pv, bool whole_buffer)
|
2019-09-02 22:32:40 +02:00
|
|
|
{
|
2020-05-10 14:27:45 +02:00
|
|
|
const char *pend = end;
|
|
|
|
*pv = strtod_rl (*pp, &pend);
|
|
|
|
if (unlikely (*pp == pend)) return false;
|
|
|
|
*pp = pend;
|
|
|
|
return !whole_buffer || end == pend;
|
2019-09-02 22:32:40 +02:00
|
|
|
}
|