Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ mutable package state.

`Kind` is `text`, `binary`, or `unknown`. `Format` and `MIME` describe the
physical content. `Encoding` is set for accepted UTF-8, UTF-16LE, or UTF-16BE
text and never appears as a MIME charset parameter. Compare `Format` against
the exported `Format*` constants rather than string literals.
text and never appears as a MIME charset parameter. Compare `Format` and
`Encoding` against the exported `Format*` and `Encoding*` constants rather
than string literals.

The format registry contains:

Expand Down
14 changes: 14 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ func ExampleDetectPrefix() {
// Output:
// text need-more
}

func ExampleResult_Encoding() {
result := magic.Detect([]byte("\xff\xfeh\x00i\x00"))

switch result.Encoding {
case magic.EncodingUTF16LE, magic.EncodingUTF16BE:
fmt.Println("decode UTF-16 before processing text")
case magic.EncodingUTF8:
fmt.Println("process text directly")
}

// Output:
// decode UTF-16 before processing text
}
8 changes: 4 additions & 4 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestJSONDetection(t *testing.T) {
Kind: KindText,
MIME: mimeJSON,
Format: FormatJSON,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
})
})
}
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestInvalidJSONRetainsExistingClassification(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
})
})
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestJSONPrefixDetection(t *testing.T) {
Kind: KindText,
MIME: mimeJSON,
Format: FormatJSON,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
Reason: ReasonNeedMore,
})
})
Expand Down Expand Up @@ -199,7 +199,7 @@ func TestInvalidJSONPrefixRetainsExistingClassification(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
Reason: ReasonNeedMore,
})
})
Expand Down
9 changes: 6 additions & 3 deletions magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ const (
mimePE = "application/vnd.microsoft.portable-executable"
mimeWASM = "application/wasm"
mimeAR = "application/x-archive"
)

encodingUTF8 = "utf-8"
encodingUTF16LE = "utf-16le"
encodingUTF16BE = "utf-16be"
// Encoding values reported in Result.Encoding.
const (
EncodingUTF8 = "utf-8"
EncodingUTF16LE = "utf-16le"
EncodingUTF16BE = "utf-16be"
)

// Detect classifies data as the complete content of a file.
Expand Down
4 changes: 2 additions & 2 deletions magic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestDetectPrefix(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
Reason: ReasonNeedMore,
},
},
Expand Down Expand Up @@ -125,7 +125,7 @@ func TestDetectDoesNotRetainInput(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
})
}

Expand Down
2 changes: 1 addition & 1 deletion signatures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func TestTextFormatRegistryAndPrecedence(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := Detect([]byte(test.input))
if got.Kind != KindText || got.Encoding != encodingUTF8 ||
if got.Kind != KindText || got.Encoding != EncodingUTF8 ||
got.Format != test.format || got.MIME != test.mime {
t.Fatalf("Detect() = %#v, want text %s %s", got, test.format, test.mime)
}
Expand Down
8 changes: 4 additions & 4 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func classifyText(data []byte) Result {
if containsDisallowedControlUTF8(data) {
return Result{Kind: KindBinary}
}
return Result{Kind: KindText, Encoding: encodingUTF8}
return Result{Kind: KindText, Encoding: EncodingUTF8}
}

func classifyUTF8BOM(data []byte) Result {
Expand All @@ -44,7 +44,7 @@ func classifyUTF8BOM(data []byte) Result {
if containsDisallowedControlUTF8(data) {
return Result{Kind: KindBinary}
}
return Result{Kind: KindText, Encoding: encodingUTF8}
return Result{Kind: KindText, Encoding: EncodingUTF8}
}

func classifyUTF16BOM(data []byte, littleEndian bool) Result {
Expand All @@ -56,9 +56,9 @@ func classifyUTF16BOM(data []byte, littleEndian bool) Result {
return Result{Kind: KindBinary}
}

encoding := encodingUTF16BE
encoding := EncodingUTF16BE
if littleEndian {
encoding = encodingUTF16LE
encoding = EncodingUTF16LE
}
return Result{Kind: KindText, Encoding: encoding}
}
Expand Down
14 changes: 7 additions & 7 deletions text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestTextDecisionTable(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
},
},
{
Expand All @@ -36,7 +36,7 @@ func TestTextDecisionTable(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF16LE,
Encoding: EncodingUTF16LE,
},
},
{
Expand All @@ -46,7 +46,7 @@ func TestTextDecisionTable(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF16BE,
Encoding: EncodingUTF16BE,
},
},
{
Expand Down Expand Up @@ -81,7 +81,7 @@ func TestTextDecisionTable(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
},
},
{
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestUTF16Validation(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF16LE,
Encoding: EncodingUTF16LE,
},
},
{
Expand All @@ -129,7 +129,7 @@ func TestUTF16Validation(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF16LE,
Encoding: EncodingUTF16LE,
},
},
{
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestPlainText(t *testing.T) {
Kind: KindText,
MIME: mimeText,
Format: FormatText,
Encoding: encodingUTF8,
Encoding: EncodingUTF8,
})
})
}
Expand Down