-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Goのシリアライザの32bit対応 #77
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,31 +201,31 @@ func unmarshalShort(src []byte) (int, int, error) { | |
} | ||
|
||
// MarshalUInt marshals unsigned 32bit integer | ||
func MarshalUInt(val int) []byte { | ||
func MarshalUInt(val int64) []byte { | ||
v := clamp(int64(val), 0, math.MaxUint32) | ||
buf := make([]byte, 1+UIntDataSize) | ||
buf[0] = byte(TypeUInt) | ||
put32(buf[1:], v) | ||
return buf | ||
} | ||
|
||
func unmarshalUInt(src []byte) (int, int, error) { | ||
func unmarshalUInt(src []byte) (int64, int, error) { | ||
if len(src) < 1+UIntDataSize { | ||
return 0, 0, xerrors.Errorf("Unmarshal UInt error: not enough data (%v)", len(src)) | ||
} | ||
return get32(src[1:]), 1 + UIntDataSize, nil | ||
} | ||
|
||
// MarshalInt marshals signed 32bit integer comparably | ||
func MarshalInt(val int) []byte { | ||
func MarshalInt(val int64) []byte { | ||
v := clamp(int64(val), math.MinInt32, math.MaxInt32) | ||
buf := make([]byte, 1+IntDataSize) | ||
buf[0] = byte(TypeInt) | ||
put32(buf[1:], v-math.MinInt32) | ||
return buf | ||
} | ||
|
||
func unmarshalInt(src []byte) (int, int, error) { | ||
func unmarshalInt(src []byte) (int64, int, error) { | ||
if len(src) < 1+IntDataSize { | ||
return 0, 0, xerrors.Errorf("Unmarshal Int error: not enough data (%v)", len(src)) | ||
} | ||
|
@@ -773,7 +773,7 @@ func unmarshalUShorts(src []byte) ([]int, int, error) { | |
// - TypeInts | ||
// - 16bit count | ||
// - repeat: 32bit BE integer... | ||
func MarshalInts(vals []int) []byte { | ||
func MarshalInts(vals []int64) []byte { | ||
if vals == nil { | ||
return MarshalNull() | ||
} | ||
|
@@ -787,14 +787,14 @@ func MarshalInts(vals []int) []byte { | |
put16(buf[1:], int64(count)) | ||
|
||
for i := 0; i < count; i++ { | ||
v := clamp(int64(vals[i]), math.MinInt32, math.MaxInt32) - math.MinInt32 | ||
v := clamp(vals[i], math.MinInt32, math.MaxInt32) - math.MinInt32 | ||
put32(buf[3+i*IntDataSize:], v) | ||
} | ||
|
||
return buf | ||
} | ||
|
||
func unmarshalInts(src []byte) ([]int, int, error) { | ||
func unmarshalInts(src []byte) ([]int64, int, error) { | ||
if len(src) < 3 { | ||
return nil, 0, xerrors.Errorf("Unmarshal Intts error: not enough data (%v)", len(src)) | ||
} | ||
|
@@ -803,7 +803,7 @@ func unmarshalInts(src []byte) ([]int, int, error) { | |
if len(src) < l { | ||
return nil, 0, xerrors.Errorf("Unmarshal Ints error: not enough data (%v)", len(src)) | ||
} | ||
vals := make([]int, count) | ||
vals := make([]int64, count) | ||
for i := 0; i < count; i++ { | ||
vals[i] = get32(src[3+i*IntDataSize:]) + math.MinInt32 | ||
} | ||
|
@@ -815,7 +815,7 @@ func unmarshalInts(src []byte) ([]int, int, error) { | |
// - TypeUInts | ||
// - 16bit count | ||
// - repeat: 32bit BE integer... | ||
func MarshalUInts(vals []int) []byte { | ||
func MarshalUInts(vals []int64) []byte { | ||
if vals == nil { | ||
return MarshalNull() | ||
} | ||
|
@@ -829,14 +829,14 @@ func MarshalUInts(vals []int) []byte { | |
put16(buf[1:], int64(count)) | ||
|
||
for i := 0; i < count; i++ { | ||
v := clamp(int64(vals[i]), 0, math.MaxUint32) | ||
v := clamp(vals[i], 0, math.MaxUint32) | ||
put32(buf[3+i*UIntDataSize:], v) | ||
} | ||
|
||
return buf | ||
} | ||
|
||
func unmarshalUInts(src []byte) ([]int, int, error) { | ||
func unmarshalUInts(src []byte) ([]int64, int, error) { | ||
if len(src) < 3 { | ||
return nil, 0, xerrors.Errorf("Unmarshal UInts error: not enough data (%v)", len(src)) | ||
} | ||
|
@@ -845,7 +845,7 @@ func unmarshalUInts(src []byte) ([]int, int, error) { | |
if len(src) < l { | ||
return nil, 0, xerrors.Errorf("Unmarshal UInts error: not enough data (%v)", len(src)) | ||
} | ||
vals := make([]int, count) | ||
vals := make([]int64, count) | ||
for i := 0; i < count; i++ { | ||
vals[i] = get32(src[3+i*UIntDataSize:]) | ||
} | ||
|
@@ -1182,62 +1182,63 @@ func get8(src []byte) int { | |
} | ||
|
||
func put16(dst []byte, val int64) { | ||
dst[0] = byte((val & 0xff00) >> 8) | ||
dst[1] = byte(val & 0xff) | ||
dst[0] = byte((val & 0xff00) >> 8) | ||
} | ||
|
||
func get16(src []byte) int { | ||
v := int(src[0]) << 8 | ||
v += int(src[1]) | ||
v := int(src[1]) | ||
v |= int(src[0]) << 8 | ||
return v | ||
} | ||
|
||
func put24(dst []byte, val int64) { | ||
dst[0] = byte((val & 0xff0000) >> 16) | ||
dst[1] = byte((val & 0xff00) >> 8) | ||
dst[2] = byte(val & 0xff) | ||
dst[1] = byte((val & 0xff00) >> 8) | ||
dst[0] = byte((val & 0xff0000) >> 16) | ||
} | ||
|
||
func get24(src []byte) int { | ||
i := int(src[0]) << 16 | ||
i += int(src[1]) << 8 | ||
i += int(src[2]) | ||
i := int(src[2]) | ||
i |= int(src[1]) << 8 | ||
i |= int(src[0]) << 16 | ||
return i | ||
} | ||
|
||
func put32(dst []byte, val int64) { | ||
dst[0] = byte((val & 0xff000000) >> 24) | ||
dst[1] = byte((val & 0xff0000) >> 16) | ||
dst[2] = byte((val & 0xff00) >> 8) | ||
dst[3] = byte(val & 0xff) | ||
dst[2] = byte((val & 0xff00) >> 8) | ||
dst[1] = byte((val & 0xff0000) >> 16) | ||
dst[0] = byte((val & 0xff000000) >> 24) | ||
} | ||
func get32(src []byte) int { | ||
i := int(src[0]) << 24 | ||
i += int(src[1]) << 16 | ||
i += int(src[2]) << 8 | ||
i += int(src[3]) | ||
|
||
func get32(src []byte) int64 { | ||
i := int64(src[3]) | ||
i |= int64(src[2]) << 8 | ||
i |= int64(src[1]) << 16 | ||
i |= int64(src[0]) << 24 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return int64(binary.BigEndian.Uint32(src)) |
||
return i | ||
} | ||
|
||
func put64(dst []byte, val uint64) { | ||
dst[0] = byte((val & 0xff00000000000000) >> 56) | ||
dst[1] = byte((val & 0xff000000000000) >> 48) | ||
dst[2] = byte((val & 0xff0000000000) >> 40) | ||
dst[3] = byte((val & 0xff00000000) >> 32) | ||
dst[4] = byte((val & 0xff000000) >> 24) | ||
dst[5] = byte((val & 0xff0000) >> 16) | ||
dst[6] = byte((val & 0xff00) >> 8) | ||
dst[7] = byte(val & 0xff) | ||
dst[6] = byte((val & 0xff00) >> 8) | ||
dst[5] = byte((val & 0xff0000) >> 16) | ||
dst[4] = byte((val & 0xff000000) >> 24) | ||
dst[3] = byte((val & 0xff00000000) >> 32) | ||
dst[2] = byte((val & 0xff0000000000) >> 40) | ||
dst[1] = byte((val & 0xff000000000000) >> 48) | ||
dst[0] = byte((val & 0xff00000000000000) >> 56) | ||
} | ||
|
||
func get64(src []byte) uint64 { | ||
i := uint64(src[0]) << 56 | ||
i += uint64(src[1]) << 48 | ||
i += uint64(src[2]) << 40 | ||
i += uint64(src[3]) << 32 | ||
i += uint64(src[4]) << 24 | ||
i += uint64(src[5]) << 16 | ||
i += uint64(src[6]) << 8 | ||
i += uint64(src[7]) | ||
i := uint64(src[7]) | ||
i |= uint64(src[6]) << 8 | ||
i |= uint64(src[5]) << 16 | ||
i |= uint64(src[4]) << 24 | ||
i |= uint64(src[3]) << 32 | ||
i |= uint64(src[2]) << 40 | ||
i |= uint64(src[1]) << 48 | ||
i |= uint64(src[0]) << 56 | ||
return i | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
最適化を標準ライブラリに任せたほうがいいですねたしかに。