Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions Sources/RegexTester/CompileOnceTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import _RegexParser
import _StringProcessing
import Foundation

@available(SwiftStdlib 5.8, *)
let litPattern = #/a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1/#
@available(SwiftStdlib 5.8, *)
let strPattern = try! Regex("a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1")

@available(SwiftStdlib 5.8, *)
struct CompileOnceTest: ParsableCommand {
static let litPattern = #/a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1/#
static let strPattern = try! Regex("a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1")

var N: Int { 10_000 }
var testString = "zzzzzzzzza"

func globalLitPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
guard testString.contains(RegexTester.litPattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func globalStrPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
guard testString.contains(RegexTester.strPattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func staticLitPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
guard testString.contains(Self.litPattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func staticStrPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
guard testString.contains(Self.strPattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func outerLitPattern() -> TimeInterval {
let start = Date.now
let pattern = #/a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1/#
for _ in 0..<N {
guard testString.contains(pattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func outerStrPattern() -> TimeInterval {
let start = Date.now
let pattern = try! Regex("a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1")
for _ in 0..<N {
guard testString.contains(pattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func innerLitPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
let pattern = #/a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1/#
guard testString.contains(pattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func innerStrPattern() -> TimeInterval {
let start = Date.now
for _ in 0..<N {
let pattern = try! Regex("a+bcd(?:g*)ab+?c?de(?:(?:(?i:f)))hij|zzz++a|3|2|1")
guard testString.contains(pattern) else { return 0 }
}
return Date.now.timeIntervalSince(start)
}

func run() throws {
let globalLit = globalLitPattern()
let globalStr = globalStrPattern()
let staticLit = staticLitPattern()
let staticStr = staticStrPattern()
let outerLit = outerLitPattern()
let outerStr = outerStrPattern()
let innerLit = innerLitPattern()
let innerStr = innerStrPattern()

print("""
Global literal: \(globalLit)
string: \(globalStr)
Static literal: \(staticLit)
string: \(staticStr)
Outer literal: \(outerLit)
string: \(outerStr)
Inner literal: \(innerLit)
string: \(innerStr)
""")
}
}
24 changes: 24 additions & 0 deletions Sources/RegexTester/Main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import _RegexParser
import _StringProcessing

@main
@available(SwiftStdlib 5.8, *)
struct Main: ParsableCommand {
static var configuration: CommandConfiguration {
.init(
subcommands: [Tester.self, CompileOnceTest.self],
defaultSubcommand: Tester.self)
}
}
7 changes: 5 additions & 2 deletions Sources/RegexTester/RegexTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ import ArgumentParser
import _RegexParser
import _StringProcessing

@main
@available(SwiftStdlib 5.8, *)
struct RegexTester: ParsableCommand {
struct Tester: ParsableCommand {
static var configuration: CommandConfiguration {
.init(subcommands: [CompileOnceTest.self])
}

typealias MatchFunctionType = (String) throws -> Regex<AnyRegexOutput>.Match?

@Argument(help: "The regex pattern to test.")
Expand Down