Skip to content

Commit

Permalink
improve the valid method
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 24, 2024
1 parent 9539486 commit aabdcca
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
59 changes: 53 additions & 6 deletions core/src/main/java/lucee/commons/i18n/FormatterWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FormatterWrapper {
private final boolean hasComma;
private final boolean hasSlash;
private final boolean hasColon;
private final boolean hasSpace;
private final boolean hasWhitespace;
private final boolean hasHyphen;

FormatterWrapper(DateTimeFormatter formatter, String pattern, short type, ZoneId zone) {
Expand All @@ -29,7 +29,7 @@ public class FormatterWrapper {
this.hasSlash = pattern.indexOf('/') != -1;
this.hasHyphen = pattern.indexOf('-') != -1;
this.hasColon = pattern.indexOf(':') != -1;
this.hasSpace = pattern.indexOf(' ') != -1;
this.hasWhitespace = pattern.chars().anyMatch(Character::isWhitespace);
}

FormatterWrapper(DateTimeFormatter formatter, String pattern, short type, ZoneId zone, boolean custom) {
Expand All @@ -44,7 +44,7 @@ public class FormatterWrapper {
this.hasSlash = pattern.indexOf('/') != -1;
this.hasHyphen = pattern.indexOf('-') != -1;
this.hasColon = pattern.indexOf(':') != -1;
this.hasSpace = pattern.indexOf(' ') != -1;
this.hasWhitespace = pattern.chars().anyMatch(Character::isWhitespace);
}

public boolean valid(String str) {
Expand Down Expand Up @@ -78,13 +78,60 @@ public boolean valid(String str) {
if (str.indexOf(':') != -1) return false;
}

if (hasSpace) {
if (str.indexOf(' ') == -1) return false;
if (hasWhitespace) {
if (!str.chars().anyMatch(Character::isWhitespace)) return false;
}
else {
if (str.indexOf(' ') != -1) return false;
if (str.chars().anyMatch(Character::isWhitespace)) return false;
}
return true;
}

public boolean validx(String str) {
if (pattern.length() > str.length()) return false;

boolean foundComma = false;
boolean foundHyphen = false;
boolean foundSlash = false;
boolean foundColon = false;
boolean foundWhitespace = false;

// Single pass through the string
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
switch (c) {
case ',':
if (!hasComma) return false;
foundComma = true;
break;
case '-':
if (!hasHyphen) return false;
foundHyphen = true;
break;
case '/':
if (!hasSlash) return false;
foundSlash = true;
break;
case ':':
if (!hasColon) return false;
foundColon = true;
break;
default:
if (Character.isWhitespace(c)) {
if (!hasWhitespace) return false;
foundWhitespace = true;
}
}
}

// Only check for required characters we haven't found
if (hasComma && !foundComma) return false;
if (hasHyphen && !foundHyphen) return false;
if (hasSlash && !foundSlash) return false;
if (hasColon && !foundColon) return false;
if (hasWhitespace && !foundWhitespace) return false;

return true;
}

}
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.253-SNAPSHOT"/>
<property name="version" value="6.2.0.254-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.253-SNAPSHOT</version>
<version>6.2.0.254-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit aabdcca

Please sign in to comment.