def _rangelen(begin, end):
    """Returns length of the range specified by begin and end.

    As this is typically used to calculate the length of a buffer, it
    always returns a result >= 0.

    See functions like `_safeDoubleArray` and `safeLongArray`.
    """
    # We allow arguments like begin=0, end=-1 on purpose. This represents
    # an empty range; the callable library should do nothing in this case
    # (see RTC-31484).
    result = end - begin + 1
    if result < 0:
        return 0
    return result