Skip to content

Commit

Permalink
SedRegex: allow matching text with the trailing suffix missing
Browse files Browse the repository at this point in the history
Closes #91

Also add test cases for issues described in #59 - text after the trailing separator should be ignored unless its a regex flag.
  • Loading branch information
jlu5 committed Mar 23, 2020
1 parent ab22cc9 commit 866875e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
4 changes: 3 additions & 1 deletion SedRegex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@

from . import config
from . import plugin
from imp import reload
from . import constants
from importlib import reload

reload(constants)
reload(config)
reload(plugin)

Expand Down
26 changes: 26 additions & 0 deletions SedRegex/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env python3

import re

SED_REGEX = re.compile(
# This part matches an optional nick followed by ":" or ",", used to direct replacement
# at a particular user.
r"^(?:(?P<nick>.+?)[:,] )?"

# Match and save the delimiter (any one symbol) as a named group
r"s(?P<delim>[^\w\s])"

# Match the pattern to replace, which can be any string up to the first instance of the delimiter
r"(?P<pattern>(?:(?!(?P=delim)).)*)(?P=delim)"

# Ditto with the replacement
r"(?P<replacement>(?:(?!(?P=delim)).)*)"

# Optional final delimiter plus flags at the end
r"(?:(?P=delim)(?P<flags>[a-z]*))?"
)

if __name__ == '__main__':
print("This is the full regex used by the plugin; paste it into your favourite regex tester "
"for debugging:")
print(SED_REGEX)
5 changes: 2 additions & 3 deletions SedRegex/plugin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
###
# Copyright (c) 2015, Michael Daniel Telatynski <postmaster@webdevguru.co.uk>
# Copyright (c) 2015-2019, James Lu <james@overdrivenetworks.com>
# Copyright (c) 2015-2020, James Lu <james@overdrivenetworks.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -52,8 +52,7 @@
'supports Python 2, consult the python2-legacy branch at '
'https://github.com/jlu5/SupyPlugins/tree/python2-legacy')

SED_REGEX = re.compile(r"^(?:(?P<nick>.+?)[:,] )?s(?P<delim>[^\w\s])(?P<pattern>.*?)(?P=delim)"
r"(?P<replacement>.*?)(?P=delim)(?P<flags>[a-z]*)$")
from .constants import *

# Replace newlines and friends with things like literal "\n" (backslash and "n")
axe_spaces = utils.str.MultipleReplacer({'\n': '\\n', '\t': '\\t', '\r': '\\r'})
Expand Down
32 changes: 31 additions & 1 deletion SedRegex/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###
# Copyright (c) 2017-2019, James Lu <james@overdrivenetworks.com>
# Copyright (c) 2017-2020, James Lu <james@overdrivenetworks.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -181,6 +181,36 @@ def testReDoSTimeout(self):
m = self.getMsg(' ', timeout=1)
self.assertIn('timed out', str(m))

def testMissingTrailingSeparator(self):
# Allow the plugin to work if you miss the trailing /
self.feedMsg('hello world')
self.feedMsg('s/world/everyone')
m = self.getMsg(' ')
self.assertIn('hello everyone', str(m))

# Make sure it works if there's a space in the replacement
self.feedMsg('hello world')
self.feedMsg('s@world@how are you')
m = self.getMsg(' ')
self.assertIn('hello how are you', str(m))

# Ditto with a space in the original text
self.feedMsg("foo bar @ baz")
self.feedMsg('s/bar @/and')
m = self.getMsg(' ')
self.assertIn('foo and baz', str(m))

def testIgnoreTextAfterTrailingSeparator(self):
# https://github.com/jlu5/SupyPlugins/issues/59
self.feedMsg('see you ltaer')
self.feedMsg('s/ltaer/later/ this text will be ignored')
m = self.getMsg(' ')
self.assertIn('see you later', str(m))

self.feedMsg('s/LTAER/later, bye/i <extra text>')
m = self.getMsg(' ')
self.assertIn('see you later, bye', str(m))

# TODO: test ignores

# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:

0 comments on commit 866875e

Please sign in to comment.