import re


def to_snakecase(string):
    """
    Converts the given string to snake-case.

    >>> to_snakecase('HelloWorld')
    'hello_world'
    >>> to_snakecase('__Init__File__')
    '__init_file__'
    >>> to_snakecase('')
    ''
    >>> to_snakecase('already_snake_case')
    'already_snake_case'
    >>> to_snakecase('   string  ')
    '___string__'
    >>> to_snakecase('ABCde.F.G..H..IH')
    'a_b_cde.f.g..h..i_h'

    :param string: The string to convert.
    :return:       The snake-cased string.
    """
    string = re.sub('(\s)',
                    lambda match: '_',
                    string)
    string = re.sub('^(_*)([^_])',
                    lambda match: match.group(1) + match.group(2).lower(),
                    string)
    string = re.sub('(\w*)([.]+)([A-Z])',
                    lambda match: (match.group(1) + match.group(2) +
                                   match.group(3).lower()),
                    string)
    string = re.sub('(?<=[^_])_+([^_])',
                    lambda match: '_' + match.group(1).lower(),
                    string)
    return re.sub('[A-Z]',
                  lambda match: '_' + match.group(0).lower(),
                  string)