-
Notifications
You must be signed in to change notification settings - Fork 45
/
util.go
54 lines (45 loc) · 865 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package squalor
import (
"errors"
"fmt"
"strings"
)
func combineErrors(errs ...error) error {
var messages []string
for _, err := range errs {
messages = append(messages, err.Error())
}
return fmt.Errorf(strings.Join(messages, "\n"))
}
func recoveryToError(r interface{}) error {
var err error
switch value := r.(type) {
case string:
err = errors.New(value)
case error:
err = value
default:
err = errors.New("unknown panic")
}
return err
}
// Rune-based string truncation with ellipsis with reasonable support for multibyte characters.
func truncate(s string, n int) string {
if n <= 0 {
return ""
}
if len(s) < n {
return s
}
offsets := make([]int, 0, n+1)
for i := range s {
offsets = append(offsets, i)
if len(offsets) == n+1 {
break
}
}
if len(offsets) <= n {
return s
}
return s[:offsets[n-1]] + "…"
}