@@ -47,8 +47,19 @@ const (
4747 UTF32LittleEndian
4848)
4949
50- // detectEncodingBytes is the internal implementation that works directly on []byte.
51- func detectEncodingBytes (b []byte ) Encoding {
50+ // DetectEncoding inspects the initial bytes of a string or byte slice (T)
51+ // and returns the detected text encoding based on the presence of known BOMs (Byte Order Marks).
52+ // If no known BOM is found, it returns Unknown.
53+ //
54+ // Supported encodings:
55+ // - UTF-8 (BOM: 0xef 0xbb 0xbf)
56+ // - UTF-16 Big Endian (BOM: 0xfe 0xff)
57+ // - UTF-16 Little Endian (BOM: 0xff 0xfe)
58+ // - UTF-32 Big Endian (BOM: 0x00 0x00 0xfe 0xff)
59+ // - UTF-32 Little Endian (BOM: 0xff 0xfe 0x00 0x00)
60+ func DetectEncoding [T ~ string | ~ []byte ](input T ) Encoding {
61+ b := []byte (input )
62+
5263 if len (b ) < 2 {
5364 return Unknown
5465 }
@@ -78,20 +89,6 @@ func detectEncodingBytes(b []byte) Encoding {
7889 return Unknown
7990}
8091
81- // DetectEncoding inspects the initial bytes of a string or byte slice (T)
82- // and returns the detected text encoding based on the presence of known BOMs (Byte Order Marks).
83- // If no known BOM is found, it returns Unknown.
84- //
85- // Supported encodings:
86- // - UTF-8 (BOM: 0xef 0xbb 0xbf)
87- // - UTF-16 Big Endian (BOM: 0xfe 0xff)
88- // - UTF-16 Little Endian (BOM: 0xff 0xfe)
89- // - UTF-32 Big Endian (BOM: 0x00 0x00 0xfe 0xff)
90- // - UTF-32 Little Endian (BOM: 0xff 0xfe 0x00 0x00)
91- func DetectEncoding [T ~ string | ~ []byte ](input T ) Encoding {
92- return detectEncodingBytes ([]byte (input ))
93- }
94-
9592// AnyOf reports whether the Encoding value equals any of the given Encoding values.
9693// It returns true if a match is found, otherwise false.
9794func (e Encoding ) AnyOf (es ... Encoding ) bool {
@@ -158,7 +155,7 @@ func (e Encoding) Bytes() []byte {
158155// Supports string or []byte inputs and returns the same type without the BOM.
159156func Trim [T ~ string | ~ []byte ](input T ) (T , Encoding ) {
160157 b := []byte (input )
161- enc := detectEncodingBytes (b )
158+ enc := DetectEncoding (b )
162159
163160 if enc == Unknown {
164161 return input , enc
@@ -177,7 +174,7 @@ func Prepend[T ~string | ~[]byte](input T, enc Encoding) T {
177174
178175 b := []byte (input )
179176
180- if detectEncodingBytes (b ) != Unknown {
177+ if DetectEncoding (b ) != Unknown {
181178 return input
182179 }
183180
0 commit comments