Skip to content

Commit

Permalink
Update convertors.
Browse files Browse the repository at this point in the history
  • Loading branch information
SyoujyoujiNaiki committed Jan 3, 2025
1 parent 735a4b2 commit 5aa9b38
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 84 deletions.
24 changes: 12 additions & 12 deletions builtin/bytesview.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,23 @@ pub fn iter(self : BytesView) -> Iter[Byte] {
})
}

///|
///| Converts the 4 bytes long BytesView to a UInt in big-endian byte order.
pub fn to_uint_be(self : BytesView) -> UInt {
(self[0].to_uint() << 24) +
(self[1].to_uint() << 16) +
(self[2].to_uint() << 8) +
self[3].to_uint()
}

///|
///| Converts the 4 bytes long BytesView to a UInt in little-endian byte order.
pub fn to_uint_le(self : BytesView) -> UInt {
self[0].to_uint() +
(self[1].to_uint() << 8) +
(self[2].to_uint() << 16) +
(self[3].to_uint() << 24)
}

///|
///| Converts the 8 bytes long BytesView to a UInt64 in big-endian byte order.
pub fn to_uint64_be(self : BytesView) -> UInt64 {
(self[0].to_uint().to_uint64() << 56) +
(self[1].to_uint().to_uint64() << 48) +
Expand All @@ -152,7 +152,7 @@ pub fn to_uint64_be(self : BytesView) -> UInt64 {
self[7].to_uint().to_uint64()
}

///|
///| Converts the 8 bytes long BytesView to a UInt64 in little-endian byte order.
pub fn to_uint64_le(self : BytesView) -> UInt64 {
self[0].to_uint().to_uint64() +
(self[1].to_uint().to_uint64() << 8) +
Expand All @@ -164,42 +164,42 @@ pub fn to_uint64_le(self : BytesView) -> UInt64 {
(self[7].to_uint().to_uint64() << 56)
}

///|
///| Converts the 4 bytes long BytesView to a Int in big-endian byte order.
pub fn to_int_be(self : BytesView) -> Int {
self.to_uint_be().reinterpret_as_int()
}

///|
///| Converts the 4 bytes long BytesView to a Int in little-endian byte order.
pub fn to_int_le(self : BytesView) -> Int {
self.to_uint_le().reinterpret_as_int()
}

///|
///| Converts the 8 bytes long BytesView to a Int64 in big-endian byte order.
pub fn to_int64_be(self : BytesView) -> Int64 {
self.to_uint64_be().reinterpret_as_int64()
}

///|
///| Converts the 8 bytes long BytesView to a Int64 in little-endian byte order.
pub fn to_int64_le(self : BytesView) -> Int64 {
self.to_uint64_le().reinterpret_as_int64()
}

///|
///| Converts the 4 bytes long BytesView to a Float in big-endian byte order.
pub fn to_float_be(self : BytesView) -> Float {
self.to_uint_be().reinterpret_as_float()
}

///|
///| Converts the 4 bytes long BytesView to a Float in little-endian byte order.
pub fn to_float_le(self : BytesView) -> Float {
self.to_uint_le().reinterpret_as_float()
}

///|
///| Converts the 8 bytes long BytesView to a Double in big-endian byte order.
pub fn to_double_be(self : BytesView) -> Double {
self.to_uint64_be().reinterpret_as_double()
}

///|
///| Converts the 8 bytes long BytesView to a Double in little-endian byte order.
pub fn to_double_le(self : BytesView) -> Double {
self.to_uint64_le().reinterpret_as_double()
}
12 changes: 6 additions & 6 deletions double/double.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ pub fn is_close(
diff <= absolute_tolerance
}

///|
pub fn to_be_bytes(self : Double) -> BytesView {
self.reinterpret_as_uint64().to_be_bytes_view()
///| Converts the Double to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_be_bytes()
}

///|
pub fn to_le_bytes(self : Double) -> BytesView {
self.reinterpret_as_uint64().to_le_bytes_view()
///| Converts the Double to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Double) -> Bytes {
self.reinterpret_as_uint64().to_le_bytes()
}
4 changes: 2 additions & 2 deletions double/double.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ impl Double {
pow(Double, Double) -> Double
round(Double) -> Double
signum(Double) -> Double
to_be_bytes(Double) -> BytesView
to_le_bytes(Double) -> BytesView
to_be_bytes(Double) -> Bytes
to_le_bytes(Double) -> Bytes
to_string(Double) -> String
trunc(Double) -> Double
}
Expand Down
8 changes: 4 additions & 4 deletions float/float.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ pub impl Hash for Float with hash_combine(self, hasher) {
hasher.combine_float(self)
}

///|
pub fn to_be_bytes_view(self : Float) -> BytesView {
///| Converts the Float to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Float) -> Bytes {
self.reinterpret_as_uint().to_be_bytes()
}

///|
pub fn to_le_bytes_view(self : Float) -> BytesView {
///| Converts the Float to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Float) -> Bytes {
self.reinterpret_as_uint().to_le_bytes()
}
4 changes: 2 additions & 2 deletions float/float.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl Float {
op_mod(Float, Float) -> Float
pow(Float, Float) -> Float
round(Float) -> Float
to_be_bytes_view(Float) -> BytesView
to_be_bytes(Float) -> Bytes
to_int(Float) -> Int
to_le_bytes_view(Float) -> BytesView
to_le_bytes(Float) -> Bytes
trunc(Float) -> Float
}

Expand Down
8 changes: 4 additions & 4 deletions int/int.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub fn abs(self : Int) -> Int {
}
}

///|
pub fn to_be_bytes(self : Int) -> BytesView {
///| Converts the Int to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Int) -> Bytes {
self.reinterpret_as_uint().to_be_bytes()
}

///|
pub fn to_le_bytes(self : Int) -> BytesView {
///| Converts the Int to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Int) -> Bytes {
self.reinterpret_as_uint().to_le_bytes()
}
4 changes: 2 additions & 2 deletions int/int.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ let min_value : Int

impl Int {
abs(Int) -> Int
to_be_bytes(Int) -> BytesView
to_le_bytes(Int) -> BytesView
to_be_bytes(Int) -> Bytes
to_le_bytes(Int) -> Bytes
}

// Type aliases
Expand Down
12 changes: 6 additions & 6 deletions int64/int64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ pub fn abs(self : Int64) -> Int64 {
}
}

///|
pub fn to_be_bytes_view(self : Int64) -> BytesView {
self.reinterpret_as_uint64().to_be_bytes_view()
///| Converts the Int64 to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : Int64) -> Bytes {
self.reinterpret_as_uint64().to_be_bytes()
}

///|
pub fn to_le_bytes_view(self : Int64) -> BytesView {
self.reinterpret_as_uint64().to_le_bytes_view()
///| Converts the Int64 to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : Int64) -> Bytes {
self.reinterpret_as_uint64().to_le_bytes()
}
4 changes: 2 additions & 2 deletions int64/int64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ let min_value : Int64
impl Int64 {
abs(Int64) -> Int64
from_int(Int) -> Int64
to_be_bytes_view(Int64) -> BytesView
to_le_bytes_view(Int64) -> BytesView
to_be_bytes(Int64) -> Bytes
to_le_bytes(Int64) -> Bytes
}

// Type aliases
Expand Down
32 changes: 16 additions & 16 deletions uint/uint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ pub fn UInt::default() -> UInt {
0
}

///|
pub fn to_be_bytes(self : UInt) -> BytesView {
let bs = Bytes::make(4, 0)
bs[0] = (self >> 24).to_byte()
bs[1] = (self >> 16).to_byte()
bs[2] = (self >> 8).to_byte()
bs[3] = self.to_byte()
bs[:]
///| Converts the UInt to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : UInt) -> Bytes {
[
(self >> 24).to_byte(),
(self >> 16).to_byte(),
(self >> 8).to_byte(),
self.to_byte(),
]
}

///|
pub fn to_le_bytes(self : UInt) -> BytesView {
let bs = Bytes::make(4, 0)
bs[0] = self.to_byte()
bs[1] = (self >> 8).to_byte()
bs[2] = (self >> 16).to_byte()
bs[3] = (self >> 24).to_byte()
bs[:]
///| Converts the UInt to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : UInt) -> Bytes {
[
self.to_byte(),
(self >> 8).to_byte(),
(self >> 16).to_byte(),
(self >> 24).to_byte(),
]
}
4 changes: 2 additions & 2 deletions uint/uint.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ let min_value : UInt

impl UInt {
default() -> UInt
to_be_bytes(UInt) -> BytesView
to_be_bytes(UInt) -> Bytes
to_int64(UInt) -> Int64
to_le_bytes(UInt) -> BytesView
to_le_bytes(UInt) -> Bytes
}

// Type aliases
Expand Down
48 changes: 24 additions & 24 deletions uint64/uint64.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ pub let min_value : UInt64 = 0UL
///|
pub let max_value : UInt64 = 18446744073709551615UL

///|
pub fn to_be_bytes_view(self : UInt64) -> BytesView {
let bs = Bytes::make(8, 0)
bs[0] = (self >> 56).to_byte()
bs[1] = (self >> 48).to_byte()
bs[2] = (self >> 40).to_byte()
bs[3] = (self >> 32).to_byte()
bs[4] = (self >> 24).to_byte()
bs[5] = (self >> 16).to_byte()
bs[6] = (self >> 8).to_byte()
bs[7] = self.to_byte()
bs[:]
///| Converts the UInt64 to a Bytes in big-endian byte order.
pub fn to_be_bytes(self : UInt64) -> Bytes {
[
(self >> 56).to_byte(),
(self >> 48).to_byte(),
(self >> 40).to_byte(),
(self >> 32).to_byte(),
(self >> 24).to_byte(),
(self >> 16).to_byte(),
(self >> 8).to_byte(),
self.to_byte(),
]
}

///|
pub fn to_le_bytes_view(self : UInt64) -> BytesView {
let bs = Bytes::make(8, 0)
bs[0] = self.to_byte()
bs[1] = (self >> 8).to_byte()
bs[2] = (self >> 16).to_byte()
bs[3] = (self >> 24).to_byte()
bs[4] = (self >> 32).to_byte()
bs[5] = (self >> 40).to_byte()
bs[6] = (self >> 48).to_byte()
bs[7] = (self >> 56).to_byte()
bs[:]
///| Converts the UInt64 to a Bytes in little-endian byte order.
pub fn to_le_bytes(self : UInt64) -> Bytes {
[
self.to_byte(),
(self >> 8).to_byte(),
(self >> 16).to_byte(),
(self >> 24).to_byte(),
(self >> 32).to_byte(),
(self >> 40).to_byte(),
(self >> 48).to_byte(),
(self >> 56).to_byte(),
]
}
4 changes: 2 additions & 2 deletions uint64/uint64.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ let min_value : UInt64


impl UInt64 {
to_be_bytes_view(UInt64) -> BytesView
to_le_bytes_view(UInt64) -> BytesView
to_be_bytes(UInt64) -> Bytes
to_le_bytes(UInt64) -> Bytes
}

// Type aliases
Expand Down

0 comments on commit 5aa9b38

Please sign in to comment.