func()

in internal/mode/advanced/indexer/encoding.go [55:80]


func (e *Encoder) encodeBytes(b []byte) (string, error) {
	if len(b) == 0 {
		return "", nil
	}

	matches, err := e.detector.GuessCharset(b)
	if err != nil {
		return "", fmt.Errorf("Couldn't guess charset: %w", err)
	}

	// Try encoding for each match, returning the first that succeeds
	for _, match := range matches {
		utf8, err := e.converter.ConvertToUtf8(b, match.Charset)
		if err == nil {
			return string(utf8), nil
		}
	}

	// `detector.GuessCharset` may return err == nil && len(matches) == 0
	bestGuess := "unknown"
	if len(matches) > 0 {
		bestGuess = matches[0].Charset
	}

	return "", fmt.Errorf("Failed to convert from %s to UTF-8", bestGuess)
}