github.com/dubbogo/gost@v1.14.0/strings/strings.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package gxstrings 19 20 import ( 21 "reflect" 22 "regexp" 23 "strings" 24 ) 25 26 func IsNil(i interface{}) bool { 27 if i == nil { 28 return true 29 } 30 31 if reflect.ValueOf(i).IsNil() { 32 return true 33 } 34 35 return false 36 } 37 38 func RegSplit(text string, regexSplit string) []string { 39 reg := regexp.MustCompile(regexSplit) 40 indexes := reg.FindAllStringIndex(text, -1) 41 lastStart := 0 42 result := make([]string, len(indexes)+1) 43 for i, element := range indexes { 44 result[i] = text[lastStart:element[0]] 45 lastStart = element[1] 46 } 47 result[len(indexes)] = text[lastStart:] 48 return result 49 } 50 51 // IsMatchPattern is used to determine whether pattern 52 // and value match with wildcards currently supported * 53 func IsMatchPattern(pattern string, value string) bool { 54 if "*" == pattern { 55 return true 56 } 57 if len(pattern) == 0 && len(value) == 0 { 58 return true 59 } 60 if len(pattern) == 0 || len(value) == 0 { 61 return false 62 } 63 i := strings.LastIndex(pattern, "*") 64 switch i { 65 case -1: 66 // doesn't find "*" 67 return value == pattern 68 case len(pattern) - 1: 69 // "*" is at the end 70 return strings.HasPrefix(value, pattern[0:i]) 71 case 0: 72 // "*" is at the beginning 73 return strings.HasSuffix(value, pattern[1:]) 74 default: 75 // "*" is in the middle 76 prefix := pattern[0:i] 77 suffix := pattern[i+1:] 78 return strings.HasPrefix(value, prefix) && strings.HasSuffix(value, suffix) 79 } 80 }