๐Ÿ“ฆ jtr109 / go-playground

๐Ÿ“„ lib.go ยท 22 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22package simplifypath

import "strings"

func simplifyPath(path string) string {
	paths := strings.Split(path, "/")
	result := []string{}
	for _, p := range paths {
		if p == "." || p == "" {
			continue
		}
		if p == ".." {
			if len(result) > 0 {
				result = result[:len(result)-1]
			}
			continue
		}
		result = append(result, p)
	}
	return "/" + strings.Join(result, "/")
}