#!/usr/bin/python # ApiCodeFilter.py # # Routines for filtering source code for output purposes. import re import sys # Expand tab characters into spaces def expandtabs(code, tabsize = 2): tmp = '' for i in code.splitlines(): tmp += i.expandtabs(tabsize) + '\n' return ''.join(tmp) # Trim extraneous trailing whitespace def trim(code): tmp = '' for i in code.splitlines(): tmp += i.rstrip() + '\n' return ''.join(tmp) # Fold repetitive if blocks # # if (foo) # doo_a(); # if (foo) # doo_b(); # # -> # # if (foo) # { # doo_a(); # doo_b(); # } # # NOTE - this logic is only correct for the situation # that foo is invariant for the scope of the # if blocks. Perhaps we ought to tighten-up # this logic. def foldIfs(code): tmp = code.splitlines() i = 0 while ii+2: c = [ tmp[i], '%s{'%(' '*indent) ] k = i+1 while k0: i = i - 1 else: i = i + 1 return '\n'.join(tmp) + '\n' # Fold consecutive empty lines def foldEmptyLines(code): tmp = code.splitlines() i = 0 while (i+1) # # case c: # default: { ... } # def foldRedundantCase(code): tmp = code.splitlines() i = 0 while (i+1)