/* Generated by Nim Compiler v1.3.5 */
/*   (c) 2020 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 64

/* section: NIM_merge_HEADERS */

#include "nimbase.h"
#undef LANGUAGE_C
#undef MIPSEB
#undef MIPSEL
#undef PPC
#undef R3000
#undef R4000
#undef i386
#undef linux
#undef mips
#undef near
#undef far
#undef powerpc
#undef unix

/* section: NIM_merge_FRAME_DEFINES */
#define nimfr_(x, y)
#define nimln_(x, y)

/* section: NIM_merge_FORWARD_TYPES */
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw;
typedef struct tyObject_CatchableError__qrLSDoe2oBoAqNtJ9badtnA tyObject_CatchableError__qrLSDoe2oBoAqNtJ9badtnA;
typedef struct Exception Exception;
typedef struct RootObj RootObj;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct tySequence__uB9b75OUPRENsBAu4AnoePA tySequence__uB9b75OUPRENsBAu4AnoePA;
typedef struct tyObject_StackTraceEntry__oLyohQ7O2XOvGnflOss8EA tyObject_StackTraceEntry__oLyohQ7O2XOvGnflOss8EA;

/* section: NIM_merge_TYPES */
struct TGenericSeq {
NI len;
NI reserved;
};
struct NimStringDesc {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
typedef NU8 tyEnum_TNimKind__jIBKr1ejBgsfM33Kxw4j7A;
typedef NU8 tySet_tyEnum_TNimTypeFlag__v8QUszD1sWlSIWZz7mC4bQ;
typedef N_NIMCALL_PTR(void, tyProc__ojoeKfW4VYIm36I9cpDTQIg) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, tyProc__WSm2xU5ARYv9aAR4l0z9c9auQ) (void* p);
struct TNimType {
NI size;
NI align;
tyEnum_TNimKind__jIBKr1ejBgsfM33Kxw4j7A kind;
tySet_tyEnum_TNimTypeFlag__v8QUszD1sWlSIWZz7mC4bQ flags;
TNimType* base;
TNimNode* node;
void* finalizer;
tyProc__ojoeKfW4VYIm36I9cpDTQIg marker;
tyProc__WSm2xU5ARYv9aAR4l0z9c9auQ deepcopy;
};
struct RootObj {
TNimType* m_type;
};
struct Exception {
  RootObj Sup;
Exception* parent;
NCSTRING name;
NimStringDesc* message;
tySequence__uB9b75OUPRENsBAu4AnoePA* trace;
Exception* up;
};
struct tyObject_CatchableError__qrLSDoe2oBoAqNtJ9badtnA {
  Exception Sup;
};
struct tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw {
  tyObject_CatchableError__qrLSDoe2oBoAqNtJ9badtnA Sup;
};
typedef NU8 tyEnum_TNimNodeKind__unfNsxrcATrufDZmpBq4HQ;
struct TNimNode {
tyEnum_TNimNodeKind__unfNsxrcATrufDZmpBq4HQ kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct tyObject_StackTraceEntry__oLyohQ7O2XOvGnflOss8EA {
NCSTRING procname;
NI line;
NCSTRING filename;
};

/* section: NIM_merge_SEQ_TYPES */
struct tySequence__uB9b75OUPRENsBAu4AnoePA {
  TGenericSeq Sup;
  tyObject_StackTraceEntry__oLyohQ7O2XOvGnflOss8EA data[SEQ_DECL_SIZE];
};

/* section: NIM_merge_PROC_HEADERS */
N_NIMCALL(NI, nimParseBiggestFloat)(NimStringDesc* s, NF* number, NI start);
N_LIB_PRIVATE N_NIMCALL(NI, rawParseUInt__aIj9c6LpxzonIV8kjAVwtEg)(NimStringDesc* s, NU64* b, NI start);
N_LIB_PRIVATE N_NOINLINE(void, integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig)(void);
N_LIB_PRIVATE N_NIMCALL(void*, newObj)(TNimType* typ, NI size);
N_LIB_PRIVATE N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_LIB_PRIVATE N_NIMCALL(void, raiseExceptionEx)(Exception* e, NCSTRING ename, NCSTRING procname, NCSTRING filename, NI line);
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void);
N_LIB_PRIVATE N_NIMCALL(NI, rawParseInt__oz568vhg7PZ19ak9aiwQxGYw)(NimStringDesc* s, NI64* b, NI start);
N_LIB_PRIVATE N_NIMCALL(NI, npuParseBiggestInt)(NimStringDesc* s, NI64* number, NI start);

/* section: NIM_merge_DATA */
extern TNimType NTI__Ie1m0dv1ZHg72IgPRr1cDw_;
extern TNimType NTI__yoNlBGx0D2tRizIdhQuENw_;
STRING_LITERAL(TM__vI9aZNKIcImom6dDyKXW1ZA_2, "Parsed integer outside of valid range", 37);

/* section: NIM_merge_VARS */
extern NIM_BOOL nimInErrorMode__759bT87luu8XGcbkw13FUjA;

/* section: NIM_merge_PROCS */
N_LIB_PRIVATE N_NIMCALL(NI, parseSaturatedNatural__hGWFPKYRsDcYFCjZLM9acZw)(NimStringDesc* s, NI* b, NI start) {
	NI result;
	NI i;
	result = (NI)0;
	i = start;
	{
		NIM_BOOL T3_;
		T3_ = (NIM_BOOL)0;
		T3_ = (i < (s ? s->Sup.len : 0));
		if (!(T3_)) goto LA4_;
		T3_ = ((NU8)(s->data[i]) == (NU8)(43));
		LA4_: ;
		if (!T3_) goto LA5_;
		i += ((NI) 1);
	}
	LA5_: ;
	{
		NIM_BOOL T9_;
		T9_ = (NIM_BOOL)0;
		T9_ = (i < (s ? s->Sup.len : 0));
		if (!(T9_)) goto LA10_;
		T9_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
		LA10_: ;
		if (!T9_) goto LA11_;
		(*b) = ((NI) 0);
		{
			while (1) {
				NIM_BOOL T15_;
				NI c;
				T15_ = (NIM_BOOL)0;
				T15_ = (i < (s ? s->Sup.len : 0));
				if (!(T15_)) goto LA16_;
				T15_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
				LA16_: ;
				if (!T15_) goto LA14;
				c = (NI)(((NU8)(s->data[i])) - ((NI) 48));
				{
					if (!((*b) <= (NI)((NI)(((NI) IL64(9223372036854775807)) - c) / ((NI) 10)))) goto LA19_;
					(*b) = (NI)((NI)((*b) * ((NI) 10)) + c);
				}
				goto LA17_;
				LA19_: ;
				{
					(*b) = ((NI) IL64(9223372036854775807));
				}
				LA17_: ;
				i += ((NI) 1);
				{
					while (1) {
						NIM_BOOL T24_;
						T24_ = (NIM_BOOL)0;
						T24_ = (i < (s ? s->Sup.len : 0));
						if (!(T24_)) goto LA25_;
						T24_ = ((NU8)(s->data[i]) == (NU8)(95));
						LA25_: ;
						if (!T24_) goto LA23;
						i += ((NI) 1);
					} LA23: ;
				}
			} LA14: ;
		}
		result = (NI)(i - start);
	}
	LA11_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, npuParseFloat)(NimStringDesc* s, NF* number, NI start) {
	NI result;
	NF bf;
	result = (NI)0;
	bf = 0.0;
	result = nimParseBiggestFloat(s, (&bf), start);
	{
		if (!!((result == ((NI) 0)))) goto LA3_;
		(*number) = bf;
	}
	LA3_: ;
	return result;
}
N_LIB_PRIVATE N_NOINLINE(void, integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig)(void) {
	tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw* T1_;
{	T1_ = (tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw*)0;
	T1_ = (tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw*) newObj((&NTI__Ie1m0dv1ZHg72IgPRr1cDw_), sizeof(tyObject_ValueError__yoNlBGx0D2tRizIdhQuENw));
	(*T1_).Sup.Sup.Sup.m_type = (&NTI__yoNlBGx0D2tRizIdhQuENw_);
	(*T1_).Sup.Sup.name = "ValueError";
	(*T1_).Sup.Sup.message = copyString(((NimStringDesc*) &TM__vI9aZNKIcImom6dDyKXW1ZA_2));
	(*T1_).Sup.Sup.parent = NIM_NIL;
	raiseExceptionEx((Exception*)T1_, "ValueError", "integerOutOfRangeError", "parseutils.nim", 403);
	goto BeforeRet_;
	}BeforeRet_: ;
}
static N_INLINE(NIM_BOOL*, nimErrorFlag)(void) {
	NIM_BOOL* result;
	result = (NIM_BOOL*)0;
	result = (&nimInErrorMode__759bT87luu8XGcbkw13FUjA);
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, rawParseUInt__aIj9c6LpxzonIV8kjAVwtEg)(NimStringDesc* s, NU64* b, NI start) {
	NI result;
	NU64 res;
	NU64 prev;
	NI i;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	res = 0ULL;
	prev = 0ULL;
	i = start;
	{
		NIM_BOOL T3_;
		NIM_BOOL T4_;
		T3_ = (NIM_BOOL)0;
		T4_ = (NIM_BOOL)0;
		T4_ = (i < (NI)((s ? s->Sup.len : 0) - ((NI) 1)));
		if (!(T4_)) goto LA5_;
		T4_ = ((NU8)(s->data[i]) == (NU8)(45));
		LA5_: ;
		T3_ = T4_;
		if (!(T3_)) goto LA6_;
		T3_ = (((NU8)(s->data[(NI)(i + ((NI) 1))])) >= ((NU8)(48)) && ((NU8)(s->data[(NI)(i + ((NI) 1))])) <= ((NU8)(57)));
		LA6_: ;
		if (!T3_) goto LA7_;
		integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig();
		if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	}
	LA7_: ;
	{
		NIM_BOOL T11_;
		T11_ = (NIM_BOOL)0;
		T11_ = (i < (s ? s->Sup.len : 0));
		if (!(T11_)) goto LA12_;
		T11_ = ((NU8)(s->data[i]) == (NU8)(43));
		LA12_: ;
		if (!T11_) goto LA13_;
		i += ((NI) 1);
	}
	LA13_: ;
	{
		NIM_BOOL T17_;
		T17_ = (NIM_BOOL)0;
		T17_ = (i < (s ? s->Sup.len : 0));
		if (!(T17_)) goto LA18_;
		T17_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
		LA18_: ;
		if (!T17_) goto LA19_;
		(*b) = 0ULL;
		{
			while (1) {
				NIM_BOOL T23_;
				T23_ = (NIM_BOOL)0;
				T23_ = (i < (s ? s->Sup.len : 0));
				if (!(T23_)) goto LA24_;
				T23_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
				LA24_: ;
				if (!T23_) goto LA22;
				prev = res;
				res = (NU64)((NU64)((NU64)((NU64)(res) * (NU64)(10ULL))) + (NU64)(((NU64) ((NI)(((NU8)(s->data[i])) - ((NI) 48))))));
				{
					if (!((NU64)(res) < (NU64)(prev))) goto LA27_;
					integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig();
					if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
				}
				LA27_: ;
				i += ((NI) 1);
				{
					while (1) {
						NIM_BOOL T31_;
						T31_ = (NIM_BOOL)0;
						T31_ = (i < (s ? s->Sup.len : 0));
						if (!(T31_)) goto LA32_;
						T31_ = ((NU8)(s->data[i]) == (NU8)(95));
						LA32_: ;
						if (!T31_) goto LA30;
						i += ((NI) 1);
					} LA30: ;
				}
			} LA22: ;
		}
		(*b) = res;
		result = (NI)(i - start);
	}
	LA19_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, npuParseBiggestUInt)(NimStringDesc* s, NU64* number, NI start) {
	NI result;
	NU64 res;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	res = 0ULL;
	result = rawParseUInt__aIj9c6LpxzonIV8kjAVwtEg(s, (&res), start);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	{
		if (!!((result == ((NI) 0)))) goto LA3_;
		(*number) = res;
	}
	LA3_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, rawParseInt__oz568vhg7PZ19ak9aiwQxGYw)(NimStringDesc* s, NI64* b, NI start) {
	NI result;
	NI64 sign;
	NI i;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	sign = IL64(-1);
	i = start;
	{
		if (!(i < (s ? s->Sup.len : 0))) goto LA3_;
		{
			if (!((NU8)(s->data[i]) == (NU8)(43))) goto LA7_;
			i += ((NI) 1);
		}
		goto LA5_;
		LA7_: ;
		{
			if (!((NU8)(s->data[i]) == (NU8)(45))) goto LA10_;
			i += ((NI) 1);
			sign = IL64(1);
		}
		goto LA5_;
		LA10_: ;
		LA5_: ;
	}
	LA3_: ;
	{
		NIM_BOOL T14_;
		T14_ = (NIM_BOOL)0;
		T14_ = (i < (s ? s->Sup.len : 0));
		if (!(T14_)) goto LA15_;
		T14_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
		LA15_: ;
		if (!T14_) goto LA16_;
		(*b) = IL64(0);
		{
			while (1) {
				NIM_BOOL T20_;
				NI c;
				T20_ = (NIM_BOOL)0;
				T20_ = (i < (s ? s->Sup.len : 0));
				if (!(T20_)) goto LA21_;
				T20_ = (((NU8)(s->data[i])) >= ((NU8)(48)) && ((NU8)(s->data[i])) <= ((NU8)(57)));
				LA21_: ;
				if (!T20_) goto LA19;
				c = (NI)(((NU8)(s->data[i])) - ((NI) 48));
				{
					if (!((NI64)((NI64)((IL64(-9223372036854775807) - IL64(1)) + ((NI64) (c))) / IL64(10)) <= (*b))) goto LA24_;
					(*b) = (NI64)((NI64)((*b) * IL64(10)) - ((NI64) (c)));
				}
				goto LA22_;
				LA24_: ;
				{
					integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig();
					if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
				}
				LA22_: ;
				i += ((NI) 1);
				{
					while (1) {
						NIM_BOOL T29_;
						T29_ = (NIM_BOOL)0;
						T29_ = (i < (s ? s->Sup.len : 0));
						if (!(T29_)) goto LA30_;
						T29_ = ((NU8)(s->data[i]) == (NU8)(95));
						LA30_: ;
						if (!T29_) goto LA28;
						i += ((NI) 1);
					} LA28: ;
				}
			} LA19: ;
		}
		{
			NIM_BOOL T33_;
			T33_ = (NIM_BOOL)0;
			T33_ = (sign == IL64(-1));
			if (!(T33_)) goto LA34_;
			T33_ = ((*b) == (IL64(-9223372036854775807) - IL64(1)));
			LA34_: ;
			if (!T33_) goto LA35_;
			integerOutOfRangeError__urgsblG9a8f3sSX4FE53fig();
			if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
		}
		goto LA31_;
		LA35_: ;
		{
			(*b) = (NI64)((*b) * sign);
			result = (NI)(i - start);
		}
		LA31_: ;
	}
	LA16_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, npuParseBiggestInt)(NimStringDesc* s, NI64* number, NI start) {
	NI result;
	NI64 res;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	res = IL64(0);
	result = rawParseInt__oz568vhg7PZ19ak9aiwQxGYw(s, (&res), start);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	{
		if (!!((result == ((NI) 0)))) goto LA3_;
		(*number) = res;
	}
	LA3_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, npuParseInt)(NimStringDesc* s, NI* number, NI start) {
	NI result;
	NI64 res;
NIM_BOOL* nimErr_;
{nimErr_ = nimErrorFlag();
	result = (NI)0;
	res = IL64(0);
	result = npuParseBiggestInt(s, (&res), start);
	if (NIM_UNLIKELY(*nimErr_)) goto BeforeRet_;
	{
		if (!!((result == ((NI) 0)))) goto LA3_;
		(*number) = ((NI) (res));
	}
	LA3_: ;
	}BeforeRet_: ;
	return result;
}
N_LIB_PRIVATE N_NIMCALL(NI, parseHex__5URmSDed0NZg9au720T3U9aA)(NimStringDesc* s, NI* number, NI start, NI maxLen) {
	NI result;
	NI i;
	NI output;
	NIM_BOOL foundDigit;
	NI last;
	NI T1_;
	result = (NI)0;
	i = start;
	output = ((NI) 0);
	foundDigit = NIM_FALSE;
	T1_ = (NI)0;
	{
		if (!(maxLen == ((NI) 0))) goto LA4_;
		T1_ = (s ? s->Sup.len : 0);
	}
	goto LA2_;
	LA4_: ;
	{
		T1_ = (NI)(i + maxLen);
	}
	LA2_: ;
	last = (((s ? s->Sup.len : 0) <= T1_) ? (s ? s->Sup.len : 0) : T1_);
	{
		NIM_BOOL T9_;
		NIM_BOOL T10_;
		T9_ = (NIM_BOOL)0;
		T10_ = (NIM_BOOL)0;
		T10_ = ((NI)(i + ((NI) 1)) < last);
		if (!(T10_)) goto LA11_;
		T10_ = ((NU8)(s->data[i]) == (NU8)(48));
		LA11_: ;
		T9_ = T10_;
		if (!(T9_)) goto LA12_;
		T9_ = (((NU8)(s->data[(NI)(i + ((NI) 1))])) == ((NU8)(120)) || ((NU8)(s->data[(NI)(i + ((NI) 1))])) == ((NU8)(88)));
		LA12_: ;
		if (!T9_) goto LA13_;
		i += ((NI) 2);
	}
	goto LA7_;
	LA13_: ;
	{
		NIM_BOOL T16_;
		T16_ = (NIM_BOOL)0;
		T16_ = (i < last);
		if (!(T16_)) goto LA17_;
		T16_ = ((NU8)(s->data[i]) == (NU8)(35));
		LA17_: ;
		if (!T16_) goto LA18_;
		i += ((NI) 1);
	}
	goto LA7_;
	LA18_: ;
	LA7_: ;
	{
		while (1) {
			if (!(i < last)) goto LA21;
			switch (((NU8)(s->data[i]))) {
			case 95:
			{
			}
			break;
			case 48 ... 57:
			{
				output = (NI)((NI)((NU64)(output) << (NU64)(((NI) 4))) | (NI)(((NU8)(s->data[i])) - ((NI) 48)));
				foundDigit = NIM_TRUE;
			}
			break;
			case 97 ... 102:
			{
				output = (NI)((NI)((NU64)(output) << (NU64)(((NI) 4))) | (NI)((NI)(((NU8)(s->data[i])) - ((NI) 97)) + ((NI) 10)));
				foundDigit = NIM_TRUE;
			}
			break;
			case 65 ... 70:
			{
				output = (NI)((NI)((NU64)(output) << (NU64)(((NI) 4))) | (NI)((NI)(((NU8)(s->data[i])) - ((NI) 65)) + ((NI) 10)));
				foundDigit = NIM_TRUE;
			}
			break;
			default:
			{
				goto LA20;
			}
			break;
			}
			i += ((NI) 1);
		} LA21: ;
	} LA20: ;
	{
		if (!foundDigit) goto LA29_;
		(*number) = output;
		result = (NI)(i - start);
	}
	LA29_: ;
	return result;
}
