From 8db135a94d177c3114a300a64b1f22f5eaf323f9 Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 15 Apr 2024 23:19:34 -0400 Subject: [PATCH] add ReadOnlySpan fast number parsers --- AOC.Common/Util.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/AOC.Common/Util.cs b/AOC.Common/Util.cs index 6cca8bf..f8b2cf5 100644 --- a/AOC.Common/Util.cs +++ b/AOC.Common/Util.cs @@ -14,4 +14,20 @@ public static class Util b = a1 % b; } } + + public static int ParseIntFast(ReadOnlySpan span) + { + var result = 0; + for (var i = 0; i < span.Length; i++) + result = result * 10 + span[i] - '0'; + return result; + } + + public static long ParseLongFast(ReadOnlySpan span) + { + var result = 0L; + for (var i = 0; i < span.Length; i++) + result = result * 10 + span[i] - '0'; + return result; + } }