Skip to content

Commit

Permalink
strcast
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Aug 24, 2023
1 parent 0129a7a commit fadfc42
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) schwarzlichtbezirk, 2021
Copyright (c) schwarzlichtbezirk, 2021-2023

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ require (
gopkg.in/djherbis/times.v1 v1.3.0
)

require golang.org/x/sys v0.10.0 // indirect
require golang.org/x/sys v0.11.0 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg=
github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY=
github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE=
github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/djherbis/times.v1 v1.3.0 h1:uxMS4iMtH6Pwsxog094W0FYldiNnfY/xba00vq6C2+o=
gopkg.in/djherbis/times.v1 v1.3.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8=
2 changes: 1 addition & 1 deletion luawpk/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func CheckPackage(t *testing.T, wptname, wpfname string) {
}

var orig []byte
if orig, err = os.ReadFile(mediadir + string(link)); err != nil {
if orig, err = os.ReadFile(mediadir + wpk.B2S(link)); err != nil {
t.Fatal(err)
}

Expand Down
19 changes: 19 additions & 0 deletions strcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package wpk

import (
"reflect"
"unsafe"
)

func B2S(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}

func S2B(s string) (b []byte) {
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Cap = sh.Len
bh.Len = sh.Len
return b
}
2 changes: 1 addition & 1 deletion tagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TagRaw []byte

// TagStr tag converter.
func (t TagRaw) TagStr() (string, bool) {
return string(t), true
return B2S(t), true
}

// StrTag is string tag constructor.
Expand Down
4 changes: 2 additions & 2 deletions wpk.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,14 @@ type Header struct {
// IsReady determines that package is ready for read the data.
func (hdr *Header) IsReady() error {
// can not read file tags table for opened on write single-file package.
if string(hdr.signature[:]) == SignBuild {
if B2S(hdr.signature[:]) == SignBuild {
if hdr.datoffset != 0 {
return ErrSignPre
}
return nil
}
// can not read file tags table on any incorrect signature
if string(hdr.signature[:]) != SignReady {
if B2S(hdr.signature[:]) != SignReady {
return ErrSignBad
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions wpk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var testpkgt = wpk.TempPath("testpack.wpt")
var testpkgf = wpk.TempPath("testpack.wpf")

var memdata = map[string][]byte{
"sample.txt": []byte("The quick brown fox jumps over the lazy dog"),
"sample.txt": wpk.S2B("The quick brown fox jumps over the lazy dog"),
"array.dat": {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
Expand Down Expand Up @@ -71,7 +71,7 @@ func CheckPackage(t *testing.T, fwpt, fwpf *os.File, tagsnum int) {

var orig []byte
if isfile {
if orig, err = os.ReadFile(mediadir + string(link)); err != nil {
if orig, err = os.ReadFile(mediadir + wpk.B2S(link)); err != nil {
t.Fatal(err)
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (ftt *FTT) Append(wpt, wpf io.WriteSeeker) (err error) {
return
}
// rewrite prebuild signature
if err = binary.Write(wpt, binary.LittleEndian, []byte(SignBuild)); err != nil {
if err = binary.Write(wpt, binary.LittleEndian, S2B(SignBuild)); err != nil {
return
}
// go to tags table start to replace it by new data
Expand Down

0 comments on commit fadfc42

Please sign in to comment.