-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve error message for Gemini
- Loading branch information
Showing
3 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Easydict/NewApp/Utility/Extensions/String/String+Regex.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// String+Regex.swift | ||
// Easydict | ||
// | ||
// Created by tisfeng on 2024/1/26. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
func extract(withPattern pattern: String) -> String? { | ||
do { | ||
let regex = try NSRegularExpression(pattern: pattern) | ||
let range = NSRange(location: 0, length: utf16.count) | ||
if let match = regex.firstMatch(in: self, options: [], range: range) { | ||
if let range = Range(match.range(at: 1), in: self) { | ||
return String(self[range]) | ||
} | ||
} | ||
} catch { | ||
print("Invalid regex: \(error.localizedDescription)") | ||
} | ||
return nil | ||
} | ||
} |