Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
matsuuram committed Dec 8, 2023
1 parent ed48433 commit 6a85307
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 0 deletions.
95 changes: 95 additions & 0 deletions bitbuffer/bitbuffer2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package bitbuffer

import (
"fmt"
"math"
"unsafe"

"golang.org/x/exp/constraints"
)

////////////////////////////////////////////////
// すげぇ単純にGeneric化してみる
//
// しかし、これに果たして意味があるのか?

// func NewPeek[T constraints.Unsigned](b *BitBuffer, length uint16) T {
// var t interface{} = *new(T)
// switch t.(type) {
// case uint8:
// return T(b.PeekUint8(length))
// case uint16:
// return T(b.PeekUint8(length))
// case uint32:
// return T(b.PeekUint8(length))
// case uint64:
// return T(b.PeekUint8(length))
// }
// }

const BYTE_SIZE = 8

func Peek[T constraints.Unsigned](b *BitBuffer, length uint16) (T, error) {
retVal, err := b.NewPeek(length)
if err != nil {
return 0, err
}

var t interface{} = *new(T)
s := uint16(unsafe.Sizeof(t) * BYTE_SIZE)
if length > s {
return 0, fmt.Errorf("length(%d) is out of range(0-%d)", length, s)
}

return T(retVal), nil
}

func (b *BitBuffer) NewPeek(length uint16) (uint64, error) {
if length > 64 {
return 0, fmt.Errorf("length(%d) is out of range(0-64)", length)
}
bufSize := uint16(len(b.buf) * BYTE_SIZE)
if (b.pos + length) > bufSize {
return 0, fmt.Errorf("length(%d) is out of buf size(%d)", length, bufSize)
}
firstByteIndex := b.pos / BYTE_SIZE
firstBytePos := b.pos % BYTE_SIZE
lastByteIndex := (b.pos + length) / BYTE_SIZE
// fmt.Printf("(b.pos + length): %d\n", (b.pos + length))
lastBytePos := (b.pos + length) % BYTE_SIZE

var retVal uint64
var pos uint16 = 0
var digit uint16 = length
// byte単位のループ
for i := firstByteIndex; i <= lastByteIndex; i++ {
// fmt.Printf("index roop: b.pos=%d, index=%d, firstByteIndex=%d, firstBytePos=%d, lastByteIndex=%d, lastBytePos=%d\n",
// b.pos, i, firstByteIndex, firstBytePos, lastByteIndex, lastBytePos)
pos = 0
if i == firstByteIndex {
pos = firstBytePos
}

// byteの中のbit単位のループ
for j := pos; j < BYTE_SIZE; j++ {
// fmt.Printf("isBitSet: %v, i=%d, j=%d, pos=%d, buf=%#08b\n", isBitSet(b.buf[i], BYTE_SIZE-1-j), i, j, pos, b.buf[i])
if isBitSet(b.buf[i], BYTE_SIZE-1-j) {
// fmt.Printf("retVal add: %d\n", uint64(math.Pow(2, float64(digit-1))))
retVal += uint64(math.Pow(2, float64(digit-1)))
}
digit--
pos++
if i == lastByteIndex && pos > lastBytePos {
break
}
}
}
b.pos += length

return retVal, nil
}

// 任意の桁のbitが立っているかを返す
func isBitSet(b byte, p uint16) bool {
return ((b >> p) & 1) == 1
}
95 changes: 95 additions & 0 deletions cmd/generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"fmt"

"golang.org/x/exp/constraints"
)

func genericTest() {

// var hoge uint16 = 1
// valueType(hoge)
// typeParameterType[uint32](1)
}

/////////////////////////////////////////////////
// 今までのGoの書き方

func containsInt(needle int, array []int) bool {
for _, v := range array {
if v == needle {
return true
}
}
return false
}

func containsInt64(needle int64, array []int64) bool {
for _, v := range array {
if v == needle {
return true
}
}
return false
}

/////////////////////////////////////////////////
// go.1.18で導入されたGenericを使うと・・・

func genericContains[T comparable](needle T, array []T) bool {
for _, v := range array {
if v == needle {
return true
}
}
return false
}

/////////////////////////////////////////////////
// 午前中に調査していたこと
// 関数内での型判定は出来るのか?

// 引数の値から調べる
// goはコンパイル時に呼び出しに応じてTの型が決定される
func valueType[T constraints.Unsigned](input T) T {
switch any(input).(type) {
case uint8:
fmt.Println("type: uint8")
case uint16:
fmt.Println("type: uint16")
case uint32:
fmt.Println("type: uint32")
case uint64:
fmt.Println("type: uint64")
default:
fmt.Println("type: other")
}
return 1
}

// 値ではなく型引数の指定情報を取得できるのか?
func typeParameterType[T constraints.Unsigned](input uint) T {
var t interface{} = *new(T)
switch t.(type) {
case uint8:
fmt.Println("type parameter: uint8")
var retVal uint8
return T(retVal)
case uint16:
fmt.Println("type parameter: uint16")
var retVal uint16
return T(retVal)
case uint32:
fmt.Println("type parameter: uint32")
var retVal uint32
return T(retVal)
case uint64:
fmt.Println("type parameter: uint64")
var retVal uint64
return T(retVal)
default:
fmt.Println("type parameter: other")
}
return 1
}
24 changes: 24 additions & 0 deletions cmd/newbitbuffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"fmt"

"github.com/small-teton/mpeg-ts-analyzer/bitbuffer"
)

func newBitBufferTest() {
buf := []byte{0b10101010, 0b10101010, 0b10101010, 0b10101011, 0b10101010, 0b10101010, 0b10101010, 0b10101010}
bb := new(bitbuffer.BitBuffer)
bb.Set(buf)

f, err := bitbuffer.Peek[uint16](bb, 14)
if err != nil {
fmt.Printf("err1: %v\n", err)
}
fmt.Printf("======first peek 14byte: %d, %#16b\n", f, f)
s, err := bitbuffer.Peek[uint32](bb, 21)
if err != nil {
fmt.Printf("err2: %v\n", err)
}
fmt.Printf("======second peek 21byte: %d, %#32b\n", s, s)
}
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ var rootCmd = &cobra.Command{
return
}

if flag, _ := cmd.Flags().GetBool("generic-test"); flag {
genericTest()
return
}
if flag, _ := cmd.Flags().GetBool("new-peek-sample"); flag {
newBitBufferTest()
return
}

if len(args) == 0 {
fmt.Println("input file path is not specified.")
return
Expand Down Expand Up @@ -50,6 +59,8 @@ func init() {
rootCmd.Flags().Bool("dump-pes-header", false, "Dump PES packet header detail.")
rootCmd.Flags().Bool("dump-timestamp", false, "Dump PCR/PTS/DTS timestamps.")
rootCmd.Flags().Bool("version", false, "show mpeg-ts-analyzer version.")
rootCmd.Flags().Bool("generic-test", false, "show mpeg-ts-analyzer version.")
rootCmd.Flags().Bool("new-peek-sample", false, "show mpeg-ts-analyzer version.")
}

func loadFlags(cmd *cobra.Command) options.Options {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require github.com/spf13/cobra v1.8.0
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8=
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit 6a85307

Please sign in to comment.