From 3d3351775a6cd1a4de8cab323ed406025216c36f Mon Sep 17 00:00:00 2001 From: Alex Tsarkov Date: Fri, 13 Mar 2026 11:02:27 +0300 Subject: [PATCH 1/2] Add parser implementation --- app/Parser.php | 55 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/app/Parser.php b/app/Parser.php index b74cf7b9f7..be333c27d3 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -2,12 +2,59 @@ namespace App; -use Exception; - final class Parser { public function parse(string $inputPath, string $outputPath): void { - throw new Exception('TODO'); + \gc_disable(); + + $data = $this->parseInput($inputPath); + $this->formatOutput($outputPath, $data); + } + + private function parseInput(string $inputPath): array + { + $data = []; + $counters = \array_fill(0, 32 * 16 * 6, 0); + + $stream = \fopen($inputPath, 'rb'); + \stream_set_read_buffer($stream, 16 << 20); + + while (4 === \fscanf($stream, 'https://stitcher.io/blog/%[^,],%d-%d-%d', $path, $y, $m, $d)) { + $data[$path] ??= $counters; + ++$data[$path][($y - 2021) << 9 | $m << 5 | $d]; + } + + \fclose($stream); + + return $data; + } + + private function formatOutput(string $outputPath, array $data): void + { + $stream = \fopen($outputPath, 'wb'); + \stream_set_write_buffer($stream, 16 << 20); + + $path_sep = "{"; + foreach ($data as $path => $counters) { + \fwrite($stream, "{$path_sep}\n \"\\/blog\\/{$path}\": {\n"); + $date_sep = ''; + foreach ($counters as $date => $count) { + if ($count !== 0) { + \fprintf( + $stream, + "{$date_sep} \"%d-%02d-%02d\": {$count}}", + ($date >> 9) + 2021, + ($date >> 5) & 0b1111, + $date & 0b11111, + ); + $date_sep = ",\n"; + } + } + $path_sep = "\n },"; + } + \fwrite($stream, "\n }\n}\n"); + + \fsync($stream) and \fclose($stream); } -} \ No newline at end of file +} From 40d64851e1dfc2a60d5ca811c3dc167e4c6171ae Mon Sep 17 00:00:00 2001 From: Alex Tsarkov Date: Sat, 14 Mar 2026 15:19:39 +0300 Subject: [PATCH 2/2] Fix typos --- app/Parser.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Parser.php b/app/Parser.php index be333c27d3..6b4440bd61 100644 --- a/app/Parser.php +++ b/app/Parser.php @@ -43,7 +43,7 @@ private function formatOutput(string $outputPath, array $data): void if ($count !== 0) { \fprintf( $stream, - "{$date_sep} \"%d-%02d-%02d\": {$count}}", + "{$date_sep} \"%d-%02d-%02d\": {$count}", ($date >> 9) + 2021, ($date >> 5) & 0b1111, $date & 0b11111, @@ -53,7 +53,7 @@ private function formatOutput(string $outputPath, array $data): void } $path_sep = "\n },"; } - \fwrite($stream, "\n }\n}\n"); + \fwrite($stream, "\n }\n}"); \fsync($stream) and \fclose($stream); }