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

#include "nimbase.h"
#include <string.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
#define nimfr_(x, y)
#define nimln_(x, y)
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
struct TGenericSeq {
NI len;
NI reserved;
};
struct NimStringDesc {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
N_NIMCALL(NimStringDesc*, rawNewString)(NI cap);
static N_INLINE(NI, addInt)(NI a, NI b);
N_NOINLINE(void, raiseOverflow)(void);
static N_INLINE(NI, chckRange)(NI i, NI a, NI b);
N_NOINLINE(void, raiseRangeError)(NI64 val);
N_NIMCALL(NimStringDesc*, copyString)(NimStringDesc* src);
N_NOINLINE(void, raiseIndexError2)(NI i, NI n);
N_NIMCALL(NimStringDesc*, addChar)(NimStringDesc* s, NIM_CHAR c);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
static N_INLINE(void, copyMem__M04YC71iJg1N7gBF3HZTngsystem)(void* dest, void* source, NI size);
static N_INLINE(void, nimCopyMem)(void* dest, void* source, NI size);
N_NIMCALL(NimStringDesc*, resizeString)(NimStringDesc* dest, NI addlen);
N_LIB_PRIVATE N_NIMCALL(NimStringDesc*, nsuToHex)(NI64 x, NI len);
N_LIB_PRIVATE N_NIMCALL(void, failedAssertImpl__W9cjVocn1tjhW7p7xohJj6A)(NimStringDesc* msg);
STRING_LITERAL(TM__VzbRQH9cb1xm1tSKH4PwBSw_3, "+", 1);
STRING_LITERAL(TM__VzbRQH9cb1xm1tSKH4PwBSw_4, "%20", 3);
STRING_LITERAL(TM__VzbRQH9cb1xm1tSKH4PwBSw_6, "iterators.nim(207, 11) `len(a) == L` the length of the string c"
"hanged while iterating over it", 93);
static N_INLINE(NI, addInt)(NI a, NI b) {	NI result;
{	result = (NI)0;
	result = (NI)((NU32)(a) + (NU32)(b));
	{
		NIM_BOOL T3_;
		T3_ = (NIM_BOOL)0;
		T3_ = (((NI) 0) <= (NI)(result ^ a));
		if (T3_) goto LA4_;
		T3_ = (((NI) 0) <= (NI)(result ^ b));
		LA4_: ;
		if (!T3_) goto LA5_;
		goto BeforeRet_;
	}
	LA5_: ;
	raiseOverflow();
	}BeforeRet_: ;
	return result;
}
static N_INLINE(NI, chckRange)(NI i, NI a, NI b) {	NI result;
{	result = (NI)0;
	{
		NIM_BOOL T3_;
		T3_ = (NIM_BOOL)0;
		T3_ = (a <= i);
		if (!(T3_)) goto LA4_;
		T3_ = (i <= b);
		LA4_: ;
		if (!T3_) goto LA5_;
		result = i;
		goto BeforeRet_;
	}
	goto LA1_;
	LA5_: ;
	{
		raiseRangeError(((NI64) (i)));
	}
	LA1_: ;
	}BeforeRet_: ;
	return result;
}
static N_INLINE(void, nimCopyMem)(void* dest, void* source, NI size) {	void* T1_;
	T1_ = (void*)0;
	T1_ = memcpy(dest, source, ((size_t) (size)));
}
static N_INLINE(void, copyMem__M04YC71iJg1N7gBF3HZTngsystem)(void* dest, void* source, NI size) {	nimCopyMem(dest, source, size);
}
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {	{
		if (!!((src == NIM_NIL))) goto LA3_;
		copyMem__M04YC71iJg1N7gBF3HZTngsystem(((void*) ((&(*dest).data[(*dest).Sup.len]))), ((void*) ((*src).data)), ((NI) ((NI)((*src).Sup.len + ((NI) 1)))));
		(*dest).Sup.len += (*src).Sup.len;
	}
	LA3_: ;
}
N_LIB_PRIVATE N_NIMCALL(NimStringDesc*, encodeUrl__hfOKCSlSHEBA7MAnVFj2bA)(NimStringDesc* s, NIM_BOOL usePlus) {	NimStringDesc* result;
	NI TM__VzbRQH9cb1xm1tSKH4PwBSw_2;
	NimStringDesc* fromSpace;
	result = (NimStringDesc*)0;
	TM__VzbRQH9cb1xm1tSKH4PwBSw_2 = addInt((s ? s->Sup.len : 0), (NI)((NI32)((s ? s->Sup.len : 0)) >> (NU32)(((NI) 2))));
	result = rawNewString(((NI)chckRange((NI)(TM__VzbRQH9cb1xm1tSKH4PwBSw_2), ((NI) 0), ((NI) 2147483647))));
	{
		if (!usePlus) goto LA3_;
		fromSpace = copyString(((NimStringDesc*) &TM__VzbRQH9cb1xm1tSKH4PwBSw_3));
	}
	goto LA1_;
	LA3_: ;
	{
		fromSpace = copyString(((NimStringDesc*) &TM__VzbRQH9cb1xm1tSKH4PwBSw_4));
	}
	LA1_: ;
	{
		NIM_CHAR c;
		NI i;
		NI L;
		c = (NIM_CHAR)0;
		i = ((NI) 0);
		L = (s ? s->Sup.len : 0);
		{
			while (1) {
				NI TM__VzbRQH9cb1xm1tSKH4PwBSw_5;
				if (!(i < L)) goto LA8;
				if ((NU)(i) >= (NU)(s ? s->Sup.len : 0)) raiseIndexError2(i,(s ? s->Sup.len : 0)-1);
				c = s->data[i];
				switch (((NU8)(c))) {
				case 97 ... 122:
				case 65 ... 90:
				case 48 ... 57:
				case 45:
				case 46:
				case 95:
				case 126:
				{
					result = addChar(result, c);
				}
				break;
				case 32:
				{
					result = resizeString(result, (fromSpace ? fromSpace->Sup.len : 0) + 0);
appendString(result, fromSpace);
				}
				break;
				default:
				{
					NimStringDesc* T12_;
					result = addChar(result, 37);
					T12_ = (NimStringDesc*)0;
					T12_ = nsuToHex(((NI64) (((NU8)(c)))), ((NI) 2));
					result = resizeString(result, (T12_ ? T12_->Sup.len : 0) + 0);
appendString(result, T12_);
				}
				break;
				}
				TM__VzbRQH9cb1xm1tSKH4PwBSw_5 = addInt(i, ((NI) 1));
				i = (NI)(TM__VzbRQH9cb1xm1tSKH4PwBSw_5);
				{
					if (!!(((s ? s->Sup.len : 0) == L))) goto LA15_;
					failedAssertImpl__W9cjVocn1tjhW7p7xohJj6A(((NimStringDesc*) &TM__VzbRQH9cb1xm1tSKH4PwBSw_6));
				}
				LA15_: ;
			} LA8: ;
		}
	}
	return result;
}
