Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve the TimeSeriesRange fromString() #488

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static cn.edu.tsinghua.iginx.utils.StringUtils.isContainSpecialChar;

@JSONType(seeAlso = {TimeSeriesInterval.class, TimeSeriesPrefixRange.class}, typeKey = "type")
public interface TimeSeriesRange extends Comparable<TimeSeriesRange> {

Expand Down Expand Up @@ -101,10 +103,21 @@ default public boolean isAfter(String tsName) {
public void setClosed(boolean closed);

//Strange function: it should not work on the implementation of TimeSeriesPrefixRange
public static TimeSeriesRange fromString(String str) {
String[] parts = str.split("-");
assert parts.length == 2;
return new TimeSeriesInterval(parts[0].equals("null") ? null : parts[0], parts[1].equals("null") ? null : parts[1]);
public static TimeSeriesRange fromString(String str) throws Exception {
if (str.contains("-") && !isContainSpecialChar(str)) {
String[] parts = str.split("-");
assert parts.length == 2;
return new TimeSeriesInterval(parts[0].equals("null") ? null : parts[0], parts[1].equals("null") ? null : parts[1]);
} else {
if (str.contains(".*") && str.indexOf(".*") == str.length() - 2)
str = str.substring(0, str.length()-2);
if(!isContainSpecialChar(str))
return new TimeSeriesPrefixRange(str);
else {
logger.error("Input invalid string format in TimeSeriesRange");
throw new Exception("Input invalid string format in TimeSeriesRange");
}
}
}

public boolean isContain(String tsName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {

Expand Down Expand Up @@ -106,4 +108,11 @@ public static String reformatColumnName(String name) {
name = name.replaceAll("[)]", "[)]");
return name;
}

public static boolean isContainSpecialChar(String str) {
String regEx = "[~!@#$%&()+=|{}':;',<>?~]|\r|\n|\t|[\u2E80-\u9FFF]";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Chinese characters are not allowed? Please fix.

Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
return m.find();
}
}