|
| 1 | +/* |
| 2 | + * Copyright 2021-present by Nedim Sabic Sabic |
| 3 | + * https://www.fibratus.io |
| 4 | + * All Rights Reserved. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package functions |
| 20 | + |
| 21 | +import ( |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/rabbitstack/fibratus/pkg/util/wildcard" |
| 25 | +) |
| 26 | + |
| 27 | +// Count counts the number of items in the slice or substrings |
| 28 | +// in the string that is matching a wildcard pattern. |
| 29 | +type Count struct{} |
| 30 | + |
| 31 | +func (f Count) Call(args []interface{}) (any, bool) { |
| 32 | + if len(args) < 2 { |
| 33 | + return false, false |
| 34 | + } |
| 35 | + |
| 36 | + var count int |
| 37 | + var caseInsensitive bool = true |
| 38 | + |
| 39 | + pattern := parseString(1, args) |
| 40 | + |
| 41 | + if len(args) > 2 { |
| 42 | + caseInsensitive, _ = args[2].(bool) |
| 43 | + } |
| 44 | + |
| 45 | + switch s := args[0].(type) { |
| 46 | + case string: |
| 47 | + substrings := strings.Fields(s) |
| 48 | + for _, ss := range substrings { |
| 49 | + switch caseInsensitive { |
| 50 | + case true: |
| 51 | + if wildcard.Match(strings.ToLower(pattern), strings.ToLower(ss)) { |
| 52 | + count++ |
| 53 | + } |
| 54 | + case false: |
| 55 | + if wildcard.Match(pattern, ss) { |
| 56 | + count++ |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + case []string: |
| 61 | + for _, i := range s { |
| 62 | + switch caseInsensitive { |
| 63 | + case true: |
| 64 | + if wildcard.Match(strings.ToLower(pattern), strings.ToLower(i)) { |
| 65 | + count++ |
| 66 | + } |
| 67 | + case false: |
| 68 | + if wildcard.Match(pattern, i) { |
| 69 | + count++ |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return count, true |
| 76 | +} |
| 77 | + |
| 78 | +func (f Count) Desc() FunctionDesc { |
| 79 | + desc := FunctionDesc{ |
| 80 | + Name: CountFn, |
| 81 | + Args: []FunctionArgDesc{ |
| 82 | + {Keyword: "string|slice", Types: []ArgType{Field, BoundField, BoundSegment, BareBoundVariable, Func, String, Slice}, Required: true}, |
| 83 | + {Keyword: "pattern", Types: []ArgType{String}, Required: true}, |
| 84 | + {Keyword: "case_insensitive", Types: []ArgType{Bool}, Required: false}, |
| 85 | + }, |
| 86 | + } |
| 87 | + return desc |
| 88 | +} |
| 89 | + |
| 90 | +func (f Count) Name() Fn { return CountFn } |
0 commit comments