rspec/rules/S1449/java/rule.adoc

47 lines
1.4 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Failure to specify a locale when calling the methods ``++toLowerCase()++``, ``++toUpperCase()++`` or ``++format()++`` on ``++String++`` objects means the system default encoding will be used, possibly creating problems with international characters or number representations. For instance with the Turkish language, when converting the small letter 'i' to upper case, the result is capital letter 'I' with a dot over it.
Case conversion without a locale may work fine in its "home" environment, but break in ways that are extremely difficult to diagnose for customers who use different encodings. Such bugs can be nearly, if not completely, impossible to reproduce when it's time to fix them. For locale-sensitive strings, the correct locale should always be used, but ``++Locale.ROOT++`` can be used for case-insensitive ones.
=== Noncompliant code example
[source,java]
----
myString.toLowerCase()
----
=== Compliant solution
[source,java]
----
myString.toLowerCase(Locale.TR)
----
== Resources
* https://wiki.sei.cmu.edu/confluence/x/4zdGBQ[CERT, STR02-J.] - Specify an appropriate locale when comparing locale-dependent data
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Define the locale to be used in this String operation.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]