mirror of
https://gitee.com/willfree/min-dev-java.git
synced 2026-10-04 19:42:01 +08:00
add test: StringHelper
This commit is contained in:
1 parent
99d7f94fec
commit
d7f063cbb9
3 files changed
+95
-12
No files matched your search
@@ -4,6 +4,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
@@ -12,7 +13,45 @@ import java.util.LinkedList;
|
||||
* @Date: 11:27 2021/3/10
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
class VarByteArray{
|
||||
private int MaxNum=9000; // byte初始化长度
|
||||
private byte[] bytes;
|
||||
private int len; //实际长度
|
||||
public VarByteArray(){
|
||||
bytes=new byte[MaxNum];
|
||||
len=0;
|
||||
}
|
||||
public VarByteArray(int maxNum){
|
||||
MaxNum=maxNum;
|
||||
bytes=new byte[MaxNum];
|
||||
len=0;
|
||||
}
|
||||
|
||||
/**
|
||||
* put进一个byte
|
||||
* @param b
|
||||
*/
|
||||
public void put(byte b){
|
||||
while(len>=MaxNum) {
|
||||
MaxNum*=2;
|
||||
byte[] newBytes=new byte[MaxNum];
|
||||
System.arraycopy(bytes,0,newBytes,0,len);
|
||||
bytes=newBytes;
|
||||
}
|
||||
bytes[len] = b;
|
||||
len++;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回一个byte[]
|
||||
* @return
|
||||
*/
|
||||
public byte[] byteArray(){
|
||||
byte[] res=new byte[len];
|
||||
System.arraycopy(bytes,0,res,0,len);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
public class StringHelper {
|
||||
|
||||
private static byte toHexChar(byte n){
|
||||
@@ -50,26 +89,26 @@ public class StringHelper {
|
||||
public static String escape(String str){
|
||||
byte[] strBytes=str.getBytes(StandardCharsets.UTF_8);
|
||||
int len=strBytes.length;
|
||||
ByteBuffer byteBuffer = null;
|
||||
int index=0;
|
||||
VarByteArray byteBuffer = new VarByteArray(str.getBytes().length);
|
||||
// int index=0;
|
||||
for(int i=0;i<len;i++){
|
||||
byte ch=strBytes[i];
|
||||
if((ch>='a'&&ch<='z')||
|
||||
(ch>='A'&&ch<='Z')||
|
||||
(ch>='0'&&ch<='9')||
|
||||
ch=='-'||ch=='.'||ch=='_'||ch=='~'){
|
||||
byteBuffer.put(index,ch);
|
||||
index++;
|
||||
byteBuffer.put(ch);
|
||||
// index++;
|
||||
}else{
|
||||
byteBuffer.put(index,(byte)'%');
|
||||
index++;
|
||||
byteBuffer.put(index,toHexChar((byte) ((ch & 0xf0) >> 4)));
|
||||
index++;
|
||||
byteBuffer.put(index,toHexChar((byte) (ch & 0xf)));
|
||||
index++;
|
||||
byteBuffer.put((byte)'%');
|
||||
// index++;
|
||||
byteBuffer.put(toHexChar((byte) ((ch & 0xf0) >> 4)));
|
||||
// index++;
|
||||
byteBuffer.put(toHexChar((byte) (ch & 0xf)));
|
||||
// index++;
|
||||
}
|
||||
}
|
||||
return new String(byteBuffer.array());
|
||||
return new String(byteBuffer.byteArray());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@ import java.math.BigInteger;
|
||||
* @Date: 21:27 2021/03/02
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class TestSizeT {
|
||||
public class SizeTest {
|
||||
|
||||
public static void main(String[] args){
|
||||
// BigInteger bigInteger=new BigInteger("3");
|
||||
@@ -0,0 +1,44 @@
|
||||
package util;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/*
|
||||
* @Author: Wang Feng
|
||||
* @Description:
|
||||
* @Version: 1.0.0
|
||||
* @Date: 19:47 2021/3/23
|
||||
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
|
||||
*/
|
||||
public class StringHelperTest {
|
||||
@Test
|
||||
public void testEscapeAndUnescape(){
|
||||
String s1="北京大学深圳研究生院";
|
||||
String s2=StringHelper.escape(s1);
|
||||
System.out.println(s2);
|
||||
String s3=StringHelper.unescape(s2);
|
||||
System.out.println(s3);
|
||||
assertEquals(s1,s3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscape(){
|
||||
String s="-_~.a北京大学z深圳A研究生院Z";
|
||||
String encoded=StringHelper.escape(s);
|
||||
String want="-_~.a%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6z%E6%B7%B1%E5%9C%B3A%E7%A0%94%E7%A9%B6%E7%94%9F%E9%99%A2Z";
|
||||
String decoded=StringHelper.unescape(encoded);
|
||||
assertEquals(encoded,want);
|
||||
assertEquals(decoded,s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEscape2(){
|
||||
String s="-_~.azAZ09";
|
||||
String encoded=StringHelper.escape(s);
|
||||
String want="-_~.azAZ09";
|
||||
String decoded=StringHelper.unescape(encoded);
|
||||
assertEquals(encoded,want);
|
||||
assertEquals(decoded,s);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user