1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-26 01:15:38 +00:00

BytesLiteral.All()

This commit is contained in:
Darien Raymond 2016-01-21 11:52:09 +00:00
parent c78460df1e
commit 85feb725a5
3 changed files with 7 additions and 13 deletions

View File

@ -4,6 +4,7 @@ import (
"net"
"github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/common/serial"
)
// Address represents a network address to be communicated with. It may be an IP address or domain
@ -28,15 +29,6 @@ func ParseAddress(addr string) Address {
return DomainAddress(addr)
}
func allZeros(data []byte) bool {
for _, v := range data {
if v != 0 {
return false
}
}
return true
}
// IPAddress creates an Address with given IP and port.
func IPAddress(ip []byte) Address {
switch len(ip) {
@ -44,7 +36,7 @@ func IPAddress(ip []byte) Address {
var addr IPv4Address = [4]byte{ip[0], ip[1], ip[2], ip[3]}
return &addr
case net.IPv6len:
if allZeros(ip[0:10]) && ip[10] == 0xff && ip[11] == 0xff {
if serial.BytesLiteral(ip[0:10]).All(0) && serial.BytesLiteral(ip[10:12]).All(0xff) {
return IPAddress(ip[12:16])
}
var addr IPv6Address = [16]byte{

View File

@ -22,13 +22,15 @@ func (this BytesLiteral) Int64Value() int64 {
int64(value[7])
}
// String returns a string presentation of this ByteLiteral
func (this BytesLiteral) String() string {
return string(this.Value())
}
func (this BytesLiteral) AllZero() bool {
// All returns true if all bytes in the ByteLiteral are the same as given value.
func (this BytesLiteral) All(v byte) bool {
for _, b := range this {
if b != 0 {
if b != v {
return false
}
}

View File

@ -14,5 +14,5 @@ func TestCmdKey(t *testing.T) {
v2testing.Current(t)
id := NewID(uuid.New())
assert.Bool(serial.BytesLiteral(id.CmdKey()).AllZero()).IsFalse()
assert.Bool(serial.BytesLiteral(id.CmdKey()).All(0)).IsFalse()
}