Skip to content

Commit 708845c

Browse files
committed
Apply ireplace patch to master
1 parent 2315591 commit 708845c

1 file changed

Lines changed: 6 additions & 13 deletions

File tree

cloudbot/util/formatting.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,19 +173,12 @@ def ireplace(text, old, new, count=None):
173173
old replaced by new. If the optional argument count is given, only the first count
174174
occurrences are replaced.
175175
"""
176-
idx = 0
177-
num = 0
178-
while idx < len(text):
179-
index_l = text.lower().find(old.lower(), idx)
180-
if index_l == -1:
181-
return text
182-
183-
text = text[:index_l] + new + text[index_l + len(old):]
184-
idx = index_l + len(old)
185-
num += 1
186-
if count and num >= count:
187-
break
188-
return text
176+
pattern = re.compile(re.escape(old), re.IGNORECASE)
177+
178+
if count:
179+
return pattern.sub(new, text, count=count)
180+
else:
181+
return pattern.sub(new, text)
189182

190183

191184
def multi_replace(text, word_dic):

0 commit comments

Comments
 (0)