TextStrategy is used for:
E - DAY_OF_WEEK
G - ERA
M - MONTH
a - AM_PM
SimpleDateFormat uses case-insensitive parsing for each of these fields.
I will add tests for each of those fields in multiple Locales.
The (?u)(?i) modifier is active just for the duration of the group.
Consider the following unit test:
@Test
public void testCaseSensitiveModifier() throws Exception {
Pattern aabb = Pattern.compile("((?u)(?i)AA)BB");
assertTrue(aabb.matcher("aaBB").matches());
assertTrue(aabb.matcher("AABB").matches());
assertFalse(aabb.matcher("aabb").matches());
assertFalse(aabb.matcher("AAbb").matches());
}
Regards,
chas
On 4/23/14, 12:04 PM, "sebb" <[email protected]> wrote:
>On 23 April 2014 17:02, <[email protected]> wrote:
>> Author: chas
>> Date: Wed Apr 23 16:02:52 2014
>> New Revision: 1589446
>>
>> URL: http://svn.apache.org/r1589446
>> Log:
>> LANG-966 - FastDateParser should be case insensitive
>>
>> Modified:
>>
>>commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/Fas
>>tDateParser.java
>>
>>commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/Fas
>>tDateParserTest.java
>>
>> Modified:
>>commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/Fas
>>tDateParser.java
>> URL:
>>http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/
>>apache/commons/lang3/time/FastDateParser.java?rev=1589446&r1=1589445&r2=1
>>589446&view=diff
>>
>>=========================================================================
>>=====
>> ---
>>commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/Fas
>>tDateParser.java (original)
>> +++
>>commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/Fas
>>tDateParser.java Wed Apr 23 16:02:52 2014
>> @@ -25,6 +25,7 @@ import java.text.ParsePosition;
>> import java.util.ArrayList;
>> import java.util.Calendar;
>> import java.util.Date;
>> +import java.util.HashMap;
>> import java.util.List;
>> import java.util.Locale;
>> import java.util.Map;
>> @@ -586,6 +587,7 @@ public class FastDateParser implements D
>> private static class TextStrategy extends Strategy {
>> private final int field;
>> private final Map<String, Integer> keyValues;
>> + private final Map<String, Integer> lKeyValues;
>
>This is an extra data area for ALL Text fields, but is only needed for
>Month parsing.
>
>I think a separate class is needed.
>
>> /**
>> * Construct a Strategy that parses a Text field
>> @@ -596,6 +598,11 @@ public class FastDateParser implements D
>> TextStrategy(final int field, final Calendar definingCalendar,
>>final Locale locale) {
>> this.field= field;
>> this.keyValues= getDisplayNames(field, definingCalendar,
>>locale);
>> + this.lKeyValues= new HashMap<String,Integer>();
>> +
>> + for(Map.Entry<String, Integer> entry :
>>keyValues.entrySet()) {
>> + lKeyValues.put(entry.getKey().toLowerCase(),
>>entry.getValue());
>> + }
>> }
>>
>> /**
>> @@ -603,7 +610,7 @@ public class FastDateParser implements D
>> */
>> @Override
>> boolean addRegex(final FastDateParser parser, final
>>StringBuilder regex) {
>> - regex.append('(');
>> + regex.append("((?i)(?u)");
>> for(final String textKeyValue : keyValues.keySet()) {
>> escapeRegex(regex, textKeyValue, false).append('|');
>
>-1
>
>AFAICT, this will enable incorrect matching of text fields that are
>not supposed to be case-blind.
>Also, the case-blind matching is not disabled at the end of the
>section, so can affect later matches.
>
>Using a separate class for case-blind matching will fix the first
>issue, and will avoid creating duplicate data unnecessarily.
>
>> }
>> @@ -616,7 +623,7 @@ public class FastDateParser implements D
>> */
>> @Override
>> void setCalendar(final FastDateParser parser, final Calendar
>>cal, final String value) {
>> - final Integer iVal = keyValues.get(value);
>> + final Integer iVal = lKeyValues.get(value.toLowerCase());
>> if(iVal == null) {
>> final StringBuilder sb= new StringBuilder(value);
>> sb.append(" not in (");
>>
>> Modified:
>>commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/Fas
>>tDateParserTest.java
>> URL:
>>http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/
>>apache/commons/lang3/time/FastDateParserTest.java?rev=1589446&r1=1589445&
>>r2=1589446&view=diff
>>
>>=========================================================================
>>=====
>> ---
>>commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/Fas
>>tDateParserTest.java (original)
>> +++
>>commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/Fas
>>tDateParserTest.java Wed Apr 23 16:02:52 2014
>> @@ -540,4 +540,16 @@ public class FastDateParserTest {
>> final DateParser parser= getInstance(yMdHmsSZ, REYKJAVIK);
>> assertEquals(REYKJAVIK, parser.getTimeZone());
>> }
>> +
>> + @Test
>> + public void testLang996() throws ParseException {
>> + Calendar expected = Calendar.getInstance(NEW_YORK, Locale.US);
>> + expected.clear();
>> + expected.set(2014, 4, 14);
>> +
>> + final DateParser fdp = getInstance("ddMMMyyyy", NEW_YORK,
>>Locale.US);
>> + assertEquals(expected.getTime(), fdp.parse("14may2014"));
>> + assertEquals(expected.getTime(), fdp.parse("14MAY2014"));
>> + assertEquals(expected.getTime(), fdp.parse("14May2014"));
>> + }
>
>Need more tests to show that the parsing of other case-significant
>dates is not affected.
>
>> }
>>
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [email protected]
>For additional commands, e-mail: [email protected]
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]