File: readint.c

package info (click to toggle)
nghttp2 1.68.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,592 kB
  • sloc: ansic: 104,233; cpp: 55,792; ruby: 30,108; yacc: 7,083; sh: 4,643; makefile: 1,506; python: 806
file content (27 lines) | stat: -rw-r--r-- 855 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <mruby.h>
#include <mruby/numeric.h>

/* mrb_read_int(): read mrb_int from a string (base 10 only) */
/* const char *p - string to read                            */
/* const char *e - end of string                             */
/* char **endp   - end of parsed integer                     */
/* mrb_int *np   - variable to save the result               */
/* returns TRUE if read succeeded                            */
/* if integer overflows, returns FALSE                       */
MRB_API mrb_bool
mrb_read_int(const char *p, const char *e, char **endp, mrb_int *np)
{
  mrb_int n = 0;

  while ((e == NULL || p < e) && ISDIGIT(*p)) {
    int ch = *p - '0';
    if (mrb_int_mul_overflow(n, 10, &n) ||
        mrb_int_add_overflow(n, ch, &n)) {
      return FALSE;
    }
    p++;
  }
  if (endp) *endp = (char*)p;
  *np = n;
  return TRUE;
}