v2ray-core/common/serial/string.go

41 lines
685 B
Go
Raw Normal View History

2016-12-04 08:10:47 +00:00
package serial
import (
"fmt"
"strings"
)
2017-02-13 21:39:55 +00:00
// ToString serialize an abitrary value into string.
2016-12-04 08:10:47 +00:00
func ToString(v interface{}) string {
if v == nil {
return " "
}
switch value := v.(type) {
case string:
return value
case *string:
return *value
case fmt.Stringer:
return value.String()
case error:
return value.Error()
default:
return fmt.Sprintf("%+v", value)
}
}
func Concat(v ...interface{}) string {
2018-10-01 10:42:14 +00:00
builder := strings.Builder{}
for _, value := range v {
builder.WriteString(ToString(value))
2016-12-04 08:10:47 +00:00
}
2018-10-01 10:42:14 +00:00
return builder.String()
2016-12-04 08:10:47 +00:00
}
2016-12-06 10:03:42 +00:00
2016-12-09 12:17:34 +00:00
func WriteString(s string) func([]byte) (int, error) {
2016-12-09 11:08:25 +00:00
return func(b []byte) (int, error) {
return copy(b, []byte(s)), nil
2016-12-06 10:03:42 +00:00
}
}