Skip to content

Commit f524a5a

Browse files
committed
StringUtils.parseLocaleString parses variant correctly when variant contains country code
This commit also includes a JUnit 4 style revision of StringUtilsTests and ObjectUtilsTests. Issue: SPR-11806 (cherry picked from commit 295a6ae)
1 parent f053ce5 commit f524a5a

File tree

3 files changed

+228
-71
lines changed

3 files changed

+228
-71
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,6 @@
5050
* @author Rick Evans
5151
* @author Arjen Poutsma
5252
* @since 16 April 2001
53-
* @see org.apache.commons.lang.StringUtils
5453
*/
5554
public abstract class StringUtils {
5655

@@ -87,7 +86,7 @@ public static boolean isEmpty(Object str) {
8786
/**
8887
* Check that the given CharSequence is neither {@code null} nor of length 0.
8988
* Note: Will return {@code true} for a CharSequence that purely consists of whitespace.
90-
* <p><pre>
89+
* <p><pre class="code">
9190
* StringUtils.hasLength(null) = false
9291
* StringUtils.hasLength("") = false
9392
* StringUtils.hasLength(" ") = true
@@ -116,7 +115,7 @@ public static boolean hasLength(String str) {
116115
* Check whether the given CharSequence has actual text.
117116
* More specifically, returns {@code true} if the string not {@code null},
118117
* its length is greater than 0, and it contains at least one non-whitespace character.
119-
* <p><pre>
118+
* <p><pre class="code">
120119
* StringUtils.hasText(null) = false
121120
* StringUtils.hasText("") = false
122121
* StringUtils.hasText(" ") = false
@@ -207,7 +206,7 @@ public static String trimWhitespace(String str) {
207206

208207
/**
209208
* Trim <i>all</i> whitespace from the given String:
210-
* leading, trailing, and inbetween characters.
209+
* leading, trailing, and in between characters.
211210
* @param str the String to check
212211
* @return the trimmed String
213212
* @see java.lang.Character#isWhitespace
@@ -264,7 +263,7 @@ public static String trimTrailingWhitespace(String str) {
264263
}
265264

266265
/**
267-
* Trim all occurences of the supplied leading character from the given String.
266+
* Trim all occurrences of the supplied leading character from the given String.
268267
* @param str the String to check
269268
* @param leadingCharacter the leading character to be trimmed
270269
* @return the trimmed String
@@ -281,7 +280,7 @@ public static String trimLeadingCharacter(String str, char leadingCharacter) {
281280
}
282281

283282
/**
284-
* Trim all occurences of the supplied trailing character from the given String.
283+
* Trim all occurrences of the supplied trailing character from the given String.
285284
* @param str the String to check
286285
* @param trailingCharacter the trailing character to be trimmed
287286
* @return the trimmed String
@@ -380,7 +379,7 @@ public static int countOccurrencesOf(String str, String sub) {
380379
}
381380

382381
/**
383-
* Replace all occurences of a substring within a string with
382+
* Replace all occurrences of a substring within a string with
384383
* another string.
385384
* @param inString String to examine
386385
* @param oldPattern String to replace
@@ -679,10 +678,11 @@ public static boolean pathEquals(String path1, String path2) {
679678
/**
680679
* Parse the given {@code localeString} value into a {@link Locale}.
681680
* <p>This is the inverse operation of {@link Locale#toString Locale's toString}.
682-
* @param localeString the locale string, following {@code Locale's}
681+
* @param localeString the locale String, following {@code Locale's}
683682
* {@code toString()} format ("en", "en_UK", etc);
684683
* also accepts spaces as separators, as an alternative to underscores
685684
* @return a corresponding {@code Locale} instance
685+
* @throws IllegalArgumentException in case of an invalid locale specification
686686
*/
687687
public static Locale parseLocaleString(String localeString) {
688688
String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
@@ -691,10 +691,10 @@ public static Locale parseLocaleString(String localeString) {
691691
validateLocalePart(language);
692692
validateLocalePart(country);
693693
String variant = "";
694-
if (parts.length >= 2) {
694+
if (parts.length > 2) {
695695
// There is definitely a variant, and it is everything after the country
696696
// code sans the separator between the country code and the variant.
697-
int endIndexOfCountryCode = localeString.lastIndexOf(country) + country.length();
697+
int endIndexOfCountryCode = localeString.indexOf(country, language.length()) + country.length();
698698
// Strip off any leading '_' and whitespace, what's left is the variant.
699699
variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
700700
if (variant.startsWith("_")) {

0 commit comments

Comments
 (0)