import re


def ConvertStringToFilename(name):
  """Converts an unicode string to a filesystem safe filename.

  For maximum compatibility we escape all chars which are not alphanumeric (in
  the unicode sense).

  Args:
   name: a unicode string that is part of a subject.

  Returns:
    A safe filename with escaped special chars.
  """
  return re.sub(
      r"\W", lambda x: "%%%02X" % ord(x.group(0)), name,
      flags=re.UNICODE).rstrip("/")