Last active
February 11, 2016 13:36
-
-
Save peterfpeterson/b1a04b53acdfd2b378cd to your computer and use it in GitHub Desktop.
regexp golf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import re | |
withargs_matchline = re.compile(r'.*boost::shared_ptr<(.+)>\(new\s+(\w+)\(.+\)\)') | |
withargs_expression = re.compile(r'(boost::shared_ptr<).+>.*\((.*new\s+\w+\().+(\)\))') | |
withoutargs_matchline = re.compile(r'.*boost::shared_ptr<(.+)>\(new (.+)\s*\)') | |
withoutargs_expression = re.compile(r'.*(boost::shared_ptr<).+>\((new .+)(\s*\)).*') | |
testing_matchline = re.compile(r'.*boost::shared_ptr<(.+)>\s+.+\(new (.+)\(') | |
testing_expression = re.compile(r'.*boost::shared_ptr<.+>\s+.+\s*(\(new\s+.+)\(.*(\)\)).*') | |
testingempty_matchline = re.compile(r'.*boost::shared_ptr<(.+)>\s+.+\(new (.+)\).*') | |
testingempty_expression = re.compile(r'.*boost::shared_ptr<.+>\s+.+\s*(\(new\s+.+\)).*') | |
def getExpression(input, debug): | |
matches = withargs_matchline.match(input) | |
if debug: | |
print "01a:", matches | |
if matches is not None: | |
return(matches, withargs_expression) | |
matches = withoutargs_matchline.match(input) | |
if debug: | |
print "01b:", matches | |
if matches is not None: | |
return (matches, withoutargs_expression) | |
matches = testing_matchline.match(input) | |
if debug: | |
print "01c:", matches | |
if matches is not None: | |
return (matches, testing_expression) | |
matches = testingempty_matchline.match(input) | |
if debug: | |
print "01d:", matches | |
if matches is not None: | |
return (matches, testingempty_expression) | |
return (None, None) | |
def fixline(input, debug=False): | |
if not "boost::shared_ptr" in input: | |
return input | |
if debug: | |
print "00 :", input | |
(matches, expression) = getExpression(input, debug) | |
if matches is None: | |
return input | |
groups = matches.groups() | |
if debug: | |
print "03 :", groups | |
if len(groups) == 2: | |
(oldclassname, newclassname) = groups | |
groups = expression.search(input).groups() | |
if debug: | |
print "04 :", groups | |
if len(groups) == 1: | |
newtext = input.replace(groups[0], | |
" = boost::make_shared<"+newclassname+'>()') | |
elif len(groups) == 2: | |
newtext = input.replace(groups[0], | |
" = boost::make_shared<"+newclassname+'>') | |
newtext = newtext.replace(groups[1], ')') | |
else: | |
newtext = input.replace(groups[0]+oldclassname, | |
"boost::make_shared<"+newclassname) | |
newtext = newtext.replace(groups[1], '') | |
newtext = newtext.replace(groups[2], ')') | |
if debug: | |
print "05 :", newtext | |
newtext = newtext.replace(r'((', r'(') | |
newtext = newtext.replace(r'))', r')') | |
if debug: | |
print "06 :", newtext | |
return newtext | |
else: | |
raise RuntimeError("Something weird with '%s'" % input) | |
return input | |
def fixfile(infilename): | |
outfilename = infilename+".tmp" | |
total_changed = 0 | |
#print outfilename | |
with open(infilename, 'r') as inhandle: | |
with open(outfilename, 'w') as outhandle: | |
for line in inhandle: | |
result = fixline(line) | |
if line != result: | |
total_changed += 1 | |
outhandle.write(result) | |
if total_changed > 0: | |
print "changed %d lines in '%s'" % ( total_changed, infilename) | |
os.rename(outfilename, infilename) | |
else: | |
os.remove(outfilename) | |
if False: # test cases | |
testcases = { | |
r'bob':'bob', | |
r'boost::shared_ptr<Widget>(new Widget(a,b,c...))': | |
r'boost::make_shared<Widget>(a,b,c...)', | |
r'horizontal->unit() = boost::shared_ptr<Kernel::Unit>(new Kernel::Units::Phi);': | |
r'horizontal->unit() = boost::make_shared<Kernel::Units::Phi>();', | |
r'boost::shared_ptr<QwtPlotCurve> curve(new QwtPlotCurve);': | |
r'boost::make_shared<QwtPlotCurve>()', | |
r'boost::shared_ptr<CompAssembly>(new CompAssembly("BankName"));': | |
r'boost::make_shared<CompAssembly>("BankName");', | |
r'boost::shared_ptr<Mantid::MantidVec> x1(new Mantid::MantidVec(xlen, 0.0));': | |
r'boost::shared_ptr<Mantid::MantidVec> x1 = boost::make_shared<Mantid::MantidVec>(xlen, 0.0);', | |
r'boost::shared_ptr<TestChannel> tChannel(new TestChannel);': | |
r'boost::shared_ptr<TestChannel> tChannel = boost::make_shared<TestChannel>();' | |
} | |
for key in testcases: | |
print "------------------------------" | |
result = fixline(key) | |
print bool(testcases[key] == result), key, "->", result | |
#print testcases[key] | |
#print result | |
else: | |
BANNED = ['Instantiator.h', 'XMLInstantiator.h'] | |
import sys | |
args = sys.argv[1:] | |
args = [item for item in args if not item in BANNED] | |
for filename in args: | |
fixfile( filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment