github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ss/parse.go (about)

     1  package ss
     2  
     3  import (
     4  	"strconv"
     5  	"strings"
     6  	"time"
     7  	"unicode"
     8  )
     9  
    10  // ParseFloat32E ...
    11  func ParseFloat32E(s string) (float32, error) {
    12  	f, e := strconv.ParseFloat(s, 32)
    13  	return float32(f), e
    14  }
    15  
    16  // ParseFloat64E ...
    17  func ParseFloat64E(s string) (float64, error) { return strconv.ParseFloat(s, 64) }
    18  
    19  // ParseIntE ...
    20  func ParseIntE(s string) (int, error) { p, e := ParseInt64E(s); return int(p), e }
    21  
    22  // ParseInt8E ...
    23  func ParseInt8E(s string) (int8, error) { p, e := strconv.ParseInt(s, 0, 8); return int8(p), e }
    24  
    25  // ParseInt16E ...
    26  func ParseInt16E(s string) (int16, error) { p, e := strconv.ParseInt(s, 0, 16); return int16(p), e }
    27  
    28  // ParseInt32E ...
    29  func ParseInt32E(s string) (int32, error) { p, e := strconv.ParseInt(s, 0, 32); return int32(p), e }
    30  
    31  // ParseInt64E ...
    32  func ParseInt64E(s string) (int64, error) { return strconv.ParseInt(s, 0, 64) }
    33  
    34  // ParseUintE ...
    35  func ParseUintE(s string) (uint, error) { p, e := ParseUint64E(s); return uint(p), e }
    36  
    37  // ParseUint8E ...
    38  func ParseUint8E(s string) (uint8, error) { p, e := strconv.ParseUint(s, 0, 8); return uint8(p), e }
    39  
    40  // ParseUint16E ...
    41  func ParseUint16E(s string) (uint16, error) { p, e := strconv.ParseUint(s, 0, 16); return uint16(p), e }
    42  
    43  // ParseUint32E ...
    44  func ParseUint32E(s string) (uint32, error) { p, e := strconv.ParseUint(s, 0, 32); return uint32(p), e }
    45  
    46  // ParseUint64E ...
    47  func ParseUint64E(s string) (uint64, error) { return strconv.ParseUint(s, 0, 64) }
    48  
    49  // ParseInt8 ...
    50  func ParseInt8(s string) int8 { i, _ := ParseInt8E(s); return i }
    51  
    52  // ParseInt16 ...
    53  func ParseInt16(s string) int16 { i, _ := ParseInt16E(s); return i }
    54  
    55  // ParseInt32 ...
    56  func ParseInt32(s string) int32 { i, _ := ParseInt32E(s); return i }
    57  
    58  // ParseInt64 ...
    59  func ParseInt64(s string) int64 { i, _ := ParseInt64E(s); return i }
    60  
    61  // ParseUint8 ...
    62  func ParseUint8(s string) uint8 { i, _ := ParseUint8E(s); return i }
    63  
    64  // ParseUint16 ...
    65  func ParseUint16(s string) uint16 { i, _ := ParseUint16E(s); return i }
    66  
    67  // ParseUint32 ...
    68  func ParseUint32(s string) uint32 { i, _ := ParseUint32E(s); return i }
    69  
    70  // ParseUint64 ...
    71  func ParseUint64(s string) uint64 { i, _ := ParseUint64E(s); return i }
    72  
    73  // ParseFloat32 ...
    74  func ParseFloat32(s string) float32 { f, _ := ParseFloat32E(s); return f }
    75  
    76  // ParseFloat64 ...
    77  func ParseFloat64(s string) float64 { f, _ := ParseFloat64E(s); return f }
    78  
    79  // ParseInt ...
    80  func ParseInt(s string) int { f, _ := ParseIntE(s); return f }
    81  
    82  // ParseUint ...
    83  func ParseUint(s string) uint { f, _ := ParseUintE(s); return f }
    84  
    85  // ParseBool returns the boolean value represented by the string.
    86  // It accepts 1, t, true, y, yes, on with camel case incentive.
    87  func ParseBool(s string) bool {
    88  	b, _ := ParseBoolE(s)
    89  	return b
    90  }
    91  
    92  // ParseBoolE returns the boolean value represented by the string.
    93  // It accepts 1, t, true, y, yes, on as true with camel case incentive
    94  // and accepts 0, f false, n, no, off as false with camel case incentive
    95  // Any other value returns an error.
    96  func ParseBoolE(s string) (bool, error) {
    97  	switch strings.ToLower(s) {
    98  	case "1", "t", "true", "y", "yes", "on":
    99  		return true, nil
   100  	case "0", "f", "false", "n", "no", "off":
   101  		return false, nil
   102  	}
   103  
   104  	return false, &strconv.NumError{Func: "ParseBoolE", Num: s, Err: strconv.ErrSyntax}
   105  }
   106  
   107  // ParseDuration ...
   108  func ParseDuration(s string) time.Duration { f, _ := ParseDurationE(s); return f }
   109  
   110  // ParseDurationE ...
   111  func ParseDurationE(s string) (time.Duration, error) {
   112  	return time.ParseDuration(StripSpaces(s))
   113  }
   114  
   115  // Strip strips runes that predicates returns true.
   116  func Strip(str string, predicates ...func(rune) bool) string {
   117  	var b strings.Builder
   118  
   119  	b.Grow(len(str))
   120  
   121  	for _, ch := range str {
   122  		if !matchesAny(predicates, ch) {
   123  			b.WriteRune(ch)
   124  		}
   125  	}
   126  
   127  	return b.String()
   128  }
   129  
   130  func matchesAny(predicates []func(rune) bool, ch rune) bool {
   131  	for _, p := range predicates {
   132  		if p(ch) {
   133  			return true
   134  		}
   135  	}
   136  
   137  	return false
   138  }
   139  
   140  // StripSpaces strips all spaces from the string.
   141  func StripSpaces(str string) string {
   142  	return Strip(str, unicode.IsSpace)
   143  }
   144  
   145  // HasSpaces test if any spaces in the string.
   146  func HasSpaces(str string) bool {
   147  	return Has(str, unicode.IsSpace)
   148  }
   149  
   150  // Has test if any rune which predicates tells it it true.
   151  func Has(str string, predicates ...func(rune) bool) bool {
   152  	for _, ch := range str {
   153  		if matchesAny(predicates, ch) {
   154  			return true
   155  		}
   156  	}
   157  
   158  	return false
   159  }
   160  
   161  func Not(p func(rune) bool) func(rune) bool {
   162  	return func(r rune) bool { return !p(r) }
   163  }