2020-02-02 02:00:47 +01:00
|
|
|
package txt
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
2020-04-26 14:31:33 +02:00
|
|
|
const (
|
2020-04-26 16:22:29 +02:00
|
|
|
ClipDefault = 160
|
|
|
|
ClipSlug = 80
|
|
|
|
ClipKeyword = 40
|
2020-12-31 13:51:31 +01:00
|
|
|
ClipVarchar = 255
|
2020-04-26 16:22:29 +02:00
|
|
|
ClipDescription = 16000
|
2020-04-26 14:31:33 +02:00
|
|
|
)
|
|
|
|
|
2020-02-02 02:00:47 +01:00
|
|
|
func Clip(s string, size int) string {
|
2020-04-26 14:31:33 +02:00
|
|
|
s = strings.TrimSpace(s)
|
|
|
|
|
|
|
|
if s == "" || size <= 0 {
|
2020-02-02 02:00:47 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
runes := []rune(s)
|
|
|
|
|
|
|
|
if len(runes) > size {
|
2020-02-02 03:36:00 +01:00
|
|
|
s = string(runes[0 : size-1])
|
2020-02-02 02:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
2020-10-05 07:40:11 +02:00
|
|
|
|
|
|
|
func TrimLen(s string, size int) string {
|
|
|
|
if len(s) < size || size < 4 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return Clip(s, size-3) + "..."
|
|
|
|
}
|