diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2018-04-29 21:19:14 +0200 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2018-04-30 10:04:36 +0200 |
| commit | 0df55a731aa3330f82d22b010a7a2a4d66521972 (patch) | |
| tree | 9e207f01dbb52afb0de83f95e63ecfc771e809c6 /vendor/github.com/jpillora/longestcommon/lc.go | |
| parent | 1f3fcfa9366e49b371c7be2b5c90b957ce93b8dd (diff) | |
| download | go-schwift-0df55a731aa3330f82d22b010a7a2a4d66521972.tar.gz | |
initial support for large objects
This has gone through a lot of iterations on my branch, and I'm quite
happy with the parts of the API that exist now. Test coverage can still
be better, and will get better in the following commits.
The API is not yet finished: I want to add Options arguments to
Object.Upload(), Object.Copy(), Object.Move() and Object.Delete() that
specify how each of these operations affect existing segments (and,
later, also existing symlinks). For Upload(), uploading in segments
shall become as easy as flipping a single switch.
Diffstat (limited to 'vendor/github.com/jpillora/longestcommon/lc.go')
| -rw-r--r-- | vendor/github.com/jpillora/longestcommon/lc.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/vendor/github.com/jpillora/longestcommon/lc.go b/vendor/github.com/jpillora/longestcommon/lc.go new file mode 100644 index 0000000..788a7a6 --- /dev/null +++ b/vendor/github.com/jpillora/longestcommon/lc.go @@ -0,0 +1,82 @@ +package longestcommon + +import "strings" + +//TrimPrefix removes the longest common prefix from all provided strings +func TrimPrefix(strs []string) { + p := Prefix(strs) + if p == "" { + return + } + for i, s := range strs { + strs[i] = strings.TrimPrefix(s, p) + } +} + +//TrimSuffix removes the longest common suffix from all provided strings +func TrimSuffix(strs []string) { + p := Suffix(strs) + if p == "" { + return + } + for i, s := range strs { + strs[i] = strings.TrimSuffix(s, p) + } +} + +//Prefix returns the longest common prefix of the provided strings +func Prefix(strs []string) string { + return longestCommonXfix(strs, true) +} + +//Suffix returns the longest common suffix of the provided strings +func Suffix(strs []string) string { + return longestCommonXfix(strs, false) +} + +func longestCommonXfix(strs []string, pre bool) string { + //short-circuit empty list + if len(strs) == 0 { + return "" + } + xfix := strs[0] + //short-circuit single-element list + if len(strs) == 1 { + return xfix + } + //compare first to rest + for _, str := range strs[1:] { + xfixl := len(xfix) + strl := len(str) + //short-circuit empty strings + if xfixl == 0 || strl == 0 { + return "" + } + //maximum possible length + maxl := xfixl + if strl < maxl { + maxl = strl + } + //compare letters + if pre { + //prefix, iterate left to right + for i := 0; i < maxl; i++ { + if xfix[i] != str[i] { + xfix = xfix[:i] + break + } + } + } else { + //suffix, iternate right to left + for i := 0; i < maxl; i++ { + xi := xfixl - i - 1 + si := strl - i - 1 + if xfix[xi] != str[si] { + xfix = xfix[xi+1:] + break + } + } + } + } + return xfix +} |
