
* Modify rule S5361: Add Python as covered language * Fix typos in S5361 rule descriptions * Remove wrong delimiters * Rename var and fix comment
51 lines
1.6 KiB
Plaintext
51 lines
1.6 KiB
Plaintext
The underlying implementation of ``++String::replaceAll++`` calls the ``++java.util.regex.Pattern.compile()++`` method each time it is called even if the first argument is not a regular expression. This has a significant performance cost and therefore should be used with care.
|
|
|
|
|
|
When ``++String::replaceAll++`` is used, the first argument should be a real regular expression. If it's not the case, ``++String::replace++`` does exactly the same thing as ``++String::replaceAll++`` without the performance drawback of the regex.
|
|
|
|
|
|
This rule raises an issue for each ``++String::replaceAll++`` used with a ``++String++`` as first parameter which doesn't contains special regex character or pattern.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replaceAll("Bob is", "It's"); // Noncompliant
|
|
changed = changed.replaceAll("\\.\\.\\.", ";"); // Noncompliant
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replace("Bob is", "It's");
|
|
changed = changed.replace("...", ";");
|
|
----
|
|
Or, with a regex:
|
|
|
|
----
|
|
String init = "Bob is a Bird... Bob is a Plane... Bob is Superman!";
|
|
String changed = init.replaceAll("\\w*\\sis", "It's");
|
|
changed = changed.replaceAll("\\.{3}", ";");
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* S4248 - Regex patterns should not be created needlessly
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|