package yu_path

// Format
//
//	@Description: 格式化文件或目录名称，去除所有转义符及不合规符号
func Format(s string) string {
	j_buf := make([]byte, 0, len(s))
	j_space := true
	for j_i := 0; j_i < len(s); j_i++ {
		switch s[j_i] {
		case 0, '\\', '/', '<', '>', ':', '"', '|', '?', '*':
		case ' ':
			if j_space {
				continue
			}
			j_buf = append(j_buf, s[j_i])
		default:
			j_space = false
			j_buf = append(j_buf, s[j_i])
		}
	}
	j_eof := len(j_buf)
	for j_eof > 0 {
		j_eof--
		if j_buf[j_eof] != ' ' && j_buf[j_eof] != '.' {
			break
		}
	}
	if j_buf[j_eof] == ' ' || j_buf[j_eof] == '.' {
		return ""
	}
	j_eof++
	return string(j_buf[:j_eof])
}

// Join
//
//	@Description: 组合路径
func Join(s ...string) string {
	if len(s) == 0 {
		return ""
	}
	if len(s) == 1 {
		return s[0]
	}
	j_buf := make([]byte, 0, len(s)*128)
	j_buf = append(j_buf, s[0]...)
	for j_i := 1; j_i < len(s); j_i++ {
		j_buf = append(j_buf, '\\')
		j_buf = append(j_buf, s[j_i]...)
	}
	return string(j_buf)
}

// JoinFormat
//
//	@Description: 组合路径并去除路径中的转义符，如：["\\","\/","//","../","..\"]
func JoinFormat(s ...string) string {
	if len(s) == 0 {
		return ""
	}
	if len(s) == 1 {
		return Format(s[0])
	}
	j_buf := make([]byte, 0, len(s)*128)
	j_buf = append(j_buf, Format(s[0])...)
	for j_i := 1; j_i < len(s); j_i++ {
		j_buf = append(j_buf, '\\')
		j_buf = append(j_buf, Format(s[j_i])...)
	}
	return string(j_buf)
}
