github.com/searKing/golang/go@v1.2.117/strings/trim.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package strings 6 7 import ( 8 "fmt" 9 "io" 10 "strings" 11 "unicode" 12 ) 13 14 // SplitPrefixNumber slices s into number prefix and unparsed and 15 // returns a slice of those substrings. 16 // If s does not start with number, SplitPrefixNumber returns 17 // a slice of length 1 whose only element is s. 18 // If s is with number only, SplitPrefixNumber returns 19 // a slice of length 1 whose only element is s. 20 func SplitPrefixNumber(s string) []string { 21 unparsed := TrimPrefixNumber(s) 22 if unparsed == "" || len(unparsed) == len(s) { 23 return []string{s} 24 } 25 return []string{s[:len(s)-len(unparsed)], unparsed} 26 } 27 28 // TrimPrefixNumber returns s without the leading number prefix string. 29 // If s doesn't start with number prefix, s is returned unchanged. 30 func TrimPrefixNumber(s string) string { 31 unparsedFloat := TrimPrefixFloat(s) 32 unparsedInt := TrimPrefixInteger(s) 33 if len(unparsedFloat) < len(unparsedInt) { 34 return unparsedFloat 35 } 36 return unparsedInt 37 } 38 39 // TrimPrefixFloat returns s without the leading float prefix string. 40 // If s doesn't start with float prefix, s is returned unchanged. 41 func TrimPrefixFloat(s string) string { 42 var value float64 43 var unparsed string 44 // Scanf, Fscanf, and Sscanf parse the arguments according to a format string, analogous to that of Printf. 45 // In the text that follows, 'space' means any Unicode whitespace character except newline. 46 // Input processed by verbs is implicitly space-delimited: 47 // the implementation of every verb except %c starts by discarding leading spaces from the remaining input, 48 // and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. 49 // see https://golang.org/pkg/fmt/#hdr-Scanning 50 space := strings.IndexFunc(s, func(r rune) bool { 51 if r == '\n' { 52 return false 53 } 54 return unicode.IsSpace(r) 55 }) 56 if space < 0 { 57 space = len(s) 58 } 59 count, err := fmt.Sscanf(s[:space], `%v%s`, &value, &unparsed) 60 61 if (err != nil && err != io.EOF) || (count == 0) { 62 return s 63 } 64 return s[space-len(unparsed):] 65 } 66 67 // TrimPrefixInteger returns s without the leading integer prefix string. 68 // If s doesn't start with integer prefix, s is returned unchanged. 69 func TrimPrefixInteger(s string) string { 70 var value int64 71 var unparsed string 72 // Scanf, Fscanf, and Sscanf parse the arguments according to a format string, analogous to that of Printf. 73 // In the text that follows, 'space' means any Unicode whitespace character except newline. 74 // Input processed by verbs is implicitly space-delimited: 75 // the implementation of every verb except %c starts by discarding leading spaces from the remaining input, 76 // and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. 77 // see https://golang.org/pkg/fmt/#hdr-Scanning 78 space := strings.IndexFunc(s, func(r rune) bool { 79 if r == '\n' { 80 return false 81 } 82 return unicode.IsSpace(r) 83 }) 84 if space < 0 { 85 space = len(s) 86 } 87 count, err := fmt.Sscanf(s[:space], `%v%s`, &value, &unparsed) 88 89 if (err != nil && err != io.EOF) || (count == 0) { 90 return s 91 } 92 return s[space-len(unparsed):] 93 } 94 95 // TrimPrefixComplex returns s without the leading complex prefix string. 96 // If s doesn't start with complex prefix, s is returned unchanged. 97 func TrimPrefixComplex(s string) string { 98 var value complex128 99 var unparsed string 100 // Scanf, Fscanf, and Sscanf parse the arguments according to a format string, analogous to that of Printf. 101 // In the text that follows, 'space' means any Unicode whitespace character except newline. 102 // Input processed by verbs is implicitly space-delimited: 103 // the implementation of every verb except %c starts by discarding leading spaces from the remaining input, 104 // and the %s verb (and %v reading into a string) stops consuming input at the first space or newline character. 105 // see https://golang.org/pkg/fmt/#hdr-Scanning 106 space := strings.IndexFunc(s, func(r rune) bool { 107 if r == '\n' { 108 return false 109 } 110 return unicode.IsSpace(r) 111 }) 112 if space < 0 { 113 space = len(s) 114 } 115 count, err := fmt.Sscanf(s[:space], `%v%s`, &value, &unparsed) 116 117 if (err != nil && err != io.EOF) || (count == 0) { 118 return s 119 } 120 return s[space-len(unparsed):] 121 }