From d3fa32f077b57245af62d3c1dddde1fb3085e113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Olav=20Ruud?= Date: Wed, 9 Oct 2024 16:07:10 +0200 Subject: [PATCH] Don't use input.count in PrefixThrough and PrefixUpTo for speedup. Calling Substring.count will calculate the length of the string the first time it is called which for large strings can be slow. --- Sources/Parsing/ParserPrinters/PrefixThrough.swift | 9 ++++----- Sources/Parsing/ParserPrinters/PrefixUpTo.swift | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sources/Parsing/ParserPrinters/PrefixThrough.swift b/Sources/Parsing/ParserPrinters/PrefixThrough.swift index 5c08341b5a..d7db24f823 100644 --- a/Sources/Parsing/ParserPrinters/PrefixThrough.swift +++ b/Sources/Parsing/ParserPrinters/PrefixThrough.swift @@ -32,12 +32,11 @@ public struct PrefixThrough: Parser where Input.SubSequence = let original = input while let index = input.firstIndex(where: { self.areEquivalent(first, $0) }) { input = input[index...] - if input.count >= count, - zip(input[index...], self.possibleMatch).allSatisfy(self.areEquivalent) + if let matchEndIndex = input.index(index, offsetBy: count, limitedBy: input.endIndex), + zip(input[..: Parser where Input.SubSequence == I let original = input while let index = input.firstIndex(where: { self.areEquivalent(first, $0) }) { input = input[index...] - if input.count >= count, - zip(input[index...], self.possibleMatch).allSatisfy(self.areEquivalent) + if let matchEndIndex = input.index(index, offsetBy: count, limitedBy: input.endIndex), + zip(input[..