123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package util
- import (
- "errors"
- "math"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- )
- // SplitPath 将路径按分隔符分隔成字符串数组。比如:
- // /a/b/c ==> []string{"a", "b", "c"}
- func SplitPath(path string) []string {
- vol := filepath.VolumeName(path)
- ret := make([]string, 0, 10)
- index := 0
- if len(vol) > 0 {
- ret = append(ret, vol)
- path = path[len(vol)+1:]
- }
- for i := 0; i < len(path); i++ {
- if os.IsPathSeparator(path[i]) {
- if i > index {
- ret = append(ret, path[index:i])
- }
- index = i + 1 // 过滤掉此符号
- }
- } // end for
- if len(path) > index {
- ret = append(ret, path[index:])
- }
- return ret
- }
- // Round 返回浮点数四舍五入后的整数
- func Round(v float64) int64 {
- return int64(math.Round(v))
- }
- // MaxInt64 返回int64最大值
- func MaxInt64(x int, y int) int {
- if x >= y {
- return x
- }
- return y
- }
- // MinInt64 返回int64最小值
- func MinInt64(x int, y int) int {
- if x <= y {
- return x
- }
- return y
- }
- // MaxInt 返回int最大值
- func MaxInt(x int, y int) int {
- if x >= y {
- return x
- }
- return y
- }
- // MinInt 返回int最小值
- func MinInt(x int, y int) int {
- if x <= y {
- return x
- }
- return y
- }
- // IdCertGender 身份证的性别
- // 返回:1 => 男性,0 => 女性
- func IdCertGender(idcert string) (int, error) {
- if !IsIdCert(idcert) {
- return 0, errors.New("invalid idcert")
- }
- v, err := strconv.Atoi(idcert[17:17])
- if err != nil {
- return 0, err
- }
- return v / 2, nil
- }
- // 生成顺序码
- func GenerateCode(code, prefix string, width int) string {
- // 生成编码
- if code == "" {
- code = "0"
- }
- if prefix != "" {
- code = strings.Replace(code, prefix, "", -1)
- }
- codeInt, _ := strconv.Atoi(code)
- codeInt = codeInt + 1
- code = strconv.Itoa(codeInt)
- codeLen := len(code)
- if codeLen < width {
- for i := 0; i < width-codeLen; i++ {
- code = "0" + code
- }
- }
- if prefix != "" {
- code = prefix + code
- }
- return code
- }
|