Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Spammer): improvement of custom format #4866

Open
wants to merge 1 commit into
base: nextgen
Choose a base branch
from

Conversation

MukjepScarlet
Copy link
Contributor

improved replacement

Comment on lines +104 to 119
private inline fun String.replace(oldValue: String, newValueProvider: () -> Any): String {
var index = 0
val newString = StringBuilder(this)
while (true) {
index = newString.indexOf(oldValue, startIndex = index)
if (index == -1) {
break
}

val newValue = newValueProvider().toString()
newString.replace(index, index + oldValue.length, newValue)

index += newValue.length
}
return newString.toString()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What makes this better than the previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. For different keyword (%d, %f, etc), one replacement will only create one StringBuilder, the previous one will create many strings. (one %d will create 3 new strings)
  2. The indexOf now will not search from the head, next search will start from the tail of previous one like:
example%ianother%iumm
^
search 1 start
example12345another%iumm
            ^
search 2 start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants