init
This commit is contained in:
56
lib/internal.nix
Normal file
56
lib/internal.nix
Normal file
@@ -0,0 +1,56 @@
|
||||
{ lib }:
|
||||
let
|
||||
inherit (builtins) elemAt genList;
|
||||
inherit (lib) mod;
|
||||
in
|
||||
rec {
|
||||
# Power of 2 calculation
|
||||
# pow2 n returns 2^n
|
||||
pow2 =
|
||||
n:
|
||||
if n == 0 then
|
||||
1
|
||||
else if n == 1 then
|
||||
2
|
||||
else
|
||||
let
|
||||
half = n / 2;
|
||||
halfPow = pow2 half;
|
||||
in
|
||||
halfPow * halfPow * (if mod n 2 == 1 then 2 else 1);
|
||||
|
||||
# Convert list of 4 octets to 32-bit integer
|
||||
# [192 168 1 1] -> 3232235777
|
||||
octetsToInt =
|
||||
octets:
|
||||
let
|
||||
a = elemAt octets 0;
|
||||
b = elemAt octets 1;
|
||||
c = elemAt octets 2;
|
||||
d = elemAt octets 3;
|
||||
in
|
||||
a * 16777216 + b * 65536 + c * 256 + d;
|
||||
|
||||
# Convert 32-bit integer to list of 4 octets
|
||||
# 3232235777 -> [192 168 1 1]
|
||||
intToOctets =
|
||||
ip:
|
||||
let
|
||||
a = ip / 16777216;
|
||||
remainder1 = mod ip 16777216;
|
||||
b = remainder1 / 65536;
|
||||
remainder2 = mod remainder1 65536;
|
||||
c = remainder2 / 256;
|
||||
d = mod remainder2 256;
|
||||
in
|
||||
[
|
||||
a
|
||||
b
|
||||
c
|
||||
d
|
||||
];
|
||||
|
||||
# Generate a list of integers from start to end (inclusive)
|
||||
# More efficient than recursive approach for large ranges
|
||||
intRange = start: end: if end < start then [ ] else genList (i: start + i) (end - start + 1);
|
||||
}
|
||||
Reference in New Issue
Block a user