// 2020 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. package netparse import ( "regexp" "strconv" "strings" ) func regexpStrOrEmpty(re *regexp.Regexp, s string) string { t := re.FindStringSubmatch(s) if len(t) > 0 { return t[1] } return "" } func regexpAtoiOrNegOne(re *regexp.Regexp, s string) int { t := re.FindStringSubmatch(s) if len(t) > 0 { if i, err := strconv.Atoi(t[1]); err == nil { return i } } return -1 } func splitAndTrim(s, delim string) []string { r := make([]string, 0) for _, c := range strings.Split(s, delim) { if trimmed := strings.TrimSpace(c); trimmed != "" { r = append(r, trimmed) } } return r } // Atoi returns converts a string to an integer and returns the int. If conversion fails it returns -99999 func Atoi(str string) int { if n, err := strconv.Atoi(str); err == nil { return n } return -9999 }