import re


def makeFeaClassName(name, existingClassNames=None):
    """Make a glyph class name which is legal to use in feature text.

    Ensures the name only includes characters in "A-Za-z0-9._", and
    isn't already defined.
    """
    name = re.sub(r"[^A-Za-z0-9._]", r"", name)
    if existingClassNames is None:
        return name
    i = 1
    origName = name
    while name in existingClassNames:
        name = "%s_%d" % (origName, i)
        i += 1
    return name