#ifndef PARSING_H
#define PARSING_H

#include <stdbool.h>
#include <stdint.h>
#include <string.h>

static inline uint64_t fast_str2ull(char **str) {
  uint64_t result = 0;
  int maxlen = 20;  // length of maximum value of 18446744073709551615

  while (maxlen-- && **str >= '0' && **str <= '9') {
    result *= 10;
    result += **str - '0';
    (*str)++;
  }

  return result;
}

static inline int64_t fast_str2ll(char **str) {
  bool neg = false;

  if (**str == '-') {
    neg = true;
    (*str)++;
  }

  uint64_t res = fast_str2ull(str);
  int64_t result = (int64_t)res;

  return neg ? -result : result;
}

static inline uint64_t findAndParseField(char **filedata, char *fld) {
  char *field = strstr(*filedata, fld);
  if (!field) return 0;
  while (*field < '0' || *field > '9') field++;
  *filedata = field;
  uint64_t val = fast_str2ull(filedata);
  return val;
}

#endif