1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

net.conv: rename functions to match other langs, making them easier t… (#18937)

This commit is contained in:
JalonSolov 2023-07-22 02:11:01 -04:00 committed by GitHub
parent bf00ac656f
commit c3ff4b2f85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 96 additions and 60 deletions

View File

@ -112,21 +112,21 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
} }
u16 { u16 {
types << u32(Oid.t_int2) types << u32(Oid.t_int2)
num := conv.htn16(data) num := conv.hton16(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u16)) lens << int(sizeof(u16))
formats << 1 formats << 1
} }
u32 { u32 {
types << u32(Oid.t_int4) types << u32(Oid.t_int4)
num := conv.htn32(data) num := conv.hton32(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u32)) lens << int(sizeof(u32))
formats << 1 formats << 1
} }
u64 { u64 {
types << u32(Oid.t_int8) types << u32(Oid.t_int8)
num := conv.htn64(data) num := conv.hton64(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u64)) lens << int(sizeof(u64))
formats << 1 formats << 1
@ -139,21 +139,21 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
} }
i16 { i16 {
types << u32(Oid.t_int2) types << u32(Oid.t_int2)
num := conv.htn16(u16(data)) num := conv.hton16(u16(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(i16)) lens << int(sizeof(i16))
formats << 1 formats << 1
} }
int { int {
types << u32(Oid.t_int4) types << u32(Oid.t_int4)
num := conv.htn32(u32(data)) num := conv.hton32(u32(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(int)) lens << int(sizeof(int))
formats << 1 formats << 1
} }
i64 { i64 {
types << u32(Oid.t_int8) types << u32(Oid.t_int8)
num := conv.htn64(u64(data)) num := conv.hton64(u64(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(i64)) lens << int(sizeof(i64))
formats << 1 formats << 1

View File

@ -1,66 +1,102 @@
module conv module conv
// htn64 converts a the 64 bit value `host` to the net format (htonll) // htn64 - DON'T USE, use hton64 instead
[deprecated: 'use hton64() instead']
[deprecated_after: '2023-12-31']
pub fn htn64(host u64) u64 { pub fn htn64(host u64) u64 {
return hton64(host)
}
// hton64 converts the 64 bit value `host` to the net format (htonll)
pub fn hton64(host u64) u64 {
$if little_endian { $if little_endian {
// vfmt off // vfmt off
return ( return ((host >> 56) & 0x00000000_000000FF) |
((host >> 56) & 0x00000000_000000FF) | ((host >> 40) & 0x00000000_0000FF00) |
((host >> 40) & 0x00000000_0000FF00) | ((host >> 24) & 0x00000000_00FF0000) |
((host >> 24) & 0x00000000_00FF0000) | ((host >> 8) & 0x00000000_FF000000) |
((host >> 8) & 0x00000000_FF000000) | ((host << 8) & 0x000000FF_00000000) |
((host << 8) & 0x000000FF_00000000) | ((host << 24) & 0x0000FF00_00000000) |
((host << 24) & 0x0000FF00_00000000) | ((host << 40) & 0x00FF0000_00000000) |
((host << 40) & 0x00FF0000_00000000) | ((host << 56) & 0xFF000000_00000000)
((host << 56) & 0xFF000000_00000000)
)
// vfmt on // vfmt on
} $else { } $else {
return host return host
} }
} }
// htn32 converts the 32 bit value `host` to the net format (htonl) // htn32 - DON'T USE, use hton32 instead
[deprecated: 'use hton32() instead']
[deprecated_after: '2023-12-31']
pub fn htn32(host u32) u32 { pub fn htn32(host u32) u32 {
return hton32(host)
}
// hton32 converts the 32 bit value `host` to the net format (htonl)
pub fn hton32(host u32) u32 {
$if little_endian { $if little_endian {
// vfmt off // vfmt off
return ( return ((host >> 24) & 0x0000_00FF) |
((host >> 24) & 0x0000_00FF) | ((host >> 8) & 0x0000_FF00) |
((host >> 8) & 0x0000_FF00) | ((host << 8) & 0x00FF_0000) |
((host << 8) & 0x00FF_0000) | ((host << 24) & 0xFF00_0000)
((host << 24) & 0xFF00_0000)
)
// vfmt on // vfmt on
} $else { } $else {
return host return host
} }
} }
// htn16 converts the 16 bit value `host` to the net format (htons) // htn16 - DON'T USE, use hton16 instead
[deprecated: 'use hton16() instead']
[deprecated_after: '2023-12-31']
pub fn htn16(host u16) u16 { pub fn htn16(host u16) u16 {
return hton16(host)
}
// hton16 converts the 16 bit value `host` to the net format (htons)
pub fn hton16(host u16) u16 {
$if little_endian { $if little_endian {
// vfmt off // vfmt off
return ( return ((host >> 8) & 0x00FF) |
((host >> 8) & 0x00FF) | ((host << 8) & 0xFF00)
((host << 8) & 0xFF00)
)
// vfmt on // vfmt on
} $else { } $else {
return host return host
} }
} }
// nth64 converts the 64 bit value `net` to the host format (ntohll) // nth64 - DON'T USE, use ntoh64 instead
[deprecated: 'use ntoh64() instead']
[deprecated_after: '2023-12-31']
pub fn nth64(net u64) u64 { pub fn nth64(net u64) u64 {
return htn64(net) return ntoh64(net)
} }
// nth32 converts the 32 bit value `net` to the host format (ntohl) // ntoh64 converts the 64 bit value `net` to the host format (ntohll)
pub fn ntoh64(net u64) u64 {
return hton64(net)
}
// nth32 - DON'T USE, use ntoh32 instead
[deprecated: 'use ntoh32() instead']
[deprecated_after: '2023-12-31']
pub fn nth32(net u32) u32 { pub fn nth32(net u32) u32 {
return htn32(net) return ntoh32(net)
} }
// nth16 converts the 16 bit value `net` to the host format (ntohs) // ntoh32 converts the 32 bit value `net` to the host format (ntohl)
pub fn nth16(net u16) u16 { pub fn ntoh32(net u32) u32 {
return htn16(net) return hton32(net)
}
// nth16 - DON'T USE, use ntoh16 instead
[deprecated: 'use ntoh16() instead']
[deprecated_after: '2023-12-31']
pub fn nth16(net u16) u16 {
return ntoh16(net)
}
// ntoh16 converts the 16 bit value `net` to the host format (ntohs)
pub fn ntoh16(net u16) u16 {
return hton16(net)
} }

View File

@ -13,32 +13,32 @@ fn check[T](f fn (a T) T, finv fn (b T) T, x T) {
} }
} }
fn test_htn64_nth64() { fn test_hton64_ntoh64() {
assert 0 == conv.htn64(0) assert 0 == conv.hton64(0)
assert 0 == conv.nth64(0) assert 0 == conv.ntoh64(0)
assert 0xFFFF_FFFF_FFFF_FFFF == conv.nth64(0xFFFF_FFFF_FFFF_FFFF) assert 0xFFFF_FFFF_FFFF_FFFF == conv.ntoh64(0xFFFF_FFFF_FFFF_FFFF)
assert 0xFFFF_FFFF_FFFF_FFFF == conv.htn64(0xFFFF_FFFF_FFFF_FFFF) assert 0xFFFF_FFFF_FFFF_FFFF == conv.hton64(0xFFFF_FFFF_FFFF_FFFF)
for x in [u64(1), 2, 128, 65536, 2147483648] { for x in [u64(1), 2, 128, 65536, 2147483648] {
check(conv.htn64, conv.nth64, x) check(conv.hton64, conv.ntoh64, x)
} }
} }
fn test_htn32_nth32() { fn test_hton32_ntoh32() {
assert 0 == conv.htn32(0) assert 0 == conv.hton32(0)
assert 0 == conv.nth32(0) assert 0 == conv.ntoh32(0)
assert 0xFFFF_FFFF == conv.nth32(0xFFFF_FFFF) assert 0xFFFF_FFFF == conv.ntoh32(0xFFFF_FFFF)
assert 0x0101_0101 == conv.htn32(0x0101_0101) assert 0x0101_0101 == conv.hton32(0x0101_0101)
for x in [u32(1), 2, 128, 65536, 2147483648] { for x in [u32(1), 2, 128, 65536, 2147483648] {
check(conv.htn32, conv.nth32, x) check(conv.hton32, conv.ntoh32, x)
} }
} }
fn test_htn16_nth16() { fn test_hton16_ntoh16() {
assert 0 == conv.htn16(0) assert 0 == conv.hton16(0)
assert 0 == conv.nth16(0) assert 0 == conv.ntoh16(0)
assert 0xFFFF == conv.nth16(0xFFFF) assert 0xFFFF == conv.ntoh16(0xFFFF)
assert 0x0101 == conv.htn16(0x0101) assert 0x0101 == conv.hton16(0x0101)
for x in [u16(1), 2, 128, 65534] { for x in [u16(1), 2, 128, 65534] {
check(conv.htn16, conv.nth16, x) check(conv.hton16, conv.ntoh16, x)
} }
} }

View File

@ -105,21 +105,21 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
} }
u16 { u16 {
types << u32(Oid.t_int2) types << u32(Oid.t_int2)
num := conv.htn16(data) num := conv.hton16(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u16)) lens << int(sizeof(u16))
formats << 1 formats << 1
} }
u32 { u32 {
types << u32(Oid.t_int4) types << u32(Oid.t_int4)
num := conv.htn32(data) num := conv.hton32(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u32)) lens << int(sizeof(u32))
formats << 1 formats << 1
} }
u64 { u64 {
types << u32(Oid.t_int8) types << u32(Oid.t_int8)
num := conv.htn64(data) num := conv.hton64(data)
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(u64)) lens << int(sizeof(u64))
formats << 1 formats << 1
@ -132,21 +132,21 @@ fn pg_stmt_match(mut types []u32, mut vals []&char, mut lens []int, mut formats
} }
i16 { i16 {
types << u32(Oid.t_int2) types << u32(Oid.t_int2)
num := conv.htn16(u16(data)) num := conv.hton16(u16(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(i16)) lens << int(sizeof(i16))
formats << 1 formats << 1
} }
int { int {
types << u32(Oid.t_int4) types << u32(Oid.t_int4)
num := conv.htn32(u32(data)) num := conv.hton32(u32(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(int)) lens << int(sizeof(int))
formats << 1 formats << 1
} }
i64 { i64 {
types << u32(Oid.t_int8) types << u32(Oid.t_int8)
num := conv.htn64(u64(data)) num := conv.hton64(u64(data))
vals << &char(&num) vals << &char(&num)
lens << int(sizeof(i64)) lens << int(sizeof(i64))
formats << 1 formats << 1