src/nulid/private/stew/endians2

Source   Edit  

Types

SomeEndianInt = uint8 | uint16 | uint32 | uint64
types that we support endian conversions for - uint8 is there for for syntactic / generic convenience. Other candidates:
  • int/uint - uncertain size, thus less suitable for binary interop
  • intX - over and underflow protection in nim might easily cause issues - need to consider before adding here
Source   Edit  

Procs

func fromBE[T: SomeEndianInt](x: T): T {.inline, ...raises: [].}
Read a big endian value and return the corresponding native endian Source   Edit  
func fromBytes(T: typedesc[SomeEndianInt]; x: openArray[byte];
               endian: Endianness = system.cpuEndian): T:type {.inline,
    ...raises: [].}

Read bytes and convert to an integer according to the given endianness.

Note: The default value of system.cpuEndian is not portable across machines.

Panics when x.len < sizeof(T) - for shorter buffers, copy the data to an array first using arrayops.initCopyFrom, taking care to zero-fill at the right end - usually the beginning for big endian and the end for little endian, but this depends on the serialization of the bytes.

Source   Edit  
func fromBytesBE(T: typedesc[SomeEndianInt]; x: openArray[byte]): T:type {.
    inline, ...raises: [].}
Read big endian bytes and convert to an integer. At runtime, v must contain at least sizeof(T) bytes. By default, native endianness is used which is not portable! Source   Edit  
func fromBytesLE(T: typedesc[SomeEndianInt]; x: openArray[byte]): T:type {.
    inline, ...raises: [].}
Read little endian bytes and convert to an integer. At runtime, v must contain at least sizeof(T) bytes. By default, native endianness is used which is not portable! Source   Edit  
func fromLE[T: SomeEndianInt](x: T): T {.inline, ...raises: [].}
Read a little endian value and return the corresponding native endian Source   Edit  
func swapBytes[T: SomeEndianInt](x: T): T {.inline, ...raises: [].}

Reverse the bytes within an integer, such that the most significant byte changes place with the least significant one, etc

Example: doAssert swapBytes(0x01234567'u32) == 0x67452301

Source   Edit  
func toBE[T: SomeEndianInt](x: T): T {.inline, ...raises: [].}
Convert a native endian value to big endian. Consider toBytesBE instead which may prevent some confusion. Source   Edit  
func toBytes(x: SomeEndianInt; endian: Endianness = system.cpuEndian): array[
    sizeof(x), byte] {.noinit, inline, ...raises: [].}
Convert integer to its corresponding byte sequence using the chosen endianness. By default, native endianness is used which is not portable! Source   Edit  
func toBytesBE(x: SomeEndianInt): array[sizeof(x), byte] {.inline, ...raises: [].}
Convert a native endian integer to a native endian byte sequence Source   Edit  
func toBytesLE(x: SomeEndianInt): array[sizeof(x), byte] {.inline, ...raises: [].}
Convert a native endian integer to a little endian byte sequence Source   Edit  
func toLE[T: SomeEndianInt](x: T): T {.inline, ...raises: [].}
Convert a native endian value to little endian. Consider toBytesLE instead which may prevent some confusion. Source   Edit