From b23c636818b579943a7c27b294866b75b36dc0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Olav=20Ruud?= Date: Wed, 9 Oct 2024 20:19:45 +0200 Subject: [PATCH] Don't use input.count in PrefixThrough and PrefixUpTo for speedup. (#354) Calling Substring.count will calculate the length of the string the first time it is called which for large strings can be slow. Co-authored-by: Stephen Celis --- 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[..