Initial commit
This commit is contained in:
@ -6,3 +6,55 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class CommonFunc {
|
||||
static let shared = CommonFunc()
|
||||
private let profanityList: [String] = ["fuck", "shit", "porn", "习近平", "鸡巴", "阴茎"] // Example profanity words
|
||||
|
||||
private init() {} // Private initializer to ensure singleton usage
|
||||
|
||||
func validateInputEng(input: String, isSingleWord: Bool = false) -> (isValid: Bool, message: String) {
|
||||
// Check for profanity
|
||||
for badWord in profanityList {
|
||||
if input.lowercased().contains(badWord) {
|
||||
return (false, globalEnvironment.DirtyInputErrToast)
|
||||
}
|
||||
}
|
||||
|
||||
// Check characters based on the isSingleWord flag
|
||||
let pattern = isSingleWord ? "^[A-Za-z]+$" : "^[A-Za-z0-9 .,;:!?'\"@-]+$"
|
||||
let regex = try! NSRegularExpression(pattern: pattern)
|
||||
let range = NSRange(location: 0, length: input.utf16.count)
|
||||
|
||||
if regex.firstMatch(in: input, options: [], range: range) == nil {
|
||||
let characterSet = isSingleWord ? "english letters" : "english letters, numbers, and punctuation"
|
||||
return (false, "Input should only contain \(characterSet).")
|
||||
}
|
||||
|
||||
return (true, "Input is valid.")
|
||||
}
|
||||
|
||||
func validateChineseInput(input: String) -> (isValid: Bool, message: String) {
|
||||
// Check for profanity
|
||||
for word in profanityList {
|
||||
if input.contains(word) {
|
||||
return (false, globalEnvironment.DirtyInputErrToast)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if all characters are valid Chinese characters or punctuation
|
||||
for character in input {
|
||||
if !character.isPunctuation && !(0x4E00...0x9FFF).contains(character.unicodeScalars.first!.value) &&
|
||||
!(0x3400...0x4DBF).contains(character.unicodeScalars.first!.value) &&
|
||||
!(0x20000...0x2A6DF).contains(character.unicodeScalars.first!.value) {
|
||||
// 既不是中文,又不是英文,返回错误
|
||||
let res = self.validateInputEng(input: input)
|
||||
if !res.isValid{
|
||||
return (false, "Input contains invalid characters.")
|
||||
}
|
||||
}
|
||||
}
|
||||
return (true, "Input is valid.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user