github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/cmn/cos/strings.go (about) 1 // Package cos provides common low-level types and utilities for all aistore projects 2 /* 3 * Copyright (c) 2018-2024, NVIDIA CORPORATION. All rights reserved. 4 */ 5 package cos 6 7 const _dfltLen = 16 8 9 func BHead(b []byte, ls ...int) string { 10 l := _dfltLen 11 if len(ls) > 0 { 12 l = ls[0] 13 } 14 if len(b) > l { 15 return string(b[:l]) + "..." 16 } 17 return string(b) 18 } 19 20 func SHead(s string) string { 21 if len(s) > _dfltLen { 22 return s[:_dfltLen] + "..." 23 } 24 return s 25 } 26 27 func IsLastB(s string, b byte) bool { 28 l := len(s) 29 return l > 0 && s[l-1] == b 30 } 31 32 func TrimLastB(s string, b byte) string { 33 if l := len(s); s[l-1] == b { 34 return s[:l-1] 35 } 36 return s 37 } 38 39 // return non-empty 40 func Either(lhs, rhs string) string { 41 if lhs != "" { 42 return lhs 43 } 44 return rhs 45 } 46 47 // (common use) 48 func Plural(num int) (s string) { 49 if num != 1 { 50 s = "s" 51 } 52 return 53 }