mirror of
https://github.com/apple/swift-foundation.git
synced 2025-05-18 03:09:06 +08:00
* rdar://107955097 (FoundationPreview: Batch move string API (continued)) - Move localized uppercase and lowercase to FoundationLocalization - if-def out of CharacterSet from FoundationPreview. It's not implemented at all there, and having a no-op stub is misleading * rdar://107955097 (FoundationPreview: Batch move string API (continued)) - Move components separated by string and range of string functions * rdar://107955097 (FoundationPreview: Batch move string API (continued)) Enable snake case options for JSON encoder and decoder. We haven't been able to enable this option because it needed `CharacterSet`, which hasn't been properly implemented for FoundationPreview. Now that we have `BuiltInUnicodeScalarSet`, which mirrors `CharacterSet`, we can switch to that and enable the options. * rdar://107955097 (FoundationPreview: Batch move string API (continued)) - Move `StringProtocol.lineRange(for:)` and `paragraphRange(for:)` to FoundationEssentials - Rename String+Regex.swift to RegexPatternCache.swift - Consolidate extensions on various String family members and remove one redundant swift file --------- Co-authored-by: I-Ting Tina Liu <iting_liu@apple.com>
46 lines
1.3 KiB
Swift
46 lines
1.3 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 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
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
struct RegexPatternCache: @unchecked Sendable {
|
|
private struct Key : Sendable, Hashable {
|
|
var pattern: String
|
|
var caseInsensitive: Bool
|
|
}
|
|
|
|
private let _lock: LockedState<[Key: Regex<AnyRegexOutput>]>
|
|
|
|
static let cache = RegexPatternCache()
|
|
|
|
fileprivate init() {
|
|
_lock = LockedState(initialState: .init())
|
|
}
|
|
|
|
func regex(for pattern: String, caseInsensitive: Bool) throws -> Regex<AnyRegexOutput>? {
|
|
|
|
let key = Key(pattern: pattern, caseInsensitive: caseInsensitive)
|
|
|
|
return try _lock.withLock { cache in
|
|
|
|
if let cached = cache[key] {
|
|
return cached
|
|
}
|
|
|
|
var r = try Regex(pattern)
|
|
if caseInsensitive {
|
|
r = r.ignoresCase()
|
|
}
|
|
cache[key] = r
|
|
return r
|
|
}
|
|
}
|
|
}
|