// File: /workspace/github_cpp_makefile_star2_O3/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, %rbx
leaq 0x8(%rsp), %r15
movzbl 0xf(%r14), %eax
testb %al, %al
js 0xa1b6
xorl $0xf, %eax
movq %r14, %r12
jmp 0xa1bd
movq (%r14), %r12
movl 0x8(%r14), %eax
testl %eax, %eax
je 0xa2a5
movl %eax, %r13d
movq %r12, %rbp
xorl %eax, %eax
xorl %ecx, %ecx
movzbl (%rbp), %edx
cmpl $0x7f, %edx
setne %sil
cmpl $0x20, %edx
setae %dil
testb %sil, %dil
jne 0xa1f1
movb $0x5f, (%rbp)
movl $0x3, %ecx
jmp 0xa216
cmpl $0x2e, %edx
je 0xa208
cmpl $0x2f, %edx
jne 0xa20d
decq %rcx
cmpq $0x1, %rcx
jbe 0xa24c
xorl %ecx, %ecx
jmp 0xa216
incq %rcx
jmp 0xa216
movl $0x3, %ecx
testb %dl, %dl
js 0xa224
incq %rax
leaq (%r12,%rax), %rbp
cmpq %r13, %rax
jl 0xa1cf
jmp 0xa29c
movl %eax, 0x8(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x1ab5c
andl $-0x80, %eax
cmpl $0xdc80, %eax           # imm = 0xDC80
jne 0xa241
movb $0x5f, (%rbp)
movl 0x8(%rsp), %eax
movl $0x3, %ecx
jmp 0xa219
cmpq $0x1, %rax
je 0xa258
movb $0x5f, -0x1(%rbp)
jmp 0xa204
movq %r14, %rdi
movl $0x2, %esi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
callq 0x6b28
movq %rax, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rdx, %rcx
shrq $0x38, %rcx
xorq $0xf, %rcx
testq %rdx, %rdx
movl %edx, %edx
cmovnsq %rcx, %rdx
cmovnsq %r15, %rax
movq %r14, %rdi
movq %rax, %rsi
callq 0x195fc
jmp 0xa1a5
decq %rcx
cmpq $0x1, %rcx
jbe 0xa2ff
movb 0xf(%r14), %al
movq %r14, %rcx
testb %al, %al
jns 0xa2b3
movq (%r14), %rcx
cmpb $0x2f, (%rcx)
jne 0xa2bf
movb $0x5f, (%rcx)
movb 0xf(%r14), %al
testb %al, %al
jns 0xa2c9
movl 0x8(%r14), %eax
jmp 0xa2ce
xorb $0xf, %al
movzbl %al, %eax
testl %eax, %eax
je 0xa2e4
movups (%r14), %xmm0
movups %xmm0, (%rbx)
movb $0xf, 0xf(%r14)
movb $0x0, (%r14)
jmp 0xa2ed
movw $0x5f, (%rbx)
movb $0xe, 0xf(%rbx)
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x5f, -0x1(%rbp)
jmp 0xa2a5
nop