aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstddjb/uint64_scan_base_max.c
blob: 6debe0a1c8d18035754d07454f75ae9e8b774502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* ISC license. */

#include <stddef.h>
#include <stdint.h>

#include <skalibs/uint64.h>
#include <skalibs/fmtscan.h>

size_t uint64_scan_base_max (char const *s, uint64_t *u, uint8_t base, uint64_t max)
{
  uint64_t result = 0 ;
  size_t pos = 0 ;
  for (;; pos++)
  {
    uint8_t c = fmtscan_num(s[pos], base) ;
    if (c >= base || result > ((max - c) / base)) break ;
    result = result * base + c ;
  }
  if (pos) *u = result ;
  return pos ;
}