Paste Code
Paste Blends
Paste Images
#!/usr/bin/env python

import os
import re

items = [
["mul_m4_m4m4", "mult_m4_m4m4", [0, 2, 1]],
["mul_m3_m3m4", "mult_m3_m3m4", [0, 2, 1]],
]

# create regular expressions
subs = []

sdetect = "("

for item in items:
old, new, shuffle = item

sfrom = r'([^A-Za-z_:])%s\s*\(' % old

if len(shuffle):
for s in shuffle:
sfrom += r'(.*)'

if s != shuffle[-1]:
sfrom += r','

sfrom += r'\)'

sto = r'\1%s(' % new

if len(shuffle):
for s in shuffle:
sto += r'\%d' % (s+2)

if s != shuffle[-1]:
sto += r','

sto += r')'

subs += [[sfrom, sto]]

if sdetect != "(":
sdetect += "|"
sdetect += old

sdetect += ")"
rdetect = re.compile(sdetect)

for i in range(0, len(subs)):
print(subs[i])
subs[i][0] = re.compile(subs[i][0])
#subs[i][1] = re.compile(subs[i][1])

# apply to files
files = []

for dirpath, dirnames, filenames in os.walk("source"):
for filename in filenames:
if filename.endswith(".c") or filename.endswith(".cpp") or filename.endswith(".h"):
files += [dirpath + os.sep + filename]

# default cache is too small, and there doesn't seem
# to be a way to compile patterns for sub()
re._MAXCACHE = len(items)*2 + 50

for file in files:
f = open(file, "r")
changed =False

lines = f.readlines()
newlines = []
for line in lines:
newline = line
if rdetect.search(line):
for sub in subs:
newline = sub[0].sub(sub[1], newline)
if line != newline:
changed = True
newlines += [newline]

f.close()

if changed:
print("modified " + file)
f = open(file, "w")
f.writelines(newlines)
f.close()
  1. #!/usr/bin/env python
  2.  
  3. import os
  4. import re
  5.  
  6. items = [
  7. ["mul_m4_m4m4", "mult_m4_m4m4", [0, 2, 1]],
  8. ["mul_m3_m3m4", "mult_m3_m3m4", [0, 2, 1]],
  9. ]
  10.  
  11. # create regular expressions
  12. subs = []
  13.  
  14. sdetect = "("
  15.  
  16. for item in items:
  17.         old, new, shuffle = item
  18.  
  19.         sfrom = r'([^A-Za-z_:])%s\s*\(' % old
  20.  
  21.         if len(shuffle):
  22.                 for s in shuffle:
  23.                         sfrom += r'(.*)'
  24.  
  25.                         if s != shuffle[-1]:
  26.                                 sfrom += r','
  27.                
  28.                 sfrom += r'\)'
  29.        
  30.         sto = r'\1%s(' % new
  31.  
  32.         if len(shuffle):
  33.                 for s in shuffle:
  34.                         sto += r'\%d' % (s+2)
  35.  
  36.                         if s != shuffle[-1]:
  37.                                 sto += r','
  38.  
  39.                 sto += r')'
  40.        
  41.         subs += [[sfrom, sto]]
  42.  
  43.         if sdetect != "(":
  44.                 sdetect += "|"
  45.         sdetect += old
  46.  
  47. sdetect += ")"
  48. rdetect = re.compile(sdetect)
  49.  
  50. for i in range(0, len(subs)):
  51.         print(subs[i])
  52.         subs[i][0] = re.compile(subs[i][0])
  53.         #subs[i][1] = re.compile(subs[i][1])
  54.  
  55. # apply to files
  56. files = []
  57.  
  58. for dirpath, dirnames, filenames in os.walk("source"):
  59.         for filename in filenames:
  60.                 if filename.endswith(".c") or filename.endswith(".cpp") or filename.endswith(".h"):
  61.                         files += [dirpath + os.sep + filename]
  62.  
  63. # default cache is too small, and there doesn't seem
  64. # to be a way to compile patterns for sub()
  65. re._MAXCACHE = len(items)*2 + 50
  66.  
  67. for file in files:
  68.         f = open(file, "r")
  69.         changed =False
  70.  
  71.         lines = f.readlines()
  72.         newlines = []
  73.         for line in lines:
  74.                 newline = line
  75.                 if rdetect.search(line):
  76.                         for sub in subs:
  77.                                 newline = sub[0].sub(sub[1], newline)
  78.                                 if line != newline:
  79.                                         changed = True
  80.                 newlines += [newline]
  81.        
  82.         f.close()
  83.  
  84.         if changed:
  85.                 print("modified " + file)
  86.                 f = open(file, "w")
  87.                 f.writelines(newlines)
  88.                 f.close()
  89.  
go to heaven