# halon/hsl-examples

Switch to while loops

eriklax committed Oct 19, 2016
1 parent 4d65852 commit cfa3a2775c5136778faba783b94f1d416002ac40
Showing with 32 additions and 9 deletions.
1. +4 −1 array/array_shuffle.hsl
2. +12 −4 math/bitwise.hsl
3. +4 −1 services/geoip/geoip.hsl
4. +12 −3 services/srs/srs.hsl
 @@ -1,14 +1,17 @@ // Knuth shuffle (O(n)) function array_shuffle(\$l) { foreach (range(1, count(\$l) - 1) as \$i) \$count = count(\$l); \$i = 1; while (\$i < \$count) { \$r = rand(0, \$i); if (\$r != \$i) { \$t = \$l[\$r]; \$l[\$r] = \$l[\$i]; \$l[\$i] = \$t; } \$i += 1; } return \$l; }
 @@ -1,50 +1,58 @@ function bitand(\$a, \$b) { \$result = 0; foreach (range(0, 31) as \$x) \$x = 0; while (\$x <= 31) { if (\$a % 2 and \$b % 2) \$result += 2 ** \$x; \$a = floor(\$a / 2); \$b = floor(\$b / 2); \$x += 1; } return \$result; } function bitor(\$a, \$b) { \$result = 0; foreach (range(0, 31) as \$x) \$x = 0; while (\$x <= 31) { if (\$a % 2 or \$b % 2) \$result += 2 ** \$x; \$a = floor(\$a / 2); \$b = floor(\$b / 2); \$x += 1; } return \$result; } function bitxor(\$a, \$b) { \$result = 0; foreach (range(0, 31) as \$x) \$x = 0; while (\$x <= 31) { if (\$a % 2 + \$b % 2 == 1) \$result += 2 ** \$x; \$a = floor(\$a / 2); \$b = floor(\$b / 2); \$x += 1; } return \$result; } function bitnot(\$a) { \$result = 0; foreach (range(0, 31) as \$x) \$x = 0; while (\$x <= 31) { if (\$a % 2 == 0) \$result += 2 ** \$x; \$a = floor(\$a / 2); \$x += 1; } return \$result; }
 @@ -25,15 +25,18 @@ function geoip(\$senderip) \$min = 0; \$max = count(\$lst); \$log = log(\$max, 2); foreach (range(0, \$log) as \$i) \$i = 0; while (\$i <= \$log) { \$mid = floor((\$min + \$max) / 2); if (\$ip > \$lst[\$mid][1]) { \$min = \$mid; \$i += 1; continue; } if (\$ip < \$lst[\$mid][0]) { \$max = \$mid; \$i += 1; continue; } return \$lst[\$mid][2];
 @@ -27,11 +27,13 @@ function SRS_reverse(\$address, \$opts = []) if (strlen(\$part[2]) != 2) return ""; \$days = 0; \$base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; foreach (range(0, 1) as \$i) \$i = 0; while (\$i <= 1) { \$pos = strpos(\$base32, strtoupper(\$part[2])[\$i]); if (\$pos == -1) return ""; \$days = \$days * strlen(\$base32) + \$pos; \$i += 1; } \$now = floor(time() / 86400) % (32 ** 2); if (\$now < \$days) \$now += (32 ** 2); @@ -44,20 +46,27 @@ function hmac_sha1_base64(\$key, \$message) { \$hash = hmac_sha1(\$key, \$message); \$raw = ""; foreach (range(0, strlen(\$hash) - 1, 2) as \$i) \$len = strlen(\$hash); \$i = 0; while (\$i < \$len) { \$raw .= chr(number("0x".\$hash[\$i:\$i+2])); \$i += 2; } return base64_encode(\$raw); } function bitand(\$a, \$b) { \$result = 0; foreach (range(0, 31) as \$x) \$x = 0; while (\$x <= 31) { if (\$a % 2 and \$b % 2) \$result += 2 ** \$x; \$a = floor(\$a / 2); \$b = floor(\$b / 2); \$x += 1; } return \$result; }