-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprefix_processor.go
58 lines (51 loc) · 1.78 KB
/
prefix_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2017 Γεράσιμος Μαρόπουλος. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package unis
import (
"strings"
)
// NewPrefixRemover accepts a "prefix" and returns a new processor
// which returns the result without that "prefix".
func NewPrefixRemover(prefix string) ProcessorFunc {
return func(original string) (result string) {
result = original
for {
if strings.HasPrefix(result, prefix) {
result = result[len(prefix):]
} else {
break
}
}
return
}
}
// NewPrepender accepts a "prefix" and returns a new processor
// which returns the result prepended with that "prefix"
// if the "original"'s prefix != prefix.
func NewPrepender(prefix string) ProcessorFunc {
return func(original string) string {
if !strings.HasPrefix(original, prefix) {
return prefix + original
}
return original
}
}
// NewExclusivePrepender accepts a "prefix" and returns a new processor
// which returns the result prepended with that "prefix"
// if the "original"'s prefix != prefix.
// The difference from NewPrepender is that
// this processor will make sure that
// the prefix is that "prefix" series of characters,
// i.e:
// 1. "//path" -> NewPrepender("/") |> "//path"
// It has a prefix already, so it doesn't prepends the "/" to the "//path",
// but it doesn't checks if that is the correct prefix.
// 1. "//path" -> NewExclusivePrepender("/") |> "/path"
// Checks if that is the correct prefix, if so returns as it's,
// otherwise replace the duplications and prepend the correct prefix.
func NewExclusivePrepender(prefix string) ProcessorFunc {
prefixRemover := NewPrefixRemover(prefix)
prepender := NewPrepender(prefix)
return NewChain(prefixRemover, prepender)
}