[gtksourceview] win32/replace.py: Sync with GLib master



commit b245c5c5287ea801d05d4f4442046b3fad97570c
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Mon Mar 6 16:09:18 2017 +0800

    win32/replace.py: Sync with GLib master
    
    This will help in the .pc generation, as well as allowing a bit of clean
    ups.

 win32/replace.py |   13 ++++++++++---
 1 files changed, 10 insertions(+), 3 deletions(-)
---
diff --git a/win32/replace.py b/win32/replace.py
index 69ef417..a81bab9 100644
--- a/win32/replace.py
+++ b/win32/replace.py
@@ -21,12 +21,19 @@ valid_actions = ['remove-prefix',
                  'replace-str',
                  'remove-str']
 
-def replace(src, dest, instring, outstring):
+def replace_multi(src, dest, replace_items):
     with open(src, 'r') as s:
         with open(dest, 'w') as d:
             for line in s:
-                i = line.replace(instring, outstring)
-                d.write(i)
+                replace_dict = dict((re.escape(key), value) \
+                               for key, value in replace_items.items())
+                replace_pattern = re.compile("|".join(replace_dict.keys()))
+                d.write(replace_pattern.sub(lambda m: \
+                        replace_dict[re.escape(m.group(0))], line))
+
+def replace(src, dest, instring, outstring):
+    replace_item = {instring: outstring}
+    replace_multi(src, dest, replace_item)
 
 def check_required_args(args, params):
     for param in params:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]