This commit is contained in:
Looly 2021-07-14 17:09:59 +08:00
parent 89a3acfa88
commit eda8bdc27a
7 changed files with 46 additions and 17 deletions

View File

@ -11,6 +11,7 @@
* 【core 】 增加NanoId
* 【core 】 MapBuilder增加put方法pr#367@Gitee
* 【core 】 StrUtil.insert支持负数index
* 【core 】 Calculator类支持取模运算issue#I40DUW@Gitee
### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee
@ -18,6 +19,7 @@
* 【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

@ -177,7 +177,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if(index < 0){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
@ -221,7 +221,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if(index < 0){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
@ -252,7 +252,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if(index < 0){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}
@ -309,7 +309,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if(index < 0){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
if ((index < 0)) {
throw new StringIndexOutOfBoundsException(index);
}

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

@ -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);
}
}