// DetectEncoding returns best guess of encoding of given content.
196091
func DetectEncoding(content []byte) (string, error) {
	if utf8.Valid(content) {
		log.Trace("Detected encoding: UTF-8 (fast)")
		return "UTF-8", nil
	}

	result, err := chardet.NewTextDetector().DetectBest(content)
	if result.Charset != "UTF-8" && len(setting.Repository.AnsiCharset) > 0 {
		log.Trace("Using default AnsiCharset: %s", setting.Repository.AnsiCharset)
		return setting.Repository.AnsiCharset, err
	}

	log.Trace("Detected encoding: %s", result.Charset)
	return result.Charset, err
}
909884
func Encode(content string) (barcode.Barcode, error) {
	checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
	if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
		return nil, fmt.Errorf("can not encode \"%s\"", content)
	}
	resBits := new(utils.BitList)
	for i, r := range content {
		if i > 0 {
			resBits.AddBit(false)
		}
		resBits.AddBit(encodingTable[r]...)
	}
	return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
}
121020
func NewEncoding(encoder string) *Encoding {
	if len(encoder) != 64 {
		panic("encoding alphabet is not 64-bytes long")
	}

	e := new(Encoding)
	e.padChar = StdPadding
	copy(e.encode[:], encoder)

	for i := 0; i < len(e.decodeMap); i++ {
		e.decodeMap[i] = 0xFF
	}
	for i := 0; i < len(encoder); i++ {
		e.decodeMap[encoder[i]] = byte(i)
	}
	return e
}
754116
func (res *Response) DetectCharset() {
	// Detect via BOM / HTML meta tag
	_, cs1, ok1 := charset.DetermineEncoding(res.Body, res.MediaType)

	// Detect via ICU
	cs2, ok2, html := "", false, false
	var det *chardet.Detector
	if strings.Contains(res.MediaType, "html") || true {
		det = chardet.NewHtmlDetector()
		html = true
	} else {
		det = chardet.NewTextDetector()
	}
	r, err := det.DetectAll(res.Body)
	if err == nil && len(r) > 0 {
		cs2 = strings.ToLower(r[0].Charset)
		ok2 = r[0].Confidence > 50
	}

	// Prefer charset if HTML, otherwise ICU
	if !ok2 && (ok1 || html) {
		res.Charset = cs1
	} else {
		res.Charset = cs2
	}

	// fmt.Printf("Detected charset via go.net/html/charset: %s (%t)\n", cs1, ok1)
	// fmt.Printf("Detected charset via saintfish/chardet:   %s (%d)\n", cs2, r[0].Confidence)
}
1095297
func (ns *Namespace) Base64Decode(content interface{}) (string, error) {
	conv, err := cast.ToStringE(content)
	if err != nil {
		return "", err
	}

	dec, err := base64.StdEncoding.DecodeString(conv)
	return string(dec), err
}
592018
func (s *S3ObjectMetadata) SetContentEncoding(v string) *S3ObjectMetadata {
	s.ContentEncoding = &v
	return s
}
685783
func Detect(filename string) Type {
	switch {
	case IsMarkdownFile(filename):
		return MARKDOWN
	case IsOrgModeFile(filename):
		return ORG_MODE
	case IsIPythonNotebook(filename):
		return IPYTHON_NOTEBOOK
	default:
		return UNRECOGNIZED
	}
}
1051101
func (c *Charmap) NewEncoder() *encoding.Encoder {
	c.Init()
	return &encoding.Encoder{
		Transformer: &cmapEncoder{
			bytes:   c.bytes,
			replace: c.ReplacementChar,
		},
	}
}
745188
func (r *Response) TransferEncoding() string {
	for _, h := range r.Headers {
		if strings.EqualFold(h.Key, "Transfer-Encoding") {
			switch h.Value {
			case "identity", "chunked":
				return h.Value
			}
		}
	}
	return "identity"
}
281201
func (r *RepositoryLicense) GetEncoding() string {
	if r == nil || r.Encoding == nil {
		return ""
	}
	return *r.Encoding
}
1174868
func NewEncoder(w io.Writer) *Encoder {
	return &Encoder{w, defaultDelimiter, defaultEscape, false}
}
267640
func checkEncoding(cfg node, report *Report) {
	for _, f := range cfg.Child("write_files").children {
		e := f.Child("encoding")
		if !e.IsValid() {
			continue
		}

		c := f.Child("content")
		if _, err := config.DecodeContent(c.String(), e.String()); err != nil {
			report.Error(c.line, fmt.Sprintf("content cannot be decoded as %q", e.String()))
		}
	}
}
35940
func detect(code []byte, languages []string, end *pcre.Regexp) (string, error) {
	languageChan := make(chan string)
	type result struct {
		lang  string
		score float64
		err   error
	}
	resultChan := make(chan result)

	go func() {
		for _, lang := range languages {
			languageChan <- lang
		}
		close(languageChan)
	}()

	var wg sync.WaitGroup

	for i := 0; i < Workers; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for l := range languageChan {
				h, err := makeHighlighter(l, code)
				if err != nil {
					resultChan <- result{l, 0, err}
					continue
				}
				if _, err := h.highlight(h.lang.Contains, 0, end); err != nil {
					resultChan <- result{l, 0, err}
					continue
				}

				result := result{l, 0, err}

				for _, h := range h.highlights {
					result.score += h.contains.Relevance
				}

				resultChan <- result
			}
		}()
	}

	go func() {
		wg.Wait()
		close(resultChan)
	}()

	var err error
	var bestLang string
	var bestLangVal float64
	for res := range resultChan {
		if res.err != nil {
			err = res.err
			continue
		}

		if res.score > bestLangVal {
			bestLang = res.lang
			bestLangVal = res.score
		}
	}

	return bestLang, err
}
564976
func newEncoding(encoder string) *encoding {
	e := new(encoding)
	e.enc = encoder
	for i := 0; i < len(e.dec); i++ {
		e.dec[i] = 0xff
	}
	for i := 0; i < len(encoder); i++ {
		e.dec[encoder[i]] = byte(i)
	}
	return e
}
1163143
func SetContentEncoding(contentEncoding string) RequestFunc {
	return func(ctx context.Context, pub *amqp.Publishing, _ *amqp.Delivery) context.Context {
		pub.ContentEncoding = contentEncoding
		return ctx
	}
}
612389
func (e *Encoder) UseCompactEncoding(flag bool) *Encoder {
	e.useCompact = flag
	return e
}
1004141
func (enc *Encoding) IsValidEncodingLength(n int) bool {
	// Fast path!
	if n == enc.baseXBlockLen {
		return true
	}
	f := func(n int) int {
		return int(math.Floor(float64(n) * enc.logOfBase / float64(8)))
	}
	if f(n) == f(n-1) {
		return false
	}
	return true
}