Skip to content

Commit

Permalink
c
Browse files Browse the repository at this point in the history
  • Loading branch information
baichangda committed Aug 2, 2024
1 parent 07d8bc4 commit 99ac821
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build
### Mac OS ###
.DS_Store
*.class
gradle-bcd.properties
gradle-bcd.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.bcd.parser.base.builder;

import cn.bcd.parser.base.Parser;
import cn.bcd.parser.base.anno.BitRemainingMode;
import cn.bcd.parser.base.anno.F_bit_num;
import cn.bcd.parser.base.anno.F_bit_num_array;
Expand All @@ -12,6 +13,8 @@

public class FieldBuilder__F_bit_num extends FieldBuilder {

public static boolean optimization = true;

private boolean finish(BuilderContext context) {
List<Field> fieldList = context.class_fieldList;
F_bit_num f_bit_num = context.field.getAnnotation(F_bit_num.class);
Expand Down Expand Up @@ -74,8 +77,11 @@ public void buildParse(BuilderContext context) {
throw ParseException.get("class[{}] field[{}] anno[{}] len[{}] must in range [1,64]", field.getDeclaringClass().getName(), field.getName(), annoClass.getName(), len);
}


ParseUtil.append(body, "final {} {}=({}){}.read({},{},{});\n", sourceValTypeName, varNameField, sourceValTypeName, varNameBitBuf, len, bigEndian, unsigned);
if (optimization && bigEndian && unsigned && Parser.logCollector_parse == null && len <= 8) {
ParseUtil.append(body, "final {} {}=({}){}.read_1_8({});\n", sourceValTypeName, varNameField, sourceValTypeName, varNameBitBuf, len);
} else {
ParseUtil.append(body, "final {} {}=({}){}.read({},{},{});\n", sourceValTypeName, varNameField, sourceValTypeName, varNameBitBuf, len, bigEndian, unsigned);
}

String valCode = ParseUtil.replaceValExprToCode(anno.valExpr(), varNameField);
if (fieldTypeClass.isEnum()) {
Expand Down Expand Up @@ -166,7 +172,12 @@ public void buildDeParse(BuilderContext context) {
if (len < 1 || len > 64) {
throw ParseException.get("class[{}] field[{}] anno[{}] len[{}] must in range [1,64]", field.getDeclaringClass().getName(), field.getName(), annoClass.getName(), len);
}
ParseUtil.append(body, "{}.write((long)({}),{},{},{});\n", varNameBitBuf, valCode, len, bigEndian, unsigned);

if (optimization && bigEndian && Parser.logCollector_parse == null && len <= 8) {
ParseUtil.append(body, "{}.write_1_8((int)({}),{});\n", varNameBitBuf, valCode, len);
} else {
ParseUtil.append(body, "{}.write((long)({}),{},{});\n", varNameBitBuf, valCode, len, bigEndian);
}

if (skipAfter > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, skipAfter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.bcd.parser.base.builder;

import cn.bcd.parser.base.Parser;
import cn.bcd.parser.base.anno.BitRemainingMode;
import cn.bcd.parser.base.anno.F_bit_num;
import cn.bcd.parser.base.anno.F_bit_num_array;
Expand All @@ -12,6 +13,8 @@

public class FieldBuilder__F_bit_num_array extends FieldBuilder {

public static boolean optimization = true;

private boolean finish(BuilderContext context) {
List<Field> fieldList = context.class_fieldList;
F_bit_num_array f_bit_num_array = context.field.getAnnotation(F_bit_num_array.class);
Expand Down Expand Up @@ -52,7 +55,7 @@ public void buildParse(BuilderContext context) {
final String varNameField = ParseUtil.getFieldVarName(context);
int skipBefore = anno.skipBefore();
int skipAfter = anno.skipAfter();
if(skipBefore>0){
if (skipBefore > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, skipBefore);
}
switch (arrayElementTypeName) {
Expand Down Expand Up @@ -83,7 +86,12 @@ public void buildParse(BuilderContext context) {

ParseUtil.append(body, "final {}[] {}=new {}[{}];\n", arrayElementTypeName, arrVarName, arrayElementTypeName, arrLenRes);
ParseUtil.append(body, "for(int i=0;i<{}.length;i++){\n", arrVarName);
ParseUtil.append(body, "final {} {}=({}){}.read({},{},{});\n", arrayElementTypeName, varNameArrayElement, arrayElementTypeName, varNameBitBuf, singleLen, bigEndian, unsigned);
if (optimization && bigEndian && unsigned && Parser.logCollector_parse == null && singleLen <= 8) {
ParseUtil.append(body, "final {} {}=({}){}.read_1_8({});\n", arrayElementTypeName, varNameArrayElement, arrayElementTypeName, varNameBitBuf, singleLen);
} else {
ParseUtil.append(body, "final {} {}=({}){}.read({},{},{});\n", arrayElementTypeName, varNameArrayElement, arrayElementTypeName, varNameBitBuf, singleLen, bigEndian, unsigned);
}

if (singleSkip > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, singleSkip);
}
Expand All @@ -95,7 +103,7 @@ public void buildParse(BuilderContext context) {
ParseUtil.append(body, "{}[i]=({})({});\n", arrVarName, arrayElementTypeName, valCode);
ParseUtil.append(body, "}\n");
ParseUtil.append(body, "{}.{}={};\n", FieldBuilder.varNameInstance, field.getName(), arrVarName);
if(skipAfter>0){
if (skipAfter > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, skipAfter);
}
if (finish(context)) {
Expand Down Expand Up @@ -125,26 +133,32 @@ public void buildDeParse(BuilderContext context) {
int skipBefore = anno.skipBefore();
int skipAfter = anno.skipAfter();
ParseUtil.append(body, "if({}!=null){\n", FieldBuilder.varNameInstance, valCode);
if (skipBefore>0){
if (skipBefore > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, skipBefore);
}
final String varNameFieldArr = varNameField + "_arr";
ParseUtil.append(body, "final {}[] {}={};\n", arrElementType, varNameFieldArr, valCode);
ParseUtil.append(body, "for(int i=0;i<{}.length;i++){\n", varNameFieldArr);
String writeValCode;
if (anno.singleValExpr().isEmpty()) {
ParseUtil.append(body, "{}.write((long)({}),{},{},{});\n", varNameBitBuf, varNameFieldArr + "[i]", singleLen, bigEndian, unsigned);
writeValCode = varNameFieldArr + "[i]";
} else {
if (isFloat) {
ParseUtil.append(body, "{}.write((long)({}),{},{},{});\n", varNameBitBuf, ParseUtil.replaceValExprToCode_round(RpnUtil.reverseExpr(anno.singleValExpr()), varNameFieldArr + "[i]"), singleLen, bigEndian, unsigned);
writeValCode = ParseUtil.replaceValExprToCode_round(RpnUtil.reverseExpr(anno.singleValExpr()), varNameFieldArr + "[i]");
} else {
ParseUtil.append(body, "{}.write((long)({}),{},{},{});\n", varNameBitBuf, ParseUtil.replaceValExprToCode(RpnUtil.reverseExpr(anno.singleValExpr()), varNameFieldArr + "[i]"), singleLen, bigEndian, unsigned);
writeValCode = ParseUtil.replaceValExprToCode(RpnUtil.reverseExpr(anno.singleValExpr()), varNameFieldArr + "[i]");
}
}
if (optimization &&bigEndian && Parser.logCollector_parse == null && singleLen <= 8) {
ParseUtil.append(body, "{}.write_1_8((int)({}),{});\n", varNameBitBuf, writeValCode, singleLen);
} else {
ParseUtil.append(body, "{}.write((long)({}),{},{});\n", varNameBitBuf, writeValCode, singleLen, bigEndian);
}
if (singleSkip > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, singleSkip);
}
ParseUtil.append(body, "}\n");
if (skipAfter>0){
if (skipAfter > 0) {
ParseUtil.append(body, "{}.skip({});\n", varNameBitBuf, skipAfter);
}
if (finish(context)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,52 @@ public static void main(String[] args) {
};
ByteBuf bb2 = Unpooled.wrappedBuffer(source2);
BitBuf_reader bitBuf2 = new BitBuf_reader(bb2);
final long res1 = bitBuf2.read(3, true, true);
final long res2 = bitBuf2.read(8, true, true);
bitBuf2.skip(3);
final long res3 = bitBuf2.read(9, false, false);
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
long l1 = bitBuf2.read_1_8(3);
long l2 = bitBuf2.read_1_8(3);
long l3 = bitBuf2.read_1_8(3);
System.out.println(l1);
System.out.println(l2);
System.out.println(l3);
// final long res1 = bitBuf2.read(3, true, true);
// final long res2 = bitBuf2.read(8, true, true);
// bitBuf2.skip(3);
// final long res3 = bitBuf2.read(9, false, false);
// System.out.println(res1);
// System.out.println(res2);
// System.out.println(res3);
}
System.out.println(System.currentTimeMillis() - t1);

}

public int read_1_8(int bit) {
final ByteBuf byteBuf = this.byteBuf;
final int bitOffset = this.bitOffset;
byte b;
if (bitOffset == 0) {
b = byteBuf.readByte();
} else {
b = this.b;
}
final int temp = bit + bitOffset;
if (temp < 8) {
this.bitOffset = temp;
if (bitOffset == 0) {
this.b = b;
}
return (b >>> (8 - temp)) & ((1 << bit) - 1);
} else if (temp > 8) {
byte nextB = byteBuf.readByte();
int res = (((b << 8) | (nextB & 0xff)) >>> (16 - temp)) & ((1 << bit) - 1);
this.b = nextB;
this.bitOffset = temp - 8;
return res;
} else {
this.bitOffset = 0;
return b & ((1 << bit) - 1);
}
}

public long read(int bit, boolean bigEndian, boolean unsigned) {
final ByteBuf byteBuf = this.byteBuf;
final int bitOffset = this.bitOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public long read(int bit, boolean bigEndian, boolean unsigned) {

if (!unsigned && ((cRight >> (bit - 1)) & 0x01) == 1) {
log.val3 = cRight | (-1L << bit);
log.signed3 = true;
} else {
log.val3 = cRight & ((0x01L << bit) - 1);
}
Expand Down Expand Up @@ -251,8 +250,6 @@ public static class ReadLog extends Log {
public long val1;
//处理大小端之后的值
public long val2;
//是否有符号
public boolean signed3;
//最终解析值
public long val3;

Expand All @@ -262,12 +259,8 @@ public ReadLog(int byteLen, int bitStart, int bit, boolean bigEndian, boolean un
this.unsigned = unsigned;
}

public final String getLogBit(long l, boolean signed) {
if (signed) {
return "-" + Strings.padStart(Long.toBinaryString(-l), bit, '0');
} else {
return Strings.padStart(Long.toBinaryString(l), bit, '0');
}
public final String getLogBit(long l) {
return Strings.padStart(Long.toBinaryString(l), bit, '0');
}

@Override
Expand All @@ -277,7 +270,7 @@ public String msg() {
bitStart, bitEnd,
bigEndian ? "yes" : "no",
unsigned ? "yes" : "no",
getLogBit(val1, false), getLogBit(val2, false), getLogBit(val3, signed3),
getLogBit(val1), getLogBit(val2), getLogBit(val3),
val1, val2, val3);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,48 @@ public static void main(String[] args) {

final ByteBuf bb = Unpooled.buffer();
final BitBuf_writer bitBufWriter = new BitBuf_writer(bb);
bitBufWriter.write(4, 3, true, true);
bitBufWriter.write(0, 3, true, true);
bitBufWriter.skip(3);
bitBufWriter.write(-217, 9, false, false);
bitBufWriter.finish();
// bitBufWriter.write(4, 3, true, true);
// bitBufWriter.write(0, 3, true, true);
// bitBufWriter.skip(3);
// bitBufWriter.write(-217, 9, false, false);
// bitBufWriter.finish();
bitBufWriter.write_1_8(4, 3);
bitBufWriter.write_1_8(0, 3);
bitBufWriter.write_1_8(2, 3);
bitBufWriter.write_1_8(0b111, 3);
bitBufWriter.write_1_8(0b0010, 4);
bitBufWriter.write_1_8(0b01000000, 8);
System.out.println(ByteBufUtil.hexDump(bb));
}
System.out.println(System.currentTimeMillis() - t1);
}


public void write(long l, int bit, boolean bigEndian, boolean unsigned) {
public void write_1_8(int v, int bit) {
v = v & ((1 << bit) - 1);
final ByteBuf byteBuf = this.byteBuf;
int bitOffset = this.bitOffset;
byte b = this.b;
if (!unsigned && l < 0) {
l = l & ((0x01L << bit) - 1);
final int temp = bit + bitOffset;
if (temp < 8) {
this.b = (byte) (b | (v << (8 - temp)));
this.bitOffset = temp;
} else if (temp > 8) {
int i = temp - 8;
byteBuf.writeByte(b | (v >>> i));
this.b = (byte) (v << (8 - i));
this.bitOffset = i;
} else {
byteBuf.writeByte(b | v);
this.b = 0;
this.bitOffset = 0;
}
}

public void write(long l, int bit, boolean bigEndian) {
l = l & ((1L << bit) - 1);
final ByteBuf byteBuf = this.byteBuf;
int bitOffset = this.bitOffset;
byte b = this.b;
if (!bigEndian) {
l = Long.reverse(l) >>> (64 - bit);
}
Expand All @@ -80,22 +104,19 @@ public void write(long l, int bit, boolean bigEndian, boolean unsigned) {
byteLen = (temp >> 3) + 1;
newL = l << (8 - finalBitOffset);
}
if (bitOffset == 0) {
b = (byte) (newL >> ((byteLen - 1) << 3));
} else {
b |= (byte) (newL >> ((byteLen - 1) << 3));
}
b |= (byte) (newL >> ((byteLen - 1) << 3));
for (int i = 1; i < byteLen; i++) {
byteBuf.writeByte(b);
b = (byte) (newL >> ((byteLen - i - 1) << 3));
}
bitOffset = finalBitOffset;
if (bitOffset == 0) {
if (finalBitOffset == 0) {
byteBuf.writeByte(b);
this.bitOffset = 0;
this.b = 0;
} else {
this.bitOffset = finalBitOffset;
this.b = b;
}

this.bitOffset = bitOffset;
this.b = b;
}

public void skip(int bit) {
Expand Down
Loading

0 comments on commit 99ac821

Please sign in to comment.