# Buggy code
def decode(encoded_uri):
    """Decodes percent-encoded characters in a URI or query string.

    This function models the behavior of `urllib.parse.unquote_plus`,
    albeit in a faster, more straightforward manner.

    Args:
        encoded_uri (str): An encoded URI (full or partial).

    Returns:
        str: A decoded URL. If the URL contains escaped non-ASCII
        characters, UTF-8 is assumed per RFC 3986.

    """

    decoded_uri = encoded_uri

    # PERF(kgriffs): Don't take the time to instantiate a new
    # string unless we have to.
    if "+" in decoded_uri:
        decoded_uri = decoded_uri.replace("+", " ")

    # Short-circuit if we can
    if "%" not in decoded_uri:
        return decoded_uri

    # NOTE(kgriffs): Clients should never submit a URI that has
    # unescaped non-ASCII chars in them, but just in case they
    # do, let's encode into a non-lossy format.
    decoded_uri = decoded_uri.encode("utf-8")

    # PERF(kgriffs): This was found to be faster than using
    # a regex sub call or list comprehension with a join.
    tokens = decoded_uri.split(b"%")
    decoded_uri = tokens[0]
    for token in tokens[1:]:
        decoded_uri += _HEX_TO_BYTE[token[:2]] + token[2:]

    # Convert back to str
    return decoded_uri.decode("utf-8", "replace")


# Traceback (KeyError)
Traceback (most recent call last):
File "/opt/ads/venv/local/lib/python2.7/site-packages/falcon/api.py", line 154, in __call__
req = self._request_type(env, options=self.req_options)
File "/opt/ads/venv/local/lib/python2.7/site-packages/falcon/request.py", line 237, in __init__
keep_blank_qs_values=self.options.keep_blank_qs_values,
File "/opt/ads/venv/local/lib/python2.7/site-packages/falcon/util/uri.py", line 327, in parse_query_string
params[k] = decode(v)
File "/opt/ads/venv/local/lib/python2.7/site-packages/falcon/util/uri.py", line 181, in decode
char, byte = _HEX_TO_BYTE[token[:2]]
KeyError: ''

# Fixed code
def decode(encoded_uri):
    """Decodes percent-encoded characters in a URI or query string.

    This function models the behavior of `urllib.parse.unquote_plus`,
    albeit in a faster, more straightforward manner.

    Args:
        encoded_uri (str): An encoded URI (full or partial).

    Returns:
        str: A decoded URL. If the URL contains escaped non-ASCII
        characters, UTF-8 is assumed per RFC 3986.

    """

    decoded_uri = encoded_uri

    # PERF(kgriffs): Don't take the time to instantiate a new
    # string unless we have to.
    if "+" in decoded_uri:
        decoded_uri = decoded_uri.replace("+", " ")

    # Short-circuit if we can
    if "%" not in decoded_uri:
        return decoded_uri

    # NOTE(kgriffs): Clients should never submit a URI that has
    # unescaped non-ASCII chars in them, but just in case they
    # do, let's encode into a non-lossy format.
    decoded_uri = decoded_uri.encode("utf-8")

    # PERF(kgriffs): This was found to be faster than using
    # a regex sub call or list comprehension with a join.
    tokens = decoded_uri.split(b"%")
    decoded_uri = tokens[0]
    for token in tokens[1:]:
        token_partial = token[:2]
        if token_partial in _HEX_TO_BYTE:
            decoded_uri += _HEX_TO_BYTE[token_partial] + token[2:]
        else:
            # malformed percentage like "x=%" or "y=%+"
            decoded_uri += b"%" + token

    # Convert back to str
    return decoded_uri.decode("utf-8", "replace")