From dac6bc4058802540d74dd1f5ec14049f28b14bd2 Mon Sep 17 00:00:00 2001 From: Sivachandran Paramasivam Date: Mon, 12 Aug 2024 14:59:54 +0530 Subject: [PATCH] Remove padding generate code We are formatting the generate code anyway --- gen/main.go | 349 +++++++++++++++++++++++++--------------------------- 1 file changed, 169 insertions(+), 180 deletions(-) diff --git a/gen/main.go b/gen/main.go index 54600e7..17e77fc 100644 --- a/gen/main.go +++ b/gen/main.go @@ -192,52 +192,49 @@ func generate(typ *def.Class, opt options) string { res += " _ = v32_\n" res += " _ = s_\n" - depth := 2 if opt.cpool { - res += emitReadI32(1) - res += pad(1) + "n := int(v32_)\n" + res += emitReadI32() + res += "n := int(v32_)\n" if opt.doNotKeepData { } else { if opt.sortedIDs { - res += pad(1) + fmt.Sprintf("this.IDMap = NewIDMap[%s](n)\n", refName(typ)) + res += fmt.Sprintf("this.IDMap = NewIDMap[%s](n)\n", refName(typ)) } else { - res += pad(1) + fmt.Sprintf("this.IDMap = make(map[%s]uint32, n)\n", refName(typ)) + res += fmt.Sprintf("this.IDMap = make(map[%s]uint32, n)\n", refName(typ)) } - res += pad(1) + fmt.Sprintf("this.%s = make([]%s, n)\n", name(typ), name(typ)) + res += fmt.Sprintf("this.%s = make([]%s, n)\n", name(typ), name(typ)) } res += " for i := 0; i < n; i++ {\n" - } else { - depth = 1 } if opt.cpool { - res += emitReadI32(depth) + res += emitReadI32() if opt.doNotKeepData { } else { - res += pad(depth) + fmt.Sprintf("id := %s(v32_)\n", refName(typ)) + res += fmt.Sprintf("id := %s(v32_)\n", refName(typ)) } } - res += generateBindLoop(typ, "bind", depth, true) + res += generateBindLoop(typ, "bind", true) if opt.cpool { if opt.doNotKeepData { } else { - res += pad(depth) + fmt.Sprintf("this.%s[i] = bind.Temp\n", name(typ)) + res += fmt.Sprintf("this.%s[i] = bind.Temp\n", name(typ)) if opt.sortedIDs { - res += pad(depth) + "this.IDMap.Set(id, i)\n" + res += "this.IDMap.Set(id, i)\n" } else { - res += pad(depth) + "this.IDMap[id] = uint32(i)\n" + res += "this.IDMap[id] = uint32(i)\n" } } - res += pad(1) + "}\n" + res += "}\n" } else { - res += pad(depth) + fmt.Sprintf("*this = bind.Temp\n") + res += fmt.Sprintf("*this = bind.Temp\n") } res += " return pos, nil\n" res += fmt.Sprintf("}\n") @@ -255,138 +252,138 @@ func generate(typ *def.Class, opt options) string { return res } -func generateBindLoop(typ *def.Class, bindName string, depth int, nestedAllowed bool) string { +func generateBindLoop(typ *def.Class, bindName string, nestedAllowed bool) string { fs := getUniqueFields(typ) cpoolFields := getUniqueCpoolFields(typ) complexFields := getNonBasicFields(typ) _ = complexFields res := "" - res += pad(depth) + fmt.Sprintf("for %sFieldIndex := 0; %sFieldIndex < len(%s.Fields); %sFieldIndex++ {\n", bindName, bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" %sArraySize := 1\n", bindName) - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Array {\n", bindName, bindName) - res += emitReadI32(depth + 2) - res += pad(depth) + fmt.Sprintf(" %sArraySize = int(v32_)\n", bindName) + res += fmt.Sprintf("for %sFieldIndex := 0; %sFieldIndex < len(%s.Fields); %sFieldIndex++ {\n", bindName, bindName, bindName, bindName) + res += fmt.Sprintf(" %sArraySize := 1\n", bindName) + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Array {\n", bindName, bindName) + res += emitReadI32() + res += fmt.Sprintf(" %sArraySize = int(v32_)\n", bindName) if len(complexFields) > 0 { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Type == typeMap.%s {\n", bindName, bindName, TypeID2Sym(complexFields[0].Type)) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = make([]%s, 0, %sArraySize)\n", + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Type == typeMap.%s {\n", bindName, bindName, TypeID2Sym(complexFields[0].Type)) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = make([]%s, 0, %sArraySize)\n", bindName, bindName, name(TypeForCPoolID(complexFields[0].Type)), name(TypeForCPoolID(complexFields[0].Type)), bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") } - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" for %sArrayIndex := 0; %sArrayIndex < %sArraySize; %sArrayIndex++ {\n", bindName, bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.ConstantPool {\n", bindName, bindName) - res += emitReadI32(depth + 2) + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" for %sArrayIndex := 0; %sArrayIndex < %sArraySize; %sArrayIndex++ {\n", bindName, bindName, bindName, bindName) + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.ConstantPool {\n", bindName, bindName) + res += emitReadI32() if len(cpoolFields) > 0 { - res += pad(depth) + fmt.Sprintf(" switch %s.Fields[%sFieldIndex].Field.Type {\n", bindName, bindName) + res += fmt.Sprintf(" switch %s.Fields[%sFieldIndex].Field.Type {\n", bindName, bindName) for _, field := range cpoolFields { - res += pad(depth) + fmt.Sprintf(" case typeMap.%s:\n", TypeID2Sym(field.Type)) - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].%s != nil {\n", bindName, bindName, goTypeName(field)) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = %s(v32_)\n", bindName, bindName, goTypeName(field), goTypeName(field)) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" case typeMap.%s:\n", TypeID2Sym(field.Type)) + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].%s != nil {\n", bindName, bindName, goTypeName(field)) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = %s(v32_)\n", bindName, bindName, goTypeName(field), goTypeName(field)) + res += fmt.Sprintf(" }\n") } - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") } - res += pad(depth) + fmt.Sprintf(" } else {\n") - res += pad(depth) + fmt.Sprintf(" %sFieldTypeID := %s.Fields[%sFieldIndex].Field.Type\n", bindName, bindName, bindName) + res += fmt.Sprintf(" } else {\n") + res += fmt.Sprintf(" %sFieldTypeID := %s.Fields[%sFieldIndex].Field.Type\n", bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" switch %sFieldTypeID {\n", bindName) - res += pad(depth) + fmt.Sprintf(" case typeMap.T_STRING:\n") - res += emitString(depth + 3) + res += fmt.Sprintf(" switch %sFieldTypeID {\n", bindName) + res += fmt.Sprintf(" case typeMap.T_STRING:\n") + res += emitString() if fieldsHas(fs, T_STRING) { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].string != nil {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].string = s_\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].string != nil {\n", bindName, bindName) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].string = s_\n", bindName, bindName) + res += fmt.Sprintf(" }\n") } else { - res += pad(depth) + fmt.Sprintf(" // skipping\n") + res += fmt.Sprintf(" // skipping\n") } - res += pad(depth) + fmt.Sprintf(" case typeMap.T_INT:\n") - res += emitReadI32(depth + 3) + res += fmt.Sprintf(" case typeMap.T_INT:\n") + res += emitReadI32() if fieldsHas(fs, T_INT) { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].uint32 != nil {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].uint32 = v32_\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].uint32 != nil {\n", bindName, bindName) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].uint32 = v32_\n", bindName, bindName) + res += fmt.Sprintf(" }\n") } else { - res += pad(depth) + fmt.Sprintf(" // skipping\n") + res += fmt.Sprintf(" // skipping\n") } - res += pad(depth) + fmt.Sprintf(" case typeMap.T_LONG:\n") - res += emitReadU64(depth + 3) + res += fmt.Sprintf(" case typeMap.T_LONG:\n") + res += emitReadU64() if fieldsHas(fs, T_LONG) { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].uint64 != nil {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].uint64 = v64_\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].uint64 != nil {\n", bindName, bindName) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].uint64 = v64_\n", bindName, bindName) + res += fmt.Sprintf(" }\n") } else { - res += pad(depth) + fmt.Sprintf(" // skipping\n") + res += fmt.Sprintf(" // skipping\n") } - res += pad(depth) + fmt.Sprintf(" case typeMap.T_BOOLEAN:\n") - res += emitReadByte(depth + 3) + res += fmt.Sprintf(" case typeMap.T_BOOLEAN:\n") + res += emitReadByte() if fieldsHas(fs, T_BOOLEAN) { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].bool != nil {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].bool = b_ != 0\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].bool != nil {\n", bindName, bindName) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].bool = b_ != 0\n", bindName, bindName) + res += fmt.Sprintf(" }\n") } else { - res += pad(depth) + fmt.Sprintf(" // skipping\n") + res += fmt.Sprintf(" // skipping\n") } - res += pad(depth) + fmt.Sprintf(" case typeMap.T_FLOAT:\n") - res += emitReadI32(depth + 3) + res += fmt.Sprintf(" case typeMap.T_FLOAT:\n") + res += emitReadI32() if fieldsHas(fs, T_FLOAT) { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].float32 != nil {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].float32 = *(*float32)(unsafe.Pointer(&v32_))\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].float32 != nil {\n", bindName, bindName) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].float32 = *(*float32)(unsafe.Pointer(&v32_))\n", bindName, bindName) + res += fmt.Sprintf(" }\n") } else { - res += pad(depth) + fmt.Sprintf(" // skipping\n") + res += fmt.Sprintf(" // skipping\n") } if nestedAllowed { for _, field := range complexFields { nestedType := TypeForCPoolID(field.Type) - res += pad(depth) + fmt.Sprintf(" case typeMap.%s:\n", TypeID2Sym(field.Type)) - res += generateBindLoop(nestedType, "bind"+name(nestedType), depth+3, false) + res += fmt.Sprintf(" case typeMap.%s:\n", TypeID2Sym(field.Type)) + res += generateBindLoop(nestedType, "bind"+name(nestedType), false) if field.Array { - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].%s != nil {\n", bindName, bindName, name(nestedType)) - res += pad(depth) + fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = append(*%s.Fields[%sFieldIndex].%s, bind%s.Temp)\n", bindName, bindName, name(nestedType), bindName, bindName, name(nestedType), name(nestedType)) - res += pad(depth) + fmt.Sprintf(" }\n") + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].%s != nil {\n", bindName, bindName, name(nestedType)) + res += fmt.Sprintf(" *%s.Fields[%sFieldIndex].%s = append(*%s.Fields[%sFieldIndex].%s, bind%s.Temp)\n", bindName, bindName, name(nestedType), bindName, bindName, name(nestedType), name(nestedType)) + res += fmt.Sprintf(" }\n") } else { panic("TODO " + field.String()) } } } - res += pad(depth) + fmt.Sprintf(" default:\n") + res += fmt.Sprintf(" default:\n") //todo array - res += pad(depth) + fmt.Sprintf(" %sFieldType := typeMap.IDMap[%s.Fields[%sFieldIndex].Field.Type]\n", bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" if %sFieldType == nil || len(%sFieldType.Fields) == 0 {\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" return 0, fmt.Errorf(\"unknown type %%d\", %s.Fields[%sFieldIndex].Field.Type)\n", bindName, bindName) - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" %sSkipObjects := 1\n", bindName) - res += pad(depth) + fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Array {\n", bindName, bindName) - res += emitReadI32(depth + 4) - res += pad(depth) + fmt.Sprintf(" %sSkipObjects = int(v32_)\n", bindName) - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" for %sSkipObjectIndex := 0; %sSkipObjectIndex < %sSkipObjects; %sSkipObjectIndex++ {\n", bindName, bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" for %sskipFieldIndex := 0; %sskipFieldIndex < len(%sFieldType.Fields); %sskipFieldIndex++ {\n", bindName, bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" %sSkipFieldType := %sFieldType.Fields[%sskipFieldIndex].Type\n", bindName, bindName, bindName) - res += pad(depth) + fmt.Sprintf(" if %sFieldType.Fields[%sskipFieldIndex].ConstantPool {\n", bindName, bindName) - res += emitReadI32(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_STRING{\n", bindName) - res += emitString(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_INT {\n", bindName) - res += emitReadI32(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_FLOAT {\n", bindName) - res += emitReadI32(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_LONG {\n", bindName) - res += emitReadU64(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_BOOLEAN {\n", bindName) - res += emitReadByte(depth + 7) - res += pad(depth) + fmt.Sprintf(" } else {\n") - res += pad(depth) + fmt.Sprintf(" return 0, fmt.Errorf(\"nested objects not implemented. \")\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf(" }\n") - res += pad(depth) + fmt.Sprintf("}\n") + res += fmt.Sprintf(" %sFieldType := typeMap.IDMap[%s.Fields[%sFieldIndex].Field.Type]\n", bindName, bindName, bindName) + res += fmt.Sprintf(" if %sFieldType == nil || len(%sFieldType.Fields) == 0 {\n", bindName, bindName) + res += fmt.Sprintf(" return 0, fmt.Errorf(\"unknown type %%d\", %s.Fields[%sFieldIndex].Field.Type)\n", bindName, bindName) + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" %sSkipObjects := 1\n", bindName) + res += fmt.Sprintf(" if %s.Fields[%sFieldIndex].Field.Array {\n", bindName, bindName) + res += emitReadI32() + res += fmt.Sprintf(" %sSkipObjects = int(v32_)\n", bindName) + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" for %sSkipObjectIndex := 0; %sSkipObjectIndex < %sSkipObjects; %sSkipObjectIndex++ {\n", bindName, bindName, bindName, bindName) + res += fmt.Sprintf(" for %sskipFieldIndex := 0; %sskipFieldIndex < len(%sFieldType.Fields); %sskipFieldIndex++ {\n", bindName, bindName, bindName, bindName) + res += fmt.Sprintf(" %sSkipFieldType := %sFieldType.Fields[%sskipFieldIndex].Type\n", bindName, bindName, bindName) + res += fmt.Sprintf(" if %sFieldType.Fields[%sskipFieldIndex].ConstantPool {\n", bindName, bindName) + res += emitReadI32() + res += fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_STRING{\n", bindName) + res += emitString() + res += fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_INT {\n", bindName) + res += emitReadI32() + res += fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_FLOAT {\n", bindName) + res += emitReadI32() + res += fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_LONG {\n", bindName) + res += emitReadU64() + res += fmt.Sprintf(" } else if %sSkipFieldType == typeMap.T_BOOLEAN {\n", bindName) + res += emitReadByte() + res += fmt.Sprintf(" } else {\n") + res += fmt.Sprintf(" return 0, fmt.Errorf(\"nested objects not implemented. \")\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf(" }\n") + res += fmt.Sprintf("}\n") return res } @@ -498,85 +495,85 @@ func generateBinding(typ *def.Class, opt options) string { return res } -func emitString(depth int) string { - res := pad(depth) + "s_ = \"\"\n" - res += pad(depth) + "if pos >= l {\n" - res += pad(depth) + " return 0, io.ErrUnexpectedEOF\n" - res += pad(depth) + "}\n" - res += pad(depth) + "b_ = data[pos]\n" - res += pad(depth) + "pos++\n" - res += pad(depth) + "switch b_ {\n" - res += pad(depth) + "case 0:\n" - res += pad(depth) + " break\n" - res += pad(depth) + "case 1:\n" - res += pad(depth) + " break\n" - res += pad(depth) + "case 3:\n" - res += emitReadI32(depth + 1) - res += pad(depth) + " if pos+int(v32_) > l {\n" - res += pad(depth) + " return 0, io.ErrUnexpectedEOF\n" - res += pad(depth) + " }\n" - - res += pad(depth) + " bs := data[pos : pos+int(v32_)]\n" - res += pad(depth) + fmt.Sprintf(" s_ = *(*string)(unsafe.Pointer(&bs))\n") - - res += pad(depth) + " pos += int(v32_)\n" - res += pad(depth) + "default:\n" - res += pad(depth) + " return 0, fmt.Errorf(\"unknown string type %d at %d\", b_, pos)\n" - res += pad(depth) + "}\n" +func emitString() string { + res := "s_ = \"\"\n" + res += "if pos >= l {\n" + res += " return 0, io.ErrUnexpectedEOF\n" + res += "}\n" + res += "b_ = data[pos]\n" + res += "pos++\n" + res += "switch b_ {\n" + res += "case 0:\n" + res += " break\n" + res += "case 1:\n" + res += " break\n" + res += "case 3:\n" + res += emitReadI32() + res += " if pos+int(v32_) > l {\n" + res += " return 0, io.ErrUnexpectedEOF\n" + res += " }\n" + + res += " bs := data[pos : pos+int(v32_)]\n" + res += fmt.Sprintf(" s_ = *(*string)(unsafe.Pointer(&bs))\n") + + res += " pos += int(v32_)\n" + res += "default:\n" + res += " return 0, fmt.Errorf(\"unknown string type %d at %d\", b_, pos)\n" + res += "}\n" return res } -func emitReadByte(depth int) string { +func emitReadByte() string { code := "" - code += pad(depth) + "if pos >= l {\n" - code += pad(depth) + " return 0, io.ErrUnexpectedEOF\n" - code += pad(depth) + "}\n" - code += pad(depth) + "b_ = data[pos]\n" - code += pad(depth) + "pos++\n" + code += "if pos >= l {\n" + code += " return 0, io.ErrUnexpectedEOF\n" + code += "}\n" + code += "b_ = data[pos]\n" + code += "pos++\n" return code } -func emitReadI32(depth int) string { +func emitReadI32() string { code := "" - code += pad(depth) + "v32_ = uint32(0)\n" - code += pad(depth) + "for shift = uint(0); ; shift += 7 {\n" - code += pad(depth) + " if shift >= 32 {\n" - code += pad(depth) + " return 0, def.ErrIntOverflow\n" - code += pad(depth) + " }\n" - code += pad(depth) + " if pos >= l {\n" - code += pad(depth) + " return 0, io.ErrUnexpectedEOF\n" - code += pad(depth) + " }\n" - code += pad(depth) + " b_ = data[pos]\n" - code += pad(depth) + " pos++\n" - code += pad(depth) + " v32_ |= uint32(b_&0x7F) << shift\n" - code += pad(depth) + " if b_ < 0x80 {\n" - code += pad(depth) + " break\n" - code += pad(depth) + " }\n" - code += pad(depth) + "}\n" + code += "v32_ = uint32(0)\n" + code += "for shift = uint(0); ; shift += 7 {\n" + code += " if shift >= 32 {\n" + code += " return 0, def.ErrIntOverflow\n" + code += " }\n" + code += " if pos >= l {\n" + code += " return 0, io.ErrUnexpectedEOF\n" + code += " }\n" + code += " b_ = data[pos]\n" + code += " pos++\n" + code += " v32_ |= uint32(b_&0x7F) << shift\n" + code += " if b_ < 0x80 {\n" + code += " break\n" + code += " }\n" + code += "}\n" return code } -func emitReadU64(depth int) string { +func emitReadU64() string { code := "" - code += pad(depth) + "v64_ = 0 \n" - code += pad(depth) + "for shift = uint(0); shift <= 56 ; shift += 7 {\n" - code += pad(depth) + " if pos >= l {\n" - code += pad(depth) + " return 0, io.ErrUnexpectedEOF\n" - code += pad(depth) + " }\n" - code += pad(depth) + " b_ = data[pos]\n" - code += pad(depth) + " pos++\n" - code += pad(depth) + " if shift == 56{\n" - code += pad(depth) + " v64_ |= uint64(b_&0xFF) << shift\n" - code += pad(depth) + " break\n" - code += pad(depth) + " } else {\n" - code += pad(depth) + " v64_ |= uint64(b_&0x7F) << shift\n" - code += pad(depth) + " if b_ < 0x80 {\n" - code += pad(depth) + " break\n" - code += pad(depth) + " }\n" - code += pad(depth) + " }\n" - code += pad(depth) + "}\n" + code += "v64_ = 0 \n" + code += "for shift = uint(0); shift <= 56 ; shift += 7 {\n" + code += " if pos >= l {\n" + code += " return 0, io.ErrUnexpectedEOF\n" + code += " }\n" + code += " b_ = data[pos]\n" + code += " pos++\n" + code += " if shift == 56{\n" + code += " v64_ |= uint64(b_&0xFF) << shift\n" + code += " break\n" + code += " } else {\n" + code += " v64_ |= uint64(b_&0x7F) << shift\n" + code += " if b_ < 0x80 {\n" + code += " break\n" + code += " }\n" + code += " }\n" + code += "}\n" return code } @@ -627,11 +624,3 @@ func listName(typ *def.Class) string { func capitalize(s string) string { return strings.ToUpper(s[:1]) + s[1:] } - -func pad(n int) string { - res := "" - for i := 0; i < n; i++ { - res += "\t" - } - return res -}