<?phpnamespace App\Model;/** * Text of 2001: the files of WinZik (LYR, FLS, INFO.txt, ID3 tags) were written by Windows 98 in Windows-1252. * They are read as UTF-8 when they already are, else converted; written back in Windows-1252 where the format wants it. */final class Text{ public static function toUtf8(string $bytes): string { if ($bytes === '' || preg_match('//u', $bytes) === 1) { return $bytes; } if (function_exists('mb_convert_encoding')) { return (string)@mb_convert_encoding($bytes, 'UTF-8', 'Windows-1252'); } if (function_exists('iconv')) { $s = @iconv('CP1252', 'UTF-8//IGNORE', $bytes); if (is_string($s)) { return $s; } } // Latin-1 by hand: every byte is the code point of the same number $out = ''; for ($i = 0, $n = strlen($bytes); $i < $n; $i++) { $c = ord($bytes[$i]); $out .= $c < 0x80 ? $bytes[$i] : chr(0xC0 | $c >> 6) . chr(0x80 | $c & 0x3F); } return $out; } /** Lower case without accents ("Été" -> "ete"), by hand: the phone's PHP has no iconv (and no intl). */ public static function fold(string $text): string { return strtr(mb_strtolower(trim($text)), [ 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'œ' => 'oe', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ý' => 'y', 'ÿ' => 'y', 'ß' => 'ss', ]); } public static function toCp1252(string $utf8): string { if ($utf8 === '' || preg_match('/[^\x00-\x7F]/', $utf8) !== 1) { return $utf8; } if (function_exists('mb_convert_encoding')) { return (string)@mb_convert_encoding($utf8, 'Windows-1252', 'UTF-8'); } if (function_exists('iconv')) { $s = @iconv('UTF-8', 'CP1252//TRANSLIT', $utf8); if (is_string($s)) { return $s; } } return preg_replace('/[^\x00-\x7F]/u', '?', $utf8) ?? $utf8; } /** The lines of a text file, any line ending, without the empty ones at the end. @return list<string> */ public static function lines(string $content): array { $lines = preg_split('/\r\n|\r|\n/', self::toUtf8($content)) ?: []; while ($lines !== [] && trim((string)end($lines)) === '') { array_pop($lines); } return $lines; } /** * One value written by VB's `Write #1`: strings are quoted ("text"), numbers and booleans are not (#TRUE#). */ public static function unquote(string $value): string { $value = trim($value); if (strlen($value) >= 2 && $value[0] === '"' && str_ends_with($value, '"')) { return substr($value, 1, -1); } return $value; } /** The file name of a path, Windows or Unix (C:\MP3\song.mp3 -> song.mp3). */ public static function baseName(string $path): string { $path = rtrim($path, '/\\'); $cut = max((int)strrpos($path, '/'), (int)strrpos($path, '\\')); return $cut > 0 || (isset($path[0]) && ($path[0] === '/' || $path[0] === '\\')) ? substr($path, $cut + 1) : $path; } /** The extension in lower case, without the dot ('mp3'). */ public static function extension(string $path): string { $name = self::baseName($path); $dot = strrpos($name, '.'); return $dot === false ? '' : strtolower(substr($name, $dot + 1)); } /** The path without its extension (C:\MP3\song.mp3 -> C:\MP3\song). */ public static function withoutExtension(string $path): string { $name = self::baseName($path); $dot = strrpos($name, '.'); return $dot === false ? $path : substr($path, 0, strlen($path) - strlen($name) + $dot); }}