implement string, float, complex

This commit is contained in:
Sergey Fedorov 2024-04-19 16:00:23 +03:00
parent 63f20dd06e
commit e51f988cc5
2 changed files with 138 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"errors" "errors"
"math"
"reflect" "reflect"
) )
@ -42,6 +43,49 @@ func (g *Generator) fillAny(any reflect.Value) error {
return err return err
} }
case reflect.String:
str, err := g.GenerateString()
if err != nil {
return err
}
any.SetString(str)
case reflect.Float32:
newFloat, err := g.GenerateFloat32()
if err != nil {
return err
}
any.SetFloat(float64(newFloat))
case reflect.Float64:
newFloat, err := g.GenerateFloat64()
if err != nil {
return err
}
any.SetFloat(float64(newFloat))
case reflect.Complex64:
newFloatReal, err := g.GenerateFloat32()
if err != nil {
return err
}
newFloatImag, err := g.GenerateFloat32()
if err != nil {
return err
}
any.SetComplex(complex(float64(newFloatReal), float64(newFloatImag)))
case reflect.Complex128:
newFloatReal, err := g.GenerateFloat64()
if err != nil {
return err
}
newFloatImag, err := g.GenerateFloat64()
if err != nil {
return err
}
any.SetComplex(complex(newFloatReal, newFloatImag))
default: default:
panic("unhandled default case") panic("unhandled default case")
} }
@ -61,3 +105,83 @@ func (g *Generator) GenerateInt() (int, error) {
g.position++ g.position++
return result, nil return result, nil
} }
func (g *Generator) GenerateUInt32() (uint32, error) {
if g.position+3 >= g.dataSize {
return 0, errors.New("the data bytes are over")
}
result := uint32(0)
for i := 0; i < 4; i++ {
result = result<<8 | uint32(g.data[g.position])
g.position++
}
return result, nil
}
func (g *Generator) GenerateString() (string, error) {
maxStrLength := uint32(2000000)
if g.position >= g.dataSize {
return "nil", errors.New("the data bytes are over")
}
length, err := g.GenerateUInt32()
if err != nil {
return "nil", errors.New("the data bytes are over")
}
if g.position > maxStrLength {
return "nil", errors.New("the string length too large")
}
startBytePos := g.position
if startBytePos >= g.dataSize {
return "nil", errors.New("the data bytes are over")
}
if startBytePos+length > g.dataSize {
return "nil", errors.New("the data bytes are over")
}
if startBytePos > startBytePos+length {
return "nil", errors.New("overflow")
}
g.position = startBytePos + length
result := string(g.data[startBytePos:g.position])
return result, nil
}
func (g *Generator) GenerateFloat32() (float32, error) {
if g.position+3 >= g.dataSize {
return 0, errors.New("the data bytes are over")
}
bits := uint32(0)
for i := 0; i < 4; i++ {
bits = bits<<8 | uint32(g.data[g.position])
g.position++
}
floatBits := uint32(math.Float32bits(math.Float32frombits(bits)))
result := math.Float32frombits(floatBits)
return result, nil
}
func (g *Generator) GenerateFloat64() (float64, error) {
if g.position+7 >= g.dataSize {
return 0, errors.New("the data bytes are over")
}
bits := uint64(0)
for i := 0; i < 8; i++ {
bits = bits<<8 | uint64(g.data[g.position])
g.position++
}
result := math.Float64frombits(bits)
return result, nil
}

14
main.go
View file

@ -13,6 +13,20 @@ type Struct2 struct {
Field2 Struct1 Field2 Struct1
} }
type Struct3 struct {
Field1 string
Field2 float32
Field3 float64
Field4 complex64
Field5 complex128
}
type Struct4 struct {
Field1 [5]int
Field2 map[string]int
Field3 func()
}
func main() { func main() {
} }