Merge remote-tracking branch 'origin/v5-dev' into v5-dev

# Conflicts:
#	hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java
This commit is contained in:
duandazhi 2021-07-14 17:11:51 +08:00
commit 75d5268249
8 changed files with 77 additions and 18 deletions

View File

@ -10,12 +10,16 @@
* 【core 】 IterUtil增加firstMatch方法
* 【core 】 增加NanoId
* 【core 】 MapBuilder增加put方法pr#367@Gitee
* 【core 】 StrUtil.insert支持负数index
* 【core 】 Calculator类支持取模运算issue#I40DUW@Gitee
### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee
* 【core 】 修复ClassScanner扫描空包遗漏问题
* 【core 】 修复FastDatePrinter歧义问题pr#366@Gitee
* 【core 】 修复DateUtil.format格式化Instant报错问题issue#I40CY2@Gitee
* 【core 】 修复StrUtil.toUnderlineCase大写问题issue#I40CGS@Gitee
* 【jwt 】 修复JWT.validate报错问题issue#I40MR2@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -144,7 +144,7 @@ public class Calculator {
* @return 是否为算术符号
*/
private boolean isOperator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')';
return c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')' || c == '%';
}
/**
@ -155,7 +155,17 @@ public class Calculator {
* @return 优先级
*/
public boolean compare(char cur, char peek) {// 如果是peek优先级高于cur返回true默认都是peek优先级要低
return operatPriority[(peek) - 40] >= operatPriority[(cur) - 40];
final int offset = 40;
if(cur == '%'){
// %优先级最高
cur = 47;
}
if(peek == '%'){
// %优先级最高
peek = 47;
}
return operatPriority[(peek) - offset] >= operatPriority[(cur) - offset];
}
/**
@ -163,7 +173,7 @@ public class Calculator {
*
* @param firstValue 第一个值
* @param secondValue 第二个值
* @param currentOp 算数符只支持'+''-''*''/'
* @param currentOp 算数符只支持'+''-''*''/''%'
* @return 结果
*/
private BigDecimal calculate(String firstValue, String secondValue, char currentOp) {
@ -181,6 +191,9 @@ public class Calculator {
case '/':
result = NumberUtil.div(firstValue, secondValue);
break;
case '%':
result = NumberUtil.toBigDecimal(firstValue).remainder(NumberUtil.toBigDecimal(secondValue));
break;
default:
throw new IllegalStateException("Unexpected value: " + currentOp);
}

View File

@ -174,6 +174,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
* @return this
*/
public StrBuilder insert(int index, char c) {
if(index < 0){
index = this.position + index;
}
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
moveDataAfterIndex(index, 1);
value[index] = c;
this.position = Math.max(this.position, index) + 1;
@ -211,9 +218,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if (ArrayUtil.isEmpty(src) || srcPos > src.length || length <= 0) {
return this;
}
if (index < 0) {
index = 0;
if(index < 0){
index = this.position + index;
}
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
if (srcPos < 0) {
srcPos = 0;
} else if (srcPos + length > src.length) {
@ -238,6 +249,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
* @return this
*/
public StrBuilder insert(int index, CharSequence csq) {
if(index < 0){
index = this.position + index;
}
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
if (null == csq) {
csq = StrUtil.EMPTY;
}
@ -288,8 +306,11 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if (start >= end) {
return this;
}
if (index < 0) {
index = 0;
if(index < 0){
index = this.position + index;
}
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
final int length = end - start;

View File

@ -22,4 +22,10 @@ public class CalculatorTest {
final double conversion = Calculator.conversion("1");
Assert.assertEquals(1, conversion, 2);
}
@Test
public void conversationTest4(){
final double conversion = Calculator.conversion("(88*66/23)%26+45%9");
Assert.assertEquals((88D * 66 / 23) % 26, conversion, 2);
}
}

View File

@ -14,7 +14,7 @@ import cn.hutool.core.lang.Console;
*
*/
public class StrBuilderTest {
/**
* StrBuilder的性能测试
*/
@ -28,7 +28,7 @@ public class StrBuilderTest {
builder.reset();
}
Console.log(timer.interval());
timer.restart();
StringBuilder b2 = new StringBuilder();
for(int i =0; i< 1000000; i++) {
@ -37,14 +37,14 @@ public class StrBuilderTest {
}
Console.log(timer.interval());
}
@Test
public void appendTest() {
StrBuilder builder = StrBuilder.create();
builder.append("aaa").append("你好").append('r');
Assert.assertEquals("aaa你好r", builder.toString());
}
@Test
public void insertTest() {
StrBuilder builder = StrBuilder.create(1);
@ -52,7 +52,7 @@ public class StrBuilderTest {
builder.insert(3, "数据插入");
Assert.assertEquals("aaa数据插入你好r", builder.toString());
}
@Test
public void insertTest2() {
StrBuilder builder = StrBuilder.create(1);
@ -60,7 +60,7 @@ public class StrBuilderTest {
builder.insert(8, "数据插入");
Assert.assertEquals("aaa你好r 数据插入", builder.toString());
}
@Test
public void resetTest() {
StrBuilder builder = StrBuilder.create(1);
@ -69,7 +69,7 @@ public class StrBuilderTest {
builder.reset();
Assert.assertEquals("", builder.toString());
}
@Test
public void resetTest2() {
StrBuilder builder = StrBuilder.create(1);
@ -79,7 +79,7 @@ public class StrBuilderTest {
builder.append("bbb".toCharArray());
Assert.assertEquals("bbb", builder.toString());
}
@Test
public void appendObjectTest() {
StrBuilder builder = StrBuilder.create(1);
@ -100,7 +100,6 @@ public class StrBuilderTest {
public void delTest2() {
// 删除中间部分测试
StrBuilder strBuilder = new StrBuilder("ABCDEFG");
int length = strBuilder.length();
StrBuilder builder = strBuilder.del(2,6);
Assert.assertEquals("ABG", builder.toString());
}

View File

@ -364,13 +364,20 @@ public class StrUtilTest {
.set("Table_Test_Of_day", "table_test_of_day")
.set("_Table_Test_Of_day_", "_table_test_of_day_")
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
.set("_TableTestOfDAYtoday", "_table_test_of_DAY_today")
.set("_TableTestOfDAYToday", "_table_test_of_DAY_today")
.set("HelloWorld_test", "hello_world_test")
.set("H2", "H2")
.set("H#case", "H#case")
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
}
@Test
public void toUnderLineCaseTest2() {
Dict.create()
.set("PNLabel", "PN_label")
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
}
@Test
public void containsAnyTest() {
//字符

View File

@ -352,7 +352,7 @@ public class JWT implements RegisteredPayload<JWT> {
// 校验时间字段
try {
JWTValidator.of(tokens.get(2)).validateDate(DateUtil.date(), leeway);
JWTValidator.of(this).validateDate(DateUtil.date(), leeway);
} catch (ValidateException e) {
return false;
}

View File

@ -3,6 +3,7 @@ package cn.hutool.jwt;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ValidateException;
import cn.hutool.jwt.signers.JWTSignerUtil;
import org.junit.Assert;
import org.junit.Test;
public class JWTValidatorTest {
@ -60,4 +61,12 @@ public class JWTValidatorTest {
// 验证算法
JWTValidator.of(token).validateAlgorithm(JWTSignerUtil.hs256("123456".getBytes()));
}
@Test
public void validateTest(){
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNb0xpIiwiZXhwIjoxNjI0OTU4MDk0NTI4LCJpYXQiOjE2MjQ5NTgwMzQ1MjAsInVzZXIiOiJ1c2VyIn0.L0uB38p9sZrivbmP0VlDe--j_11YUXTu3TfHhfQhRKc";
byte[] key = "1234567890".getBytes();
boolean validate = JWT.of(token).setKey(key).validate(0);
Assert.assertFalse(validate);
}
}