2020-08-29 06:52:22 +05:30
|
|
|
package common
|
2019-10-21 14:22:31 -07:00
|
|
|
|
2021-03-11 22:06:04 +02:00
|
|
|
import (
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// IsAnchor is a function handler
|
2019-10-21 14:22:31 -07:00
|
|
|
type IsAnchor func(str string) bool
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//IsConditionAnchor checks for condition anchor
|
2019-10-21 14:22:31 -07:00
|
|
|
func IsConditionAnchor(str string) bool {
|
|
|
|
if len(str) < 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return (str[0] == '(' && str[len(str)-1] == ')')
|
|
|
|
}
|
|
|
|
|
2021-09-13 18:59:28 +03:00
|
|
|
//IsGlobalAnchor checks for global condition anchor
|
|
|
|
func IsGlobalAnchor(str string) bool {
|
|
|
|
left := "<("
|
|
|
|
right := ")"
|
|
|
|
if len(str) < len(left)+len(right) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: trim spaces ?
|
|
|
|
return (str[:len(left)] == left && str[len(str)-len(right):] == right)
|
|
|
|
}
|
|
|
|
|
|
|
|
//ContainsCondition returns true, if str is either condition anchor or
|
|
|
|
// global condition anchor
|
|
|
|
func ContainsCondition(str string) bool {
|
|
|
|
return IsConditionAnchor(str) || IsGlobalAnchor(str)
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//IsNegationAnchor checks for negation anchor
|
2019-10-21 14:22:31 -07:00
|
|
|
func IsNegationAnchor(str string) bool {
|
|
|
|
left := "X("
|
|
|
|
right := ")"
|
|
|
|
if len(str) < len(left)+len(right) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//TODO: trim spaces ?
|
|
|
|
return (str[:len(left)] == left && str[len(str)-len(right):] == right)
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// IsAddingAnchor checks for addition anchor
|
2019-10-21 14:22:31 -07:00
|
|
|
func IsAddingAnchor(key string) bool {
|
|
|
|
const left = "+("
|
|
|
|
const right = ")"
|
|
|
|
|
|
|
|
if len(key) < len(left)+len(right) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return left == key[:len(left)] && right == key[len(key)-len(right):]
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
// IsEqualityAnchor checks for equality anchor
|
2019-10-21 14:22:31 -07:00
|
|
|
func IsEqualityAnchor(str string) bool {
|
|
|
|
left := "=("
|
|
|
|
right := ")"
|
|
|
|
if len(str) < len(left)+len(right) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
//TODO: trim spaces ?
|
|
|
|
return (str[:len(left)] == left && str[len(str)-len(right):] == right)
|
|
|
|
}
|
|
|
|
|
2020-01-24 12:05:53 -08:00
|
|
|
//IsExistenceAnchor checks for existence anchor
|
|
|
|
func IsExistenceAnchor(str string) bool {
|
2019-10-21 14:22:31 -07:00
|
|
|
left := "^("
|
|
|
|
right := ")"
|
|
|
|
|
|
|
|
if len(str) < len(left)+len(right) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return (str[:len(left)] == left && str[len(str)-len(right):] == right)
|
|
|
|
}
|
2019-12-30 17:08:50 -08:00
|
|
|
|
2021-07-23 20:53:37 +03:00
|
|
|
// IsNonAnchor checks that key does not have any anchor
|
|
|
|
func IsNonAnchor(str string) bool {
|
|
|
|
key, _ := RemoveAnchor(str)
|
|
|
|
return str == key
|
|
|
|
}
|
|
|
|
|
2021-01-07 11:24:38 -08:00
|
|
|
// RemoveAnchor remove anchor from the given key. It returns
|
|
|
|
// the anchor-free tag value and the prefix of the anchor.
|
|
|
|
func RemoveAnchor(key string) (string, string) {
|
2019-12-30 17:08:50 -08:00
|
|
|
if IsConditionAnchor(key) {
|
2021-01-07 11:24:38 -08:00
|
|
|
return key[1 : len(key)-1], key[0:1]
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
2021-09-13 18:59:28 +03:00
|
|
|
if IsExistenceAnchor(key) || IsAddingAnchor(key) || IsEqualityAnchor(key) || IsNegationAnchor(key) || IsGlobalAnchor(key) {
|
2021-01-07 11:24:38 -08:00
|
|
|
return key[2 : len(key)-1], key[0:2]
|
2019-12-30 17:08:50 -08:00
|
|
|
}
|
|
|
|
|
2021-01-07 11:24:38 -08:00
|
|
|
return key, ""
|
|
|
|
}
|
|
|
|
|
2021-03-11 22:06:04 +02:00
|
|
|
// RemoveAnchorsFromPath removes all anchor from path string
|
|
|
|
func RemoveAnchorsFromPath(str string) string {
|
|
|
|
components := strings.Split(str, "/")
|
|
|
|
if components[0] == "" {
|
|
|
|
components = components[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, component := range components {
|
|
|
|
components[i], _ = RemoveAnchor(component)
|
|
|
|
}
|
|
|
|
|
|
|
|
newPath := path.Join(components...)
|
|
|
|
if path.IsAbs(str) {
|
|
|
|
newPath = "/" + newPath
|
|
|
|
}
|
|
|
|
return newPath
|
|
|
|
}
|
|
|
|
|
2021-01-07 11:24:38 -08:00
|
|
|
// AddAnchor adds an anchor with the supplied prefix.
|
|
|
|
// The suffix is assumed to be ")".
|
|
|
|
func AddAnchor(key, anchorPrefix string) string {
|
|
|
|
return anchorPrefix + key + ")"
|
2020-01-24 12:05:53 -08:00
|
|
|
}
|