该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
/**
*
* @param input
* 输入字符串
* @param format
* 格式字符串
* @param export
* 输出
* @return
*/
public static boolean sscanf(String input, String format, @SuppressWarnings("rawtypes") Collection export) {
if (format.indexOf('%') < 0) {
return false;
}
// 分解格式
List subFormats = new ArrayList<>();
if (!resolveFormatString(subFormats, format)) {
return false;
}
// 解析输入
if (!resolveInputString(subFormats, format, export, input)) {
return false;
}
return true;
}
@SuppressWarnings("unchecked")
private static boolean resolveInputString(List subFormats, String format,
@SuppressWarnings("rawtypes") Collection export, String input) {
StringWriter writer = null;
for (int formatIndex = 0, inputIndex = 0; formatIndex < subFormats.size(); ++formatIndex) {
String subFormat = subFormats.get(formatIndex);
if ("%d".equals(subFormat) || "%l".equals(subFormat)) {
for (; inputIndex < input.length(); ++inputIndex) {
char c = input.charAt(inputIndex);
if ('9' < c || '0' > c) {
if (writer == null) {
continue;
} else {
break;
}
} else {
if (writer == null) {
writer = new StringWriter(input.length());
}
writer.append(c);
}
}
long integer = Long.parseLong(writer.toString());
writer = null;
export.add(integer);
} else if ("%f".equals(subFormat)) {
int dotCount = 0;
for (; inputIndex < input.length(); ++inputIndex) {
char c = input.charAt(inputIndex);
if (c == '.') {
++dotCount;
}
if (dotCount > 1) {
break;
}
if (('9' < c || '0' > c) && '.' != c) {
if (writer == null) {
continue;
} else {
break;
}
} else {
if (writer == null) {
writer = new StringWriter(input.length());
}
writer.append(c);
}
}
double integer = Double.parseDouble(writer.toString());
writer = null;
export.add(integer);
} else if ("%c".equals(subFormat)) {
char c = input.charAt(inputIndex);
export.add(c);
++inputIndex;
} else if ("%s".equals(subFormat)) {
if (formatIndex >= subFormats.size() - 1) {
export.add(input.substring(inputIndex));
inputIndex = input.length();
continue;
}
String subFormat2 = subFormats.get(formatIndex + 1);
if ("%d".equals(subFormat2) || "%l".equals(subFormat2)) {
for (; inputIndex < input.length(); ++inputIndex) {
char c = input.charAt(inputIndex);
if (writer == null) {
writer = new StringWriter(input.length());
}
if ('9' < c || '0' > c) {
writer.append(c);
} else {
break;
}
}
export.add(writer.toString());
writer = null;
} else if ("%f".equals(subFormat2)) {
for (; inputIndex < input.length(); ++inputIndex) {
char c = input.charAt(inputIndex);
if (writer == null) {
writer = new StringWriter(input.length());
}
if (('9' < c || '0' > c) && '.' != c) {
writer.append(c);
} else {
break;
}
}
export.add(writer.toString());
writer = null;
} else if ("%s".equals(subFormat2) || "%c".equals(subFormat2)) {
return false;
} else {
int nextInputIndex = input.indexOf(subFormat2, inputIndex);
String skipString = input.substring(inputIndex, nextInputIndex);
inputIndex = nextInputIndex + subFormat2.length();
export.add(skipString);
++formatIndex;
}
} else {
inputIndex = input.indexOf(subFormat, inputIndex) + subFormat.length();
}
}
return true;
}
private static boolean resolveFormatString(List subFormats, String format) {
// 分解格式
StringWriter writer = null;
for (int index = 0; index < format.length(); ++index) {
char c = format.charAt(index);
// 普通字符
if (c != '%') {
if (writer != null) {
subFormats.add(writer.toString());
}
writer = new StringWriter(format.length());
writer.append(c);
continue;
}
// 结束判定
if (index == format.length() - 1) {
writer.append(c);
break;
}
// 下一个字符
++index;
char c2 = format.charAt(index);
// 发现%
if (c2 == '%') {
writer.append(c);
continue;
}
// 格式判定
if ("dflsc".indexOf(c2) < 0) {
return false;
}
if (writer != null) {
subFormats.add(writer.toString());
}
writer = new StringWriter(format.length());
writer.append(c);
writer.append(c2);
}
if (writer != null) {
subFormats.add(writer.toString());
}
return true;
}