Skip to content

Commit

Permalink
remove named parameters of SourceDirSubdir and document it with examp…
Browse files Browse the repository at this point in the history
…les instead

* also avoid protocol to be matched from the query parameters
  • Loading branch information
azr committed Jan 8, 2019
1 parent e4ac591 commit a6260be
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,37 @@ import (
"strings"
)

// SourceDirSubdir takes a source and returns the URL without
// SourceDirSubdir takes a source URL and returns a tuple of the URL without
// the subdir and the subdir.
func SourceDirSubdir(src string) (url string, subdir string) {
// Calcaulate an offset to avoid accidentally marking the scheme
// as the dir.
var offset int
if idx := strings.Index(src, "://"); idx > -1 {
offset = idx + 3
}
//
// ex:
// dom.com/path/?q=p => dom.com/path/?q=p, ""
// proto://dom.com/path//*?q=p => proto://dom.com/path?q=p, "*"
// proto://dom.com/path//path2?q=p => proto://dom.com/path?q=p, "path2"
//
func SourceDirSubdir(src string) (string, string) {

// URL might contains another url in query parameters
stop := len(src)
if idx := strings.Index(src, "?"); idx > -1 {
stop = idx
}

// Calculate an offset to avoid accidentally marking the scheme
// as the dir.
var offset int
if idx := strings.Index(src[:stop], "://"); idx > -1 {
offset = idx + 3
}

// First see if we even have an explicit subdir
idx := strings.Index(src[offset:stop], "//")
if idx == -1 {
return src, ""
}

idx += offset
subdir = src[idx+2:]
subdir := src[idx+2:]
src = src[:idx]

// Next, check if we have query parameters and push them onto the
Expand Down

0 comments on commit a6260be

Please sign in to comment.