Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
924060929 committed Oct 31, 2024
1 parent 0b34f22 commit fb83e5a
Show file tree
Hide file tree
Showing 19 changed files with 763 additions and 400 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
public class AndChecker extends FormatChecker {
private final List<FormatChecker> checkers;

public AndChecker(StringInspect stringInspect, List<FormatChecker> checkers) {
super(stringInspect);
public AndChecker(String name, List<FormatChecker> checkers) {
super(name);
this.checkers = checkers;
}

@Override
protected boolean doCheck() {
protected boolean doCheck(StringInspect stringInspect) {
for (FormatChecker checker : checkers) {
if (!checker.check()) {
if (!checker.check(stringInspect).matched) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,22 @@ public class AtLeastChecker extends FormatChecker {
private int maxRead;
private Predicate<Character> checker;

public AtLeastChecker(StringInspect stringInspect, int minCount, int maxRead, Predicate<Character> checker) {
super(stringInspect);
public AtLeastChecker(String name, int minCount, int maxRead, Predicate<Character> checker) {
super(name);
this.minCount = minCount;
this.maxRead = maxRead;
this.checker = checker;
}

@Override
protected boolean doCheck() {
protected boolean doCheck(StringInspect stringInspect) {
int count = 0;
boolean checkRead = maxRead >= 0;
while (!stringInspect.eos() && (!checkRead || count < maxRead)) {
if (!checker.test(stringInspect.lookAndStep())) {
if (!checker.test(stringInspect.lookAt())) {
break;
}
stringInspect.step();
count++;
}
return count >= minCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
public class CharChecker extends FormatChecker {
public final char c;

public CharChecker(StringInspect stringInspect, char c) {
super(stringInspect);
public CharChecker(String name, char c) {
super(name);
this.c = c;
}

@Override
protected boolean doCheck() {
protected boolean doCheck(StringInspect stringInspect) {
if (stringInspect.eos() || stringInspect.lookAndStep() != c) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

public class CheckResult {
public final FormatChecker checker;
public final StringInspect stringInspect;
public final boolean matched;
public final int checkStartIndex;
public final int checkEndIndex;

public CheckResult(
FormatChecker checker, StringInspect stringInspect,
boolean matched, int checkStartIndex, int checkEndIndex) {
this.checker = checker;
this.stringInspect = stringInspect;
this.matched = matched;
this.checkStartIndex = checkStartIndex;
this.checkEndIndex = checkEndIndex;
}

public int matchesLength() {
return checkEndIndex - checkStartIndex;
}

public String matchesContent() {
return stringInspect.str.substring(checkStartIndex, checkEndIndex);
}

public void stepBack() {
stringInspect.setIndex(checkStartIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
public class CustomCharChecker extends FormatChecker {
private Predicate<Character> checker;

public CustomCharChecker(StringInspect stringInspect, Predicate<Character> checker) {
super(stringInspect);
public CustomCharChecker(String name, Predicate<Character> checker) {
super(name);
this.checker = checker;
}

@Override
protected boolean doCheck() {
protected boolean doCheck(StringInspect stringInspect) {
if (stringInspect.eos() || !checker.test(stringInspect.lookAndStep())) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,79 +21,117 @@

/** DateTimeChecker */
public class DateTimeChecker extends FormatChecker {
private DateTimeChecker(StringInspect stringInspect) {
super(stringInspect);
}
private static final DateTimeChecker INSTANCE = new DateTimeChecker();

public static boolean isValidDateTime(String str) {
str = str.trim();
return new DateTimeChecker(new StringInspect(str)).check();
}
private final FormatChecker checker;

@Override
protected boolean doCheck() {
FormatChecker dateFormatChecker = and(
private DateTimeChecker() {
super("DateTimeChecker");

this.checker =
or(
// date
and(
and("Date",
or(
// 20241012
number(8, 8),
digit(8, 8),
// 2024-10-12
and(
number(4, 4), // year
digit(1, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
digit(1, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2) // day
digit(1, 2) // day
)
),
option(ch('Z'))
),
// datetime
and(
or(
and("DateTime",
or("YearToSecond",
// 20241012010203
number(14, 14),
and("FullCompactDateTime",
digit(8, 8),
atLeast(0, c -> c == 'T'),
digit(6, 6)
),

// 241012010203 or 241012T010203
and("ShortCompactDateTime",
digit(6, 6),
atLeast(0, c -> c == 'T'),
digit(6, 6)
),

// 2024-01-01 01:02:03
and(
number(4, 4), // year
and("NormalDateTime",
digit(1, 4), // year
chars(DateLiteral.punctuations::contains),
number(2, 2), // month
digit(1, 2), // month
chars(DateLiteral.punctuations::contains),
number(2, 2), // day
digit(1, 2), // day
atLeast(1, c -> c == 'T' || c == ' ' || DateLiteral.punctuations.contains(c)),
number(2, 2), // hour
chars(DateLiteral.punctuations::contains),
number(2, 2), // minute
chars(DateLiteral.punctuations::contains),
number(2, 2) // second
digit(1, 2), // hour
option(
and(
chars(DateLiteral.punctuations::contains),
digit(1, 2), // minute
option(
and(
chars(DateLiteral.punctuations::contains),
digit(1, 2) // second
)
)
)
)
)
),
option(nanoSecond()),
option(timeZone())
option("NanoSecond", nanoSecond()),
option("TimeZone", timeZone())
)
)
);
return dateFormatChecker.check();
);
}

public static boolean isValidDateTime(String str) {
str = str.trim();
StringInspect stringInspect = new StringInspect(str.trim());
return INSTANCE.check(stringInspect).matched && stringInspect.eos();
}

@Override
protected boolean doCheck(StringInspect stringInspect) {
return checker.check(stringInspect).matched;
}

private FormatChecker nanoSecond() {
return and(
ch('.'),
number(1)
digit(1)
);
}

private FormatChecker timeZone() {
// Z or +08:00 or -01:00
return or(
ch('Z'),
and(
chars(c -> c == '+' || c == '-'),
number(2, 2),
ch(':'),
number(2, 2)
// Z or +08:00 or UTC-01:00
return and(
// timezone: Europe/London, America/New_York
atLeast(0, c -> ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '/' || c == '_'),
option(
and(
chars(c -> c == '+' || c == '-'),
digit(1, 2),
option(
and(
ch(':'),
digit(1, 2),
option(
and(
ch(':'),
digit(1, 2)
)
)
)
)
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.doris.nereids.trees.expressions.literal.format;

import java.util.function.Predicate;

public class DebugChecker<T extends FormatChecker> extends FormatChecker {
private final T childChecker;
private final Predicate<T> debugPoint;

public DebugChecker(String name, T childChecker, Predicate<T> debugPoint) {
super(name);
this.childChecker = childChecker;
this.debugPoint = debugPoint;
}

@Override
protected boolean doCheck(StringInspect stringInspect) {
return debugPoint.test(childChecker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package org.apache.doris.nereids.trees.expressions.literal.format;

/** NumberChecker */
public class NumberChecker extends FormatChecker {
/** DigitChecker */
public class DigitChecker extends FormatChecker {
private int minCount;
private int maxRead;

public NumberChecker(StringInspect stringInspect, int minCount, int maxRead) {
super(stringInspect);
public DigitChecker(String name, int minCount, int maxRead) {
super(name);
this.minCount = minCount;
this.maxRead = maxRead;
}

@Override
protected boolean doCheck() {
protected boolean doCheck(StringInspect stringInspect) {
int numberCount = 0;
boolean checkRead = maxRead >= 0;
while (!stringInspect.eos() && (!checkRead || numberCount < maxRead)) {
Expand All @@ -42,10 +42,4 @@ protected boolean doCheck() {
}
return numberCount >= minCount;
}

public static void main(String[] args) {
NumberChecker numberChecker = new NumberChecker(new StringInspect("123"), 1, 3);
System.out.println(numberChecker.check());
System.out.println(numberChecker.getCheckContent());
}
}
Loading

0 comments on commit fb83e5a

Please sign in to comment.