// File: Alcaro[P]bridges/arlib/file.cpp
// Function: file::sanitize_rel_path(string)
string file::sanitize_rel_path(string path)
{
	// this one currently leaves consecutive slashes alone
	// this means sanitized filename inequality does not mean they point to different files,
	//  but case insensitive FS means that already so no harm done
again:
	uint8_t * start = path.bytes().ptr();
	uint8_t * end = start + path.length();
	uint8_t * iter = start;
	
	size_t ndot = 0; // 0 - none seen, just after a slash; 1, 2 - obvious; 3+ - either 3+ seen, or a non-dot
	while (iter < end)
	{
		uint8_t& byte = *iter;
#ifdef _WIN32
		if (byte == '\\') byte = '/';
		if (byte == ':') byte = '_'; // no NTFS ADS allowed, nor C:/
#endif
		if (byte < ' ' || byte == '\x7F') byte = '_';
		
		if (byte == '/')
		{
			if (UNLIKELY(ndot == 1 || ndot == 2))
			{
				if (iter == start+1)
				{
					path = path.substr(2, ~0);
					goto again;
				}
				iter[-1] = '_';
			}
			ndot = 0;
		}
		else if (byte == '.') ndot++;
		else ndot = 3;
		
		if (byte >= 0x80)
		{
			uint32_t index = iter-start;
			uint32_t cp = path.codepoint_at(index);
			if (cp >= 0xDC80 && cp <= 0xDCFF) byte = '_';
			iter = start+index;
		}
		else iter++;
	}
	
	if (UNLIKELY(ndot == 1 || ndot == 2))
		iter[-1] = '_';
	
	if (path[0] == '/') path[0] = '_';
	if (!path) return "_";
	return path;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rsi, -0x158(%rbp)
movq %rdi, -0x150(%rbp)
movq %rdi, %rax
movq %rax, -0x148(%rbp)
movq %rdi, -0xe8(%rbp)
movq %rsi, -0xf0(%rbp)
movq -0x158(%rbp), %rax
leaq -0x108(%rbp), %rcx
movq %rcx, -0xc8(%rbp)
movq %rax, -0xd0(%rbp)
movq -0xd0(%rbp), %rax
leaq -0x108(%rbp), %rcx
movq %rcx, -0x60(%rbp)
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rax
movq %rax, -0x160(%rbp)
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x12427
movq -0x160(%rbp), %rax
movq %rax, -0x50(%rbp)
movq -0x50(%rbp), %rdi
callq 0x5cc0
movq -0x160(%rbp), %rsi
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, %edx
leaq -0x108(%rbp), %rdi
callq 0x5ed0
jmp 0x12442
movq -0x160(%rbp), %rax
movq (%rax), %rsi
movl 0x8(%rax), %eax
movl %eax, %edx
leaq -0x108(%rbp), %rdi
callq 0x5ed0
leaq -0x108(%rbp), %rdi
callq 0x7490
movq %rax, %rcx
movq -0x158(%rbp), %rax
movq %rcx, -0xf8(%rbp)
movq -0xf8(%rbp), %rcx
movq %rcx, -0x170(%rbp)
movq %rax, -0xc0(%rbp)
movq -0xc0(%rbp), %rax
movq %rax, -0x168(%rbp)
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x124bc
movq -0x168(%rbp), %rax
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
xorl $0xf, %eax
movzbl %al, %eax
movl %eax, -0xb4(%rbp)
jmp 0x124cc
movq -0x168(%rbp), %rax
movl 0x8(%rax), %eax
movl %eax, -0xb4(%rbp)
movq -0x170(%rbp), %rax
movl -0xb4(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x110(%rbp)
movq -0xf8(%rbp), %rax
movq %rax, -0x118(%rbp)
movq $0x0, -0x120(%rbp)
movq -0x118(%rbp), %rax
cmpq -0x110(%rbp), %rax
jae 0x126ff
movq -0x118(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x20, %eax
jl 0x1253c
movq -0x128(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x7f, %eax
jne 0x12546
movq -0x128(%rbp), %rax
movb $0x5f, (%rax)
movq -0x128(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1263c
movb $0x1, %al
cmpq $0x1, -0x120(%rbp)
movb %al, -0x171(%rbp)
je 0x1257c
cmpq $0x2, -0x120(%rbp)
sete %al
movb %al, -0x171(%rbp)
movb -0x171(%rbp), %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x1258f
jmp 0x1262f
movq -0x118(%rbp), %rax
movq -0xf8(%rbp), %rcx
addq $0x1, %rcx
cmpq %rcx, %rax
jne 0x12624
movq -0x158(%rbp), %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x9f90
movq %rax, %rcx
movq -0x158(%rbp), %rax
movq %rcx, -0x138(%rbp)
movq %rdx, -0x130(%rbp)
movq %rax, -0x98(%rbp)
leaq -0x138(%rbp), %rax
movq %rax, -0xa0(%rbp)
movq -0x98(%rbp), %rdi
movq -0xa0(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0xb0(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0xa8(%rbp)
movq -0xb0(%rbp), %rsi
movq -0xa8(%rbp), %rdx
callq 0xa370
jmp 0x123a1
movq -0x118(%rbp), %rax
movb $0x5f, -0x1(%rax)
movq $0x0, -0x120(%rbp)
jmp 0x1266c
movq -0x128(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x2e, %eax
jne 0x1265f
movq -0x120(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x120(%rbp)
jmp 0x1266a
movq $0x3, -0x120(%rbp)
jmp 0x1266c
movq -0x128(%rbp), %rax
movzbl (%rax), %eax
cmpl $0x80, %eax
jl 0x126e8
movq -0x158(%rbp), %rdi
movq -0x118(%rbp), %rax
movq -0xf8(%rbp), %rcx
subq %rcx, %rax
movl %eax, -0x13c(%rbp)
leaq -0x13c(%rbp), %rsi
callq 0x3d3a0
movl %eax, -0x140(%rbp)
cmpl $0xdc80, -0x140(%rbp)   # imm = 0xDC80
jb 0x126cf
cmpl $0xdcff, -0x140(%rbp)   # imm = 0xDCFF
ja 0x126cf
movq -0x128(%rbp), %rax
movb $0x5f, (%rax)
movq -0xf8(%rbp), %rax
movl -0x13c(%rbp), %ecx
addq %rcx, %rax
movq %rax, -0x118(%rbp)
jmp 0x126fa
movq -0x118(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x118(%rbp)
jmp 0x124fc
movb $0x1, %al
cmpq $0x1, -0x120(%rbp)
movb %al, -0x172(%rbp)
je 0x12722
cmpq $0x2, -0x120(%rbp)
sete %al
movb %al, -0x172(%rbp)
movb -0x172(%rbp), %al
xorb $-0x1, %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x12732
jmp 0x1273d
movq -0x118(%rbp), %rax
movb $0x5f, -0x1(%rax)
movq -0x158(%rbp), %rax
movq %rax, -0x78(%rbp)
movl $0x0, -0x7c(%rbp)
movq -0x78(%rbp), %rax
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
movq %rax, -0x180(%rbp)
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x1278c
movq -0x180(%rbp), %rax
movq %rax, -0x30(%rbp)
jmp 0x1279a
movq -0x180(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
movslq -0x7c(%rbp), %rcx
movzbl (%rax,%rcx), %eax
cmpl $0x2f, %eax
jne 0x12820
movq -0x158(%rbp), %rax
movq %rax, -0x88(%rbp)
movl $0x0, -0x8c(%rbp)
movq -0x88(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x188(%rbp)
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rdi
callq 0x5cc0
movsbl %al, %eax
cmpl $0x0, %eax
jl 0x12803
movq -0x188(%rbp), %rax
movq %rax, -0x10(%rbp)
jmp 0x12811
movq -0x188(%rbp), %rax
movq (%rax), %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rax
movslq -0x8c(%rbp), %rcx
movb $0x5f, (%rax,%rcx)
movq -0x158(%rbp), %rdi
callq 0x5720
testb $0x1, %al
jne 0x12874
movq -0x150(%rbp), %rax
movq %rax, -0xd8(%rbp)
leaq 0x50c3f(%rip), %rax     # 0x63484
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
movq %rdi, -0x190(%rbp)
callq 0x5c00
movq -0x190(%rbp), %rdi
movq -0xe0(%rbp), %rsi
callq 0x5c20
jmp 0x12887
movq -0x158(%rbp), %rsi
movq -0x150(%rbp), %rdi
callq 0x5d40
movq -0x148(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
nopw (%rax,%rax)