| | |
| | |
| | |
| |
|
| | package objabi |
| |
|
| | import ( |
| | "fmt" |
| | "strconv" |
| | "strings" |
| | ) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | func PathToPrefix(s string) string { |
| | slash := strings.LastIndex(s, "/") |
| | |
| | n := 0 |
| | for r := 0; r < len(s); r++ { |
| | if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F { |
| | n++ |
| | } |
| | } |
| |
|
| | |
| | if n == 0 { |
| | return s |
| | } |
| |
|
| | |
| | const hex = "0123456789abcdef" |
| | p := make([]byte, 0, len(s)+2*n) |
| | for r := 0; r < len(s); r++ { |
| | if c := s[r]; c <= ' ' || (c == '.' && r > slash) || c == '%' || c == '"' || c >= 0x7F { |
| | p = append(p, '%', hex[c>>4], hex[c&0xF]) |
| | } else { |
| | p = append(p, c) |
| | } |
| | } |
| |
|
| | return string(p) |
| | } |
| |
|
| | |
| | |
| | func PrefixToPath(s string) (string, error) { |
| | percent := strings.IndexByte(s, '%') |
| | if percent == -1 { |
| | return s, nil |
| | } |
| |
|
| | p := make([]byte, 0, len(s)) |
| | for i := 0; i < len(s); { |
| | if s[i] != '%' { |
| | p = append(p, s[i]) |
| | i++ |
| | continue |
| | } |
| | if i+2 >= len(s) { |
| | |
| | |
| | return "", fmt.Errorf("malformed prefix %q: escape sequence must contain two hex digits", s) |
| | } |
| |
|
| | b, err := strconv.ParseUint(s[i+1:i+3], 16, 8) |
| | if err != nil { |
| | |
| | return "", fmt.Errorf("malformed prefix %q: escape sequence %q must contain two hex digits", s, s[i:i+3]) |
| | } |
| |
|
| | p = append(p, byte(b)) |
| | i += 3 |
| | } |
| | return string(p), nil |
| | } |
| |
|