blob: 427a8a570005af4304dafb5898b0ff31f2449bf2 [file] [log] [blame]
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 utils
import (
"errors"
"fmt"
"regexp"
"strings"
)
var (
ipRegex, _ = regexp.Compile(`^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))`)
)
// HashString hashes a string to a unique hashcode.
func HashString(s string) int {
val := []byte(s)
var h int
for idx := range val {
h = 31*h + int(val[idx])
}
return h
}
func StrJoin(str, key string, value interface{}) string {
if key == "" || value == "" {
return str
}
return str + key + ": " + fmt.Sprint(value) + ", "
}
func VerifyIP(ip string) error {
if strings.Contains(ip, ";") {
return errors.New("multiple IP addr does not support")
}
ips := ipRegex.FindAllString(ip, -1)
if len(ips) == 0 {
return errors.New("IP addr error")
}
if len(ips) > 1 {
return errors.New("multiple IP addr does not support")
}
return nil
}