def _remove_dot_segments(path):
    """
    Remove . and .. segments from path.

    Implements 5.2.4. Remove Dot Segments.
    """
    input = path
    output = []
    while input:
        if input.startswith('./'):
            input = input[2:]
        elif input.startswith('../'):
            input = input[3:]
        elif input.startswith('/./'):
            input = input[2:]
        elif input == '/.':
            input = '/'
        elif input.startswith('/../'):
            input = input[3:]
            if output:
                output.pop()
        elif input == '/..':
            input = '/'
            if output:
                output.pop()
        elif input == '.' or input == '..':
            input = ''
        else:
            start = 1 if input.startswith('/') else 0
            i = input.find('/', start)
            if i > -1:
                output.append(input[:i])
                input = input[i:]
            else:
                output += input
                input = ''
    return ''.join(output)
