import struct


def _decode_str(reader):
    """
    Reads and decodes string.

    Parameters
    ----------
    reader : apptrace.Reader
        Trace reader object.

    Returns
    -------
    tuple
        a tuple containg number of read bytes and decoded value.
    """
    sz = 0
    val = ''
    sz, = struct.unpack('<B', reader.read(1))
    if sz == 0xFF:
        buf = struct.unpack('<2B', reader.read(2))
        sz = (buf[1] << 8) | buf[0]
    val, = struct.unpack('<%ds' % sz, reader.read(sz))
    val = val.decode("utf-8")
    if sz < 0xFF:
        return (sz + 1,val)  # one extra byte for length
    return (sz + 3,val)