// ProgramRule adds the rule specified by args only if the
// rule is not already present in the chain. Reciprocally,
// it removes the rule only if present.
190992
func ProgramRule(table Table, chain string, action Action, args []string) error {
	if Exists(table, chain, args...) != (action == Delete) {
		return nil
	}
	return RawCombinedOutput(append([]string{"-t", string(table), string(action), chain}, args...)...)
}
55274
func NewStringRule(key string, req bool, opts ...string) (*StringRule, error) {
	// Return error if key is empty
	if key == "" {
		return nil, EmptyKeyError
	}

	var def *string
	if len(opts) > 0 {
		def = &opts[0]
	}

	return &StringRule{
		key:      key,
		required: req,
		default_: def,
	}, nil
}
805688
func (en *en_SS) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
	return locales.PluralRuleOther
}
1233905
func (s *DescribeReceiptRuleOutput) SetRule(v *ReceiptRule) *DescribeReceiptRuleOutput {
	s.Rule = v
	return s
}
161118
func (es *es_CO) RangePluralRule(num1 float64, v1 uint64, num2 float64, v2 uint64) locales.PluralRule {
	return locales.PluralRuleOther
}
1295716
func (r *Rules) parseRule(rule string) error {
	rule = strings.TrimSpace(rule)

	// Ignore blank lines
	if rule == "" {
		return nil
	}
	// Comment
	if strings.HasPrefix(rule, "#") {
		return nil
	}

	// Fail any rules that contain **
	if strings.Contains(rule, "**") {
		return errors.New("double-star (**) syntax is not supported")
	}

	// Fail any patterns that can't compile. A non-empty string must be
	// given to Match() to avoid optimization that skips rule evaluation.
	if _, err := filepath.Match(rule, "abc"); err != nil {
		return err
	}

	p := &pattern{raw: rule}

	// Negation is handled at a higher level, so strip the leading ! from the
	// string.
	if strings.HasPrefix(rule, "!") {
		p.negate = true
		rule = rule[1:]
	}

	// Directory verification is handled by a higher level, so the trailing /
	// is removed from the rule. That way, a directory named "foo" matches,
	// even if the supplied string does not contain a literal slash character.
	if strings.HasSuffix(rule, "/") {
		p.mustDir = true
		rule = strings.TrimSuffix(rule, "/")
	}

	if strings.HasPrefix(rule, "/") {
		// Require path matches the root path.
		p.match = func(n string, fi os.FileInfo) bool {
			rule = strings.TrimPrefix(rule, "/")
			ok, err := filepath.Match(rule, n)
			if err != nil {
				log.Printf("Failed to compile %q: %s", rule, err)
				return false
			}
			return ok
		}
	} else if strings.Contains(rule, "/") {
		// require structural match.
		p.match = func(n string, fi os.FileInfo) bool {
			ok, err := filepath.Match(rule, n)
			if err != nil {
				log.Printf("Failed to compile %q: %s", rule, err)
				return false
			}
			return ok
		}
	} else {
		p.match = func(n string, fi os.FileInfo) bool {
			// When there is no slash in the pattern, we evaluate ONLY the
			// filename.
			n = filepath.Base(n)
			ok, err := filepath.Match(rule, n)
			if err != nil {
				log.Printf("Failed to compile %q: %s", rule, err)
				return false
			}
			return ok
		}
	}

	r.patterns = append(r.patterns, p)
	return nil
}
499446
func FindRule(key string) *Rule {
	collections.ruleMutex.Lock()
	defer collections.ruleMutex.Unlock()

	obj := collections.rules[key]
	if obj == nil {
		return nil
	}

	return obj
}
780154
func (evt *Event) HasRule() bool {
	_, ok := evt.Node.(*ast.Rule)
	return ok
}
158664
func (af *af) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
	return locales.PluralRuleOther
}
594953
func lengthRule(ctx *Context, arg *Argument) error {
	if arg.Value == "" || arg.Value == "~" {
		return errors.Format("rule `length` expected 1-2 arguments like: (5), [3~8) etc")
	}

	pair := strings.Split(arg.Value, "~")
	if len(pair) > 2 {
		return errors.Format("invalid argument for rule `length`: %v", arg.Value)
	}

	v := reflects.Indirect(ctx.Value)
	if err := assertKind("length", ctx, v, reflect.String, reflect.Slice, reflect.Map, reflect.Chan); err != nil {
		return err
	}

	l := v.Len()
	if len(pair) == 1 {
		length, err := strconv.Atoi(arg.Value)
		if err != nil {
			return err
		}
		return assert(l == length, "length1", ctx, arg, l)
	} else if len(pair) == 2 {
		start, err := toInt(pair[0], -1)
		if err != nil {
			return err
		}
		end, err := toInt(pair[1], math.MaxInt32)
		if err != nil {
			return err
		}

		if arg.Left == LeftOpen {
			err = assert(l > start, "length2", ctx, arg, l)
		} else if arg.Left == LeftSquare {
			err = assert(l >= start, "length2", ctx, arg, l)
		}
		if err != nil {
			return err
		}

		if arg.Right == RightOpen {
			err = assert(l < end, "length2", ctx, arg, l)
		} else if arg.Right == RightSquare {
			err = assert(l <= end, "length2", ctx, arg, l)
		}
		if err != nil {
			return err
		}
	}
	return nil
}
193148
func NewRule(p Pos, name *Identifier) *Rule {
	return &Rule{p: p, Name: name}
}
270753
func checkIfRuleExists(listChainOutput string, args ...string) bool {
	rule := strings.Join(args, " ")
	for _, line := range strings.Split(listChainOutput, "\n") {
		if strings.TrimSpace(line) == rule {
			return true
		}
	}
	return false
}
1353328
func ParseRemovalRule(rule string) (RemovalRule, error) {
	rule = strings.TrimSpace(rule)
	tokens := strings.Split(rule, "=")

	// Simple little language:
	//   port=N matches rules that filter (only) by port=N
	//
	// Note this language is internal, so isn't required to be stable

	if len(tokens) == 2 {
		if tokens[0] == "port" {
			port, err := strconv.Atoi(tokens[1])
			if err != nil {
				return nil, fmt.Errorf("cannot parse rule %q", rule)
			}

			return &PortRemovalRule{Port: port}, nil
		} else {
			return nil, fmt.Errorf("cannot parse rule %q", rule)
		}
	}
	return nil, fmt.Errorf("cannot parse rule %q", rule)
}
1064514
func applyRules(text string, rules []rule) string {
	for _, rule := range rules {
		text = rule.sub(text)
	}
	return text
}
169374
func (fo *fo_FO) OrdinalPluralRule(num float64, v uint64) locales.PluralRule {
	return locales.PluralRuleUnknown
}
348971
func ParseRuleFromCallExpr(module *Module, terms []*Term) (*Rule, error) {

	if len(terms) <= 1 {
		return nil, fmt.Errorf("rule argument list must take at least one argument")
	}

	loc := terms[0].Location
	args := terms[1:]
	value := BooleanTerm(true).SetLocation(loc)

	rule := &Rule{
		Location: loc,
		Head: &Head{
			Location: loc,
			Name:     Var(terms[0].String()),
			Args:     args,
			Value:    value,
		},
		Module: module,
		Body:   NewBody(NewExpr(BooleanTerm(true).SetLocation(loc)).SetLocation(loc)),
	}
	return rule, nil
}
1163819
func (r *ChainRouter) Rule(mo, pattern string, fx FlatHandler) FlatChains {
	if fx == nil {
		fx = IdentityCall
	}
	methods := GetMethods(mo)
	patt := reggy.CreateClassic(pattern)
	fr := FlatRouteBuild(methods, patt, fx, r.Log)
	cr := &ChainRouta{ClassicMatchMux: patt, Handler: fr.Handle}
	r.wg.Lock()
	defer r.wg.Unlock()
	r.paths = append(r.paths, cr)
	return fr
}