Add IndexError check to $firstwords function

This commit is contained in:
Bob Swift
2020-02-27 16:30:00 -07:00
committed by Philipp Wolfer
parent dd5be66728
commit becd3e0d7a
2 changed files with 8 additions and 3 deletions

View File

@@ -863,9 +863,12 @@ def func_firstwords(parser, text, length):
if len(text) <= length:
return text
else:
if text[length] == ' ':
return text[:length]
return text[:length].rsplit(' ', 1)[0]
try:
if text[length] == ' ':
return text[:length]
return text[:length].rsplit(' ', 1)[0]
except IndexError:
return ''
@script_function()

View File

@@ -456,6 +456,8 @@ class ScriptParserTest(PicardTestCase):
self.assertScriptResultEquals("$firstwords(Abc Def Ghi,0)", "")
self.assertScriptResultEquals("$firstwords(Abc Def Ghi,NaN)", "")
self.assertScriptResultEquals("$firstwords(Abc Def Ghi,)", "")
self.assertScriptResultEquals("$firstwords(Abc Def Ghi,-2)", "Abc Def")
self.assertScriptResultEquals("$firstwords(Abc Def Ghi,-50)", "")
def test_cmd_startswith(self):
self.assertScriptResultEquals("$startswith(abc,a)", "1")