1package stringext
2
3import (
4 "encoding/base64"
5 "strings"
6
7 "golang.org/x/text/cases"
8 "golang.org/x/text/language"
9)
10
11func Capitalize(text string) string {
12 return cases.Title(language.English, cases.Compact).String(text)
13}
14
15// NormalizeSpace normalizes whitespace in the given content string.
16// It replaces Windows-style line endings with Unix-style line endings,
17// converts tabs to four spaces, and trims leading and trailing whitespace.
18func NormalizeSpace(content string) string {
19 content = strings.ReplaceAll(content, "\r\n", "\n")
20 content = strings.ReplaceAll(content, "\t", " ")
21 content = strings.TrimSpace(content)
22 return content
23}
24
25// IsValidBase64 reports whether s is canonical base64 under standard
26// encoding (RFC 4648). It requires that s round-trips through
27// decode/encode unchanged — rejecting whitespace, missing padding,
28// and other leniencies that DecodeString alone would accept.
29func IsValidBase64(s string) bool {
30 if s == "" {
31 return false
32 }
33 decoded, err := base64.StdEncoding.DecodeString(s)
34 if err != nil {
35 return false
36 }
37 // Round-trip check rejects whitespace, missing padding, and other
38 // leniencies that DecodeString silently accepts.
39 return base64.StdEncoding.EncodeToString(decoded) == s
40}