Type signatures + porting Zalgo from Ayase

This commit is contained in:
flash 2017-12-16 20:17:29 +01:00
parent a8de88b877
commit db12ccd25b
5 changed files with 148 additions and 3 deletions

View file

@ -0,0 +1,4 @@
<?php
namespace Misuzu;
require_once 'vendor/autoload.php';

4
public/index.php Normal file
View file

@ -0,0 +1,4 @@
<?php
namespace Misuzu;
require_once __DIR__ . '/../misuzu.php';

2
public/robots.txt Normal file
View file

@ -0,0 +1,2 @@
User-agent: *
Disallow:

124
src/Zalgo.php Normal file
View file

@ -0,0 +1,124 @@
<?php
namespace Misuzu;
class Zalgo {
private const ZALGO_CHARS_UP = [
"\u{030d}", "\u{030e}", "\u{0304}", "\u{0305}", "\u{033f}",
"\u{0311}", "\u{0306}", "\u{0310}", "\u{0352}", "\u{0357}",
"\u{0351}", "\u{0307}", "\u{0308}", "\u{030a}", "\u{0342}",
"\u{0344}", "\u{034a}", "\u{034b}", "\u{034c}", "\u{0303}",
"\u{0302}", "\u{030c}", "\u{0350}", "\u{0300}", "\u{0301}",
"\u{030b}", "\u{030f}", "\u{0312}", "\u{0313}", "\u{0314}",
"\u{033d}", "\u{0309}", "\u{0363}", "\u{0364}", "\u{0365}",
"\u{0366}", "\u{0367}", "\u{0368}", "\u{0369}", "\u{036a}",
"\u{036b}", "\u{036c}", "\u{036d}", "\u{036e}", "\u{036f}",
"\u{033e}", "\u{035b}", "\u{0346}", "\u{031a}",
];
private const ZALGO_CHARS_DOWN = [
"\u{0316}", "\u{0317}", "\u{0318}", "\u{0319}", "\u{031c}",
"\u{031d}", "\u{031e}", "\u{031f}", "\u{0320}", "\u{0324}",
"\u{0325}", "\u{0326}", "\u{0329}", "\u{032a}", "\u{032b}",
"\u{032c}", "\u{032d}", "\u{032e}", "\u{032f}", "\u{0330}",
"\u{0331}", "\u{0332}", "\u{0333}", "\u{0339}", "\u{033a}",
"\u{033b}", "\u{033c}", "\u{0345}", "\u{0347}", "\u{0348}",
"\u{0349}", "\u{034d}", "\u{034e}", "\u{0353}", "\u{0354}",
"\u{0355}", "\u{0356}", "\u{0359}", "\u{035a}", "\u{0323}",
];
private const ZALGO_CHARS_MIDDLE = [
"\u{0315}", "\u{031b}", "\u{0340}", "\u{0341}", "\u{0358}",
"\u{0321}", "\u{0322}", "\u{0327}", "\u{0328}", "\u{0334}",
"\u{0335}", "\u{0336}", "\u{034f}", "\u{035c}", "\u{035d}",
"\u{035e}", "\u{035f}", "\u{0360}", "\u{0362}", "\u{0338}",
"\u{0337}", "\u{0361}", "\u{0489}",
];
public const ZALGO_MODE_MINI = 1;
public const ZALGO_MODE_NORMAL = 2;
public const ZALGO_MODE_MAX = 3;
private const ZALGO_MODES = [
ZALGO_MODE_MINI,
ZALGO_MODE_NORMAL,
ZALGO_MODE_MAX,
];
public const ZALGO_DIR_UP = 0x1;
public const ZALGO_DIR_MID = 0x2;
public const ZALGO_DIR_DOWN = 0x4;
public static function strip(string $text): string {
$text = str_replace(self::ZALGO_CHARS_UP, '', $text);
$text = str_replace(self::ZALGO_CHARS_MIDDLE, '', $text);
$text = str_replace(self::ZALGO_CHARS_DOWN, '', $text);
return $text;
}
private static function isZalgo(string $char): bool {
return in_array($char, self::ZALGO_CHARS_UP)
|| in_array($char, self::ZALGO_CHARS_MIDDLE)
|| in_array($char, self::ZALGO_CHARS_DOWN);
}
private static function getZalgo(array $array, int $length): string {
$string = '';
for ($i = 0; $i < $length; $i++)
$string .= array_rand_value($array, '');
return $string;
}
public static function run(string $text, int $mode = self::ZALGO_MODE_MINI, int $direction = self::ZALGO_DIR_MID | self::ZALGO_DIR_DOWN): string {
$text_length = strlen($text);
if (!$text_length || !$mode || !$direction || !in_array($mode, self::ZALGO_MODES))
return $text;
$going_up = has_flag($direction, self::ZALGO_DIR_UP);
$going_mid = has_flag($direction, self::ZALGO_DIR_MID);
$going_down = has_flag($direction, self::ZALGO_DIR_DOWN);
if (!$going_up || !$going_mid || !$going_down)
return $text;
$str = '';
for ($i = 0; $i < $text_length; $i++) {
$char = $text[$i];
if (self::isZalgo($char))
continue;
$str .= $char;
switch ($mode) {
case self::ZALGO_MODE_MINI:
$num_up = mt_rand(0, 8);
$num_mid = mt_rand(0, 2);
$num_down = mt_rand(0, 8);
break;
case self::ZALGO_MODE_NORMAL:
$num_up = mt_rand(0, 16) / 2 + 1;
$num_mid = mt_rand(0, 6) / 2;
$num_down = mt_rand(0, 8) / 2 + 1;
break;
case self::ZALGO_MODE_MAX:
$num_up = mt_rand(0, 64) / 4 + 3;
$num_mid = mt_rand(0, 16) / 4 + 1;
$num_down = mt_rand(0, 64) / 4 + 3;
break;
}
if ($going_up) $str .= self::getZalgo(self::ZALGO_CHARS_UP, $num_up);
if ($going_mid) $str .= self::getZalgo(self::ZALGO_CHARS_MIDDLE, $num_mid);
if ($going_down) $str .= self::getZalgo(self::ZALGO_CHARS_DOWN, $num_down);
}
return $str;
}
}

View file

@ -2,17 +2,28 @@
// both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless // both of these are provided by illuminate/database already but i feel like it makes sense to add these definitions regardless
if (!function_exists('starts_with')) { if (!function_exists('starts_with')) {
function starts_with($string, $text) { function starts_with(string $string, string $text): bool {
return substr($string, 0, strlen($text)) === $text; return substr($string, 0, strlen($text)) === $text;
} }
} }
if (!function_exists('ends_with')) { if (!function_exists('ends_with')) {
function ends_with($string, $text) { function ends_with(string $string, string $text): bool {
return substr($string, 0 - strlen($text)) === $text; return substr($string, 0 - strlen($text)) === $text;
} }
} }
function dechex_pad($value, $padding = 2) { function dechex_pad(int $value, int $padding = 2): string {
return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT); return str_pad(dechex($value), $padding, '0', STR_PAD_LEFT);
} }
function array_rand_value(array $array, $fallback = null) {
if (!$array)
return $fallback;
return $array[array_rand($array)];
}
function has_flag(int $flags, int $flag): bool {
return ($flags & $flag) > 0;
}