96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package utils
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var MARKDOWN_ESC []string = []string{"_", "*", "[", "]", "(", ")", "~", "`", ">", "#", "+", "-", "=", "|", "{", "}", ".", "!"}
|
|
|
|
func WriteMDv2Text( sb *strings.Builder, t string ) {
|
|
for _, c := range MARKDOWN_ESC {
|
|
t = strings.Replace( t, c, "\\" + c, -1 )
|
|
}
|
|
|
|
sb.WriteString( t )
|
|
}
|
|
|
|
func MDv2Text( t string ) string {
|
|
sb := strings.Builder{}
|
|
|
|
for _, c := range MARKDOWN_ESC {
|
|
t = strings.Replace( t, c, "\\" + c, -1 )
|
|
}
|
|
|
|
sb.WriteString( t )
|
|
return sb.String()
|
|
}
|
|
|
|
func ToPower( t string ) string {
|
|
for s, r := range POWER_NUMBERS {
|
|
t = strings.ReplaceAll( t, s, r )
|
|
}
|
|
return t
|
|
}
|
|
|
|
func ReadBOM( buff *bytes.Buffer ) {
|
|
b, _ := buff.ReadByte()
|
|
if b != 0xef {
|
|
buff.UnreadByte()
|
|
return
|
|
}
|
|
b, _ = buff.ReadByte()
|
|
if b != 0xbb {
|
|
buff.UnreadByte()
|
|
buff.UnreadByte()
|
|
return
|
|
}
|
|
b, _ = buff.ReadByte()
|
|
if b != 0xbf {
|
|
buff.UnreadByte()
|
|
buff.UnreadByte()
|
|
buff.UnreadByte()
|
|
}
|
|
}
|
|
|
|
func TryGetEnv[T any]( name string, fallback T ) T {
|
|
v := os.Getenv( name )
|
|
|
|
if v != "" {
|
|
switch any( fallback ).(type) {
|
|
case uint64:
|
|
p, err := strconv.ParseUint( v, 10, 64 )
|
|
if err == nil {
|
|
return any( uint64( p ) ).(T)
|
|
}
|
|
case uint32:
|
|
p, err := strconv.ParseUint( v, 10, 32 )
|
|
if err == nil {
|
|
return any( uint32( p ) ).(T)
|
|
}
|
|
case int:
|
|
p, err := strconv.ParseInt( v, 10, 32 )
|
|
if err == nil {
|
|
return any( int( p ) ).(T)
|
|
}
|
|
case float64:
|
|
p, err := strconv.ParseFloat( v, 64 )
|
|
if err == nil {
|
|
return any( float64( p ) ).(T)
|
|
}
|
|
case float32:
|
|
p, err := strconv.ParseFloat( v, 32 )
|
|
if err == nil {
|
|
return any( float32( p ) ).(T)
|
|
}
|
|
default:
|
|
return any( v ).(T)
|
|
}
|
|
|
|
}
|
|
|
|
return fallback
|
|
}
|