mirror of https://github.com/longtai-cn/hippo4j
parent
71e96fb9bc
commit
db8ac51f4b
@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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 cn.hippo4j.common.toolkit;
|
||||
|
||||
/**
|
||||
* number util
|
||||
*/
|
||||
public class NumberUtil {
|
||||
public static boolean isNumber(CharSequence str) {
|
||||
if (StringUtil.isBlank(str)) {
|
||||
return false;
|
||||
}
|
||||
char[] chars = str.toString().toCharArray();
|
||||
int sz = chars.length;
|
||||
boolean hasExp = false;
|
||||
boolean hasDecPoint = false;
|
||||
boolean allowSigns = false;
|
||||
boolean foundDigit = false;
|
||||
// deal with any possible sign up front
|
||||
int start = (chars[0] == '-' || chars[0] == '+') ? 1 : 0;
|
||||
if (sz > start + 1) {
|
||||
if (chars[start] == '0' && (chars[start + 1] == 'x' || chars[start + 1] == 'X')) {
|
||||
int i = start + 2;
|
||||
if (i == sz) {
|
||||
return false;
|
||||
}
|
||||
for (; i < chars.length; i++) {
|
||||
if ((chars[i] < '0' || chars[i] > '9') && (chars[i] < 'a' || chars[i] > 'f') && (chars[i] < 'A' || chars[i] > 'F')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
sz--;
|
||||
int i = start;
|
||||
while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
|
||||
if (chars[i] >= '0' && chars[i] <= '9') {
|
||||
foundDigit = true;
|
||||
allowSigns = false;
|
||||
|
||||
} else if (chars[i] == '.') {
|
||||
if (hasDecPoint || hasExp) {
|
||||
return false;
|
||||
}
|
||||
hasDecPoint = true;
|
||||
} else if (chars[i] == 'e' || chars[i] == 'E') {
|
||||
if (hasExp) {
|
||||
return false;
|
||||
}
|
||||
if (!foundDigit) {
|
||||
return false;
|
||||
}
|
||||
hasExp = true;
|
||||
allowSigns = true;
|
||||
} else if (chars[i] == '+' || chars[i] == '-') {
|
||||
if (!allowSigns) {
|
||||
return false;
|
||||
}
|
||||
allowSigns = false;
|
||||
foundDigit = false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (i < chars.length) {
|
||||
if (chars[i] >= '0' && chars[i] <= '9') {
|
||||
return true;
|
||||
}
|
||||
if (chars[i] == 'e' || chars[i] == 'E') {
|
||||
return false;
|
||||
}
|
||||
if (chars[i] == '.') {
|
||||
if (hasDecPoint || hasExp) {
|
||||
return false;
|
||||
}
|
||||
return foundDigit;
|
||||
}
|
||||
if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
|
||||
return foundDigit;
|
||||
}
|
||||
if (chars[i] == 'l' || chars[i] == 'L') {
|
||||
return foundDigit && !hasExp;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return !allowSigns && foundDigit;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue