From f1246a43175dc040584140c9792beb5dce521e9b Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Thu, 17 May 2012 09:36:38 +0300 Subject: [PATCH] Fix locale parsing error with en_en, tr_tr, etc Previously, StringUtils#parseLocaleString would parse locale strings having the same lowercase token for both language and country incorrectly, e.g. 'tr_tr' would parse to 'tr_TR_tr' as opposed to the expected 'tr_TR'. This commit fixes this behavior by using using String#lastIndexOf instead of String#indexOf when determining the location of the country code token. Issue: SPR-9420 --- .../main/java/org/springframework/util/StringUtils.java | 4 ++-- .../java/org/springframework/util/StringUtilsTests.java | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/StringUtils.java b/spring-core/src/main/java/org/springframework/util/StringUtils.java index 3e1960870f..c44c0c4ca7 100644 --- a/spring-core/src/main/java/org/springframework/util/StringUtils.java +++ b/spring-core/src/main/java/org/springframework/util/StringUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -674,7 +674,7 @@ public abstract class StringUtils { if (parts.length >= 2) { // There is definitely a variant, and it is everything after the country // code sans the separator between the country code and the variant. - int endIndexOfCountryCode = localeString.indexOf(country) + country.length(); + int endIndexOfCountryCode = localeString.lastIndexOf(country) + country.length(); // Strip off any leading '_' and whitespace, what's left is the variant. variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode)); if (variant.startsWith("_")) { diff --git a/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java b/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java index 65cf242724..b366ed7f96 100644 --- a/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/StringUtilsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -639,4 +639,11 @@ public class StringUtilsTests extends TestCase { } } + /** + * See SPR-9420. + */ + public void testParseLocaleWithSameLowercaseTokenForLanguageAndCountry() { + assertEquals("tr_TR", StringUtils.parseLocaleString("tr_tr").toString()); + assertEquals("bg_BG_vnt", StringUtils.parseLocaleString("bg_bg_vnt").toString()); + } }