Add llReplaceSubString calculation + tests

This commit is contained in:
Sei Lisa 2023-04-09 12:28:59 +02:00
parent e4c9627772
commit 5f1062d378
3 changed files with 83 additions and 0 deletions

View file

@ -1818,6 +1818,31 @@ def llPow(base, exp):
except ValueError: # should happen only with negative base and noninteger exponent
return Indet
def llReplaceSubString(source, search, replace, count):
source = fs(source)
search = fs(search)
replace = fs(replace)
count = fi(count)
if not search:
return source
if count == 0:
return source.replace(search, replace)
if count > 0:
return source.replace(search, replace, count)
# Replace *last* occurrences of "search" in "source"
slen = len(search)
i = len(source)
while True:
i = source.rfind(search, 0, i)
if i < 0:
break
source = source[:i] + replace + source[i + slen:]
count += 1
if count == 0:
break
return source
def llRot2Angle(r):
r = q2f(r)
# Used by llAngleBetween.

View file

@ -197,4 +197,33 @@
, llSubStringIndex("a", "blah")
, llStringTrim(" a\n ", STRING_TRIM)
, llStringTrim("", STRING_TRIM)
, llReplaceSubString("cabadabaeaba", "aba", "faba", -4)
, llReplaceSubString("cabadabaeaba", "aba", "faba", -3)
, llReplaceSubString("cabadabaeaba", "aba", "faba", -2)
, llReplaceSubString("cabadabaeaba", "aba", "faba", -1)
, llReplaceSubString("cabadabaeaba", "aba", "faba", 0)
, llReplaceSubString("cabadabaeaba", "aba", "faba", 1)
, llReplaceSubString("cabadabaeaba", "aba", "faba", 2)
, llReplaceSubString("cabadabaeaba", "aba", "faba", 3)
, llReplaceSubString("cabadabaeaba", "aba", "faba", 4)
, llReplaceSubString("cabadabaeaba", "eba", "feba", -4)
, llReplaceSubString("cabadabaeaba", "eba", "feba", -1)
, llReplaceSubString("cabadabaeaba", "eba", "feba", 0)
, llReplaceSubString("cabadabaeaba", "eba", "feba", 1)
, llReplaceSubString("cabadabaeaba", "eba", "feba", 4)
, llReplaceSubString("cabadabaeaba", "", "faba", -20)
, llReplaceSubString("cabadabaeaba", "", "faba", -1)
, llReplaceSubString("cabadabaeaba", "", "faba", 0)
, llReplaceSubString("cabadabaeaba", "", "faba", 1)
, llReplaceSubString("cabadabaeaba", "", "faba", 20)
, llReplaceSubString("", "", "faba", -2)
, llReplaceSubString("", "", "faba", -1)
, llReplaceSubString("", "", "faba", 0)
, llReplaceSubString("", "", "faba", 1)
, llReplaceSubString("", "", "faba", 2)
, llReplaceSubString("", "a", "ba", -2)
, llReplaceSubString("", "a", "ba", -1)
, llReplaceSubString("", "a", "ba", 0)
, llReplaceSubString("", "a", "ba", 1)
, llReplaceSubString("", "a", "ba", 2)
]

View file

@ -197,4 +197,33 @@
, -1
, "a"
, ""
, "cfabadfabaefaba"
, "cfabadfabaefaba"
, "cabadfabaefaba"
, "cabadabaefaba"
, "cfabadfabaefaba"
, "cfabadabaeaba"
, "cfabadfabaeaba"
, "cfabadfabaefaba"
, "cfabadfabaefaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, "cabadabaeaba"
, ""
, ""
, ""
, ""
, ""
, ""
, ""
, ""
, ""
, ""
]