interpret/src/pair.c

25 lines
572 B
C

#include "pair.h"
#include <stdint.h>
#include <errno.h>
/** @return Constructs `a` and `b` as a pair. */
struct pair pair(const char *const a, const char *const b) {
struct pair p;
p.a = a, p.b = b;
return p;
}
/** Doesn't check anything.
@return Whether it was able to parse unsigned `p` to `n`. */
int pair_to_natural(const char *s, const char *const e, uint32_t *const n) {
uint32_t accum = 0;
while(s < e) {
unsigned next = accum * 10 + (unsigned)(*s - '0');
if(accum >= next) return errno = ERANGE, 0;
accum = next;
s++;
}
*n = accum;
return 1;
}