// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, (%rsp)
movzbl 0xf(%r14), %eax
movq %r14, %r12
testb %al, %al
jns 0x9c9e
movq (%r14), %r12
testb %al, %al
js 0x9ca7
xorl $0xf, %eax
jmp 0x9cab
movl 0x8(%r14), %eax
movl %eax, %ebp
xorl %ebx, %ebx
xorl %r13d, %r13d
cmpq %rbp, %r13
jge 0x9da7
movb (%r12,%r13), %al
cmpb $0x20, %al
setae %cl
cmpb $0x7f, %al
setne %al
testb %al, %cl
jne 0x9cd2
movb $0x5f, (%r12,%r13)
movzbl (%r12,%r13), %eax
cmpl $0x2e, %eax
je 0x9cf6
movl $0x3, %r15d
cmpl $0x2f, %eax
jne 0x9cfc
leaq -0x1(%rbx), %rax
cmpq $0x1, %rax
jbe 0x9d3f
xorl %r15d, %r15d
jmp 0x9cfc
incq %rbx
movq %rbx, %r15
cmpb $0x0, (%r12,%r13)
js 0x9d08
incq %r13
jmp 0x9d2e
movl %r13d, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x1aabc
andl $-0x80, %eax
cmpl $0xdc80, %eax           # imm = 0xDC80
jne 0x9d29
movb $0x5f, (%r12,%r13)
movl 0x8(%rsp), %r13d
movb $0x1, %cl
xorl %eax, %eax
movq %r15, %rbx
testb %cl, %cl
jne 0x9cb2
jmp 0x9d9d
cmpq $0x1, %r13
jne 0x9d92
movq %r14, %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x658a
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movzbl 0x17(%rsp), %ecx
movq %rcx, %rsi
xorq $0xf, %rsi
testb %cl, %cl
movl %edx, %edx
leaq 0x8(%rsp), %rcx
cmovnsq %rcx, %rax
cmovnsq %rsi, %rdx
movq %r14, %rdi
movq %rax, %rsi
callq 0x193dc
movb $0x1, %al
xorl %ecx, %ecx
movq %rbx, %r15
jmp 0x9d32
movb $0x5f, -0x1(%r12,%r13)
jmp 0x9cf1
testb %al, %al
jne 0x9c8f
jmp 0x9dfe
decq %rbx
cmpq $0x1, %rbx
jbe 0x9e11
cmpb $0x0, 0xf(%r14)
movq %r14, %rax
jns 0x9dbd
movq (%r14), %rax
cmpb $0x2f, (%rax)
jne 0x9dc5
movb $0x5f, (%rax)
movzbl 0xf(%r14), %eax
testb %al, %al
jns 0x9dd4
movl 0x8(%r14), %eax
jmp 0x9dd7
xorl $0xf, %eax
testl %eax, %eax
je 0x9df1
movups (%r14), %xmm0
movq (%rsp), %rax
movups %xmm0, (%rax)
movb $0xf, 0xf(%r14)
movb $0x0, (%r14)
jmp 0x9dfe
movq (%rsp), %rax
movw $0x5f, (%rax)
movb $0xe, 0xf(%rax)
movq (%rsp), %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x5f, -0x1(%r12,%r13)
jmp 0x9db0
nop