<commit_before>"""
Utility functions for decoding response bodies.
"""
from __future__ import absolute_import

import codecs
import collections
import six
from io import BytesIO

import gzip
import zlib
import brotli

from typing import Union  # noqa


# We have a shared single-element cache for encoding and decoding.
# This is quite useful in practice, e.g.
# flow.request.content = flow.request.content.replace(b"foo", b"bar")
# does not require an .encode() call if content does not contain b"foo"
CachedDecode = collections.namedtuple("CachedDecode", "encoded encoding errors decoded")
_cache = CachedDecode(None, None, None, None)


def decode(encoded, encoding, errors='strict'):
    # type: (Union[str, bytes], str, str) -> Union[str, bytes]
    """
    Decode the given input object

    Returns:
        The decoded value

    Raises:
        ValueError, if decoding fails.
    """
    if len(encoded) == 0:
        return encoded

    global _cache
    cached = (
        isinstance(encoded, bytes) and
        _cache.encoded == encoded and
        _cache.encoding == encoding and
        _cache.errors == errors
    )
    if cached:
        return _cache.decoded
    try:
        try:
            decoded = custom_decode[encoding](encoded)
        except KeyError:
            decoded = codecs.decode(encoded, encoding, errors)<commit_msg>Fix decoding when content-encoding: none<commit_after>"""
Utility functions for decoding response bodies.
"""
from __future__ import absolute_import

import codecs
import collections
import six
from io import BytesIO

import gzip
import zlib
import brotli

from typing import Union  # noqa


# We have a shared single-element cache for encoding and decoding.
# This is quite useful in practice, e.g.
# flow.request.content = flow.request.content.replace(b"foo", b"bar")
# does not require an .encode() call if content does not contain b"foo"
CachedDecode = collections.namedtuple("CachedDecode", "encoded encoding errors decoded")
_cache = CachedDecode(None, None, None, None)


def decode(encoded, encoding, errors='strict'):
    # type: (Union[str, bytes], str, str) -> Union[str, bytes]
    """
    Decode the given input object

    Returns:
        The decoded value

    Raises:
        ValueError, if decoding fails.
    """
    if len(encoded) == 0 or encoding == "none":
        return encoded

    global _cache
    cached = (
        isinstance(encoded, bytes) and
        _cache.encoded == encoded and
        _cache.encoding == encoding and
        _cache.errors == errors
    )
    if cached:
        return _cache.decoded
    try:
        try:
            decoded = custom_decode[encoding](encoded)
        except KeyError:
            decoded = codecs.decode(encoded, encoding, errors)
{'fraction_non_alphanumeric': 0.09008354522339267, 'fraction_numerical': 0.000726480203414457, 'mean_word_length': 3.038123167155425, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5587060', 'n_tokens_mistral': 772, 'n_tokens_neox': 751, 'n_words': 272}