def _ExpandTabs(text, column, tabsize, mark_tabs=False):
  """Expand tab characters in a string into spaces.

  Args:
    text: a string containing tab characters.
    column: the initial column for the first character in text
    tabsize: tab stops occur at columns that are multiples of tabsize
    mark_tabs: if true, leave a tab character as the first character
               of the expansion, so that the caller can find where
               the tabs were.

  Note that calling _ExpandTabs with mark_tabs=True is not idempotent.
  """
  expanded = ""
  while True:
    tabpos = text.find("\t")
    if tabpos < 0:
      break
    fillwidth = tabsize - (tabpos + column) % tabsize
    column += tabpos + fillwidth
    if mark_tabs:
      fill = "\t" + " " * (fillwidth - 1)
    else:
      fill = " " * fillwidth
    expanded += text[0:tabpos] + fill
    text = text[tabpos+1:]
  return expanded + text