Xray-core/common/serial/string.go

36 lines
608 B
Go
Raw Normal View History

2020-11-25 13:01:53 +02:00
package serial
import (
"fmt"
"strings"
)
// ToString serialize an arbitrary value into string.
func ToString(v interface{}) string {
if v == nil {
2020-12-24 21:45:35 +02:00
return ""
2020-11-25 13:01:53 +02:00
}
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)
}
}
// Concat concatenates all input into a single string.
func Concat(v ...interface{}) string {
builder := strings.Builder{}
for _, value := range v {
builder.WriteString(ToString(value))
}
return builder.String()
}