def applydiff(tokens, deltas):
    """tokens can be a string or a list of strings.  deltas is [Delta,...]. Delta.text is actually a tokenlist of the same type as tokens (string or list of string).  If tokens & tokenslist are strings, must be unicode for the offsets to match what JS produces.    (If they're lists, it doesn't matter; the offsets are relative to the lists, not internal to the strings)."""
    sizechange = 0
    for a, b, replace in deltas:
        tokens = tokens[:a + sizechange] + replace + tokens[b + sizechange:]
        sizechange += len(replace) - (b - a)
    return tokens
