Skip to content

Commit

Permalink
Deprecating Foundation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen committed May 27, 2023
1 parent 900277d commit b953973
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 24 deletions.
2 changes: 0 additions & 2 deletions Sources/Tekkon/Tekkon_Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.

import Foundation

/// The namespace for this package.
public enum Tekkon {
// MARK: - Static Constants
Expand Down
4 changes: 1 addition & 3 deletions Sources/Tekkon/Tekkon_Phonabets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.

import Foundation

public extension Tekkon {
// MARK: - Dynamic Constants and Basic Enums

Expand Down Expand Up @@ -115,7 +113,7 @@ public extension Tekkon {
/// - strOf: 要取代的內容。
/// - strWith: 要取代成的內容。
public mutating func selfReplace(_ strOf: String, _ strWith: String = "") {
valueStorage = valueStorage.replacingOccurrences(of: strOf, with: strWith)
if valueStorage == strOf { valueStorage = strWith }
ensureType()
}

Expand Down
16 changes: 7 additions & 9 deletions Sources/Tekkon/Tekkon_SyllableComposer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.

import Foundation

public extension Tekkon {
// MARK: - Syllable Composer

Expand Down Expand Up @@ -65,7 +63,7 @@ public extension Tekkon {
public func getComposition(isHanyuPinyin: Bool = false, isTextBookStyle: Bool = false) -> String {
switch isHanyuPinyin {
case false: // 注音輸出的場合
let valReturnZhuyin = value.replacingOccurrences(of: " ", with: "")
let valReturnZhuyin = value.swapping(" ", with: "")
return isTextBookStyle ? cnvPhonaToTextbookReading(target: valReturnZhuyin) : valReturnZhuyin
case true: // 拼音輸出的場合
let valReturnPinyin = Tekkon.cnvPhonaToHanyuPinyin(targetJoined: value)
Expand All @@ -87,7 +85,7 @@ public extension Tekkon {
case "˙": toneReturned = "5"
default: break
}
return romajiBuffer.replacingOccurrences(of: "v", with: "ü") + toneReturned
return romajiBuffer.swapping("v", with: "ü") + toneReturned
}

/// 注拼槽內容是否為空。
Expand Down Expand Up @@ -132,7 +130,7 @@ public extension Tekkon {
/// 注意:回傳結果會受到當前注音排列 parser 屬性的影響。
/// - Parameters:
/// - key: 傳入的 UniChar 內容。
public func inputValidityCheck(key inputKey: UniChar = 0) -> Bool {
public func inputValidityCheck(key inputKey: UInt32 = 0) -> Bool {
if let scalar = UnicodeScalar(inputKey) {
let input = String(scalar)
switch parser {
Expand All @@ -159,9 +157,9 @@ public extension Tekkon {
case .ofAlvinLiu:
return Tekkon.mapAlvinLiuStaticKeys[input] != nil
case .ofWadeGilesPinyin:
return Tekkon.mapWadeGilesPinyinKeys.contains(input)
return Tekkon.mapWadeGilesPinyinKeys.has(string: input)
case .ofHanyuPinyin, .ofSecondaryPinyin, .ofYalePinyin, .ofHualuoPinyin, .ofUniversalPinyin:
return Tekkon.mapArayuruPinyin.contains(input)
return Tekkon.mapArayuruPinyin.has(string: input)
}
}
return false
Expand Down Expand Up @@ -222,7 +220,7 @@ public extension Tekkon {
/// 如果是諸如複合型注音排列的話,翻譯結果有可能為空,但翻譯過程已經處理好聲介韻調分配了。
/// - Parameters:
/// - fromCharCode: 傳入的 UniChar 內容。
public mutating func receiveKey(fromCharCode inputCharCode: UniChar = 0) {
public mutating func receiveKey(fromCharCode inputCharCode: UInt16 = 0) {
if let scalar = UnicodeScalar(inputCharCode) {
receiveKey(fromString: String(scalar))
}
Expand Down Expand Up @@ -781,6 +779,6 @@ public extension Tekkon {

private extension String {
func doesHave(_ target: String) -> Bool {
target.isEmpty ? isEmpty : contains(target)
has(string: target)
}
}
110 changes: 100 additions & 10 deletions Sources/Tekkon/Tekkon_Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.

import Foundation

public extension Tekkon {
// MARK: - Phonabet to Hanyu-Pinyin Conversion Processing

Expand All @@ -17,7 +15,7 @@ public extension Tekkon {
static func cnvPhonaToHanyuPinyin(targetJoined: String) -> String {
var targetConverted = targetJoined
for pair in arrPhonaToHanyuPinyin {
targetConverted = targetConverted.replacingOccurrences(of: pair[0], with: pair[1])
targetConverted = targetConverted.swapping(pair[0], with: pair[1])
}
return targetConverted
}
Expand All @@ -28,7 +26,7 @@ public extension Tekkon {
static func cnvHanyuPinyinToTextbookStyle(targetJoined: String) -> String {
var targetConverted = targetJoined
for pair in arrHanyuPinyinTextbookStyleConversionTable {
targetConverted = targetConverted.replacingOccurrences(of: pair[0], with: pair[1])
targetConverted = targetConverted.swapping(pair[0], with: pair[1])
}
return targetConverted
}
Expand All @@ -54,7 +52,7 @@ public extension Tekkon {
target: String
) -> String {
var newNeta = target
if !"ˊˇˋ˙".contains(String(target.reversed()[0])), !target.contains("_") { newNeta += "1" }
if !"ˊˇˋ˙".has(string: String(target.reversed()[0])), !target.has(string: "_") { newNeta += "1" }
return newNeta
}

Expand All @@ -69,11 +67,11 @@ public extension Tekkon {
var result = targetJoined
for key in Tekkon.mapHanyuPinyin.keys.sorted(by: { $0.count > $1.count }) {
guard let value = Tekkon.mapHanyuPinyin[key] else { continue }
result = result.replacingOccurrences(of: key, with: value)
result = result.swapping(key, with: value)
}
for key in Tekkon.mapArayuruPinyinIntonation.keys.sorted(by: { $0.count > $1.count }) {
guard let value = Tekkon.mapArayuruPinyinIntonation[key] else { continue }
result = result.replacingOccurrences(of: key, with: (key == "1") ? newToneOne : value)
result = result.swapping(key, with: (key == "1") ? newToneOne : value)
}
return result
}
Expand All @@ -82,8 +80,100 @@ public extension Tekkon {
/// 檢測字串是否包含半形英數內容
private extension String {
var isNotPureAlphanumerical: Bool {
let regex = ".*[^A-Za-z0-9].*"
let testString = NSPredicate(format: "SELF MATCHES %@", regex)
return testString.evaluate(with: self)
let x = unicodeScalars.map(\.value).filter {
if $0 >= 48, $0 <= 57 { return false }
if $0 >= 65, $0 <= 90 { return false }
if $0 >= 97, $0 <= 122 { return false }
return true
}
return !x.isEmpty
}
}

// This package is trying to deprecate its dependency of Foundation, hence the following contents.

extension StringProtocol {
func has(string target: any StringProtocol) -> Bool {
let selfArray = Array(unicodeScalars)
let targetArray = Array(target.description.unicodeScalars)
guard !target.isEmpty else { return isEmpty }
guard count >= target.count else { return false }
for index in 0 ..< selfArray.count {
let currentChar = selfArray[index]
if currentChar == targetArray.first {
let range = index ..< (Swift.min(index + targetArray.count, selfArray.count))
let ripped = Array(selfArray[range])
if ripped == targetArray { return true }
}
}
return false
}

func sliced(by separator: any StringProtocol = "") -> [String] {
let selfArray = Array(unicodeScalars)
let arrSeparator = Array(separator.description.unicodeScalars)
var result: [String] = []
var buffer: [Unicode.Scalar] = []
var sleepCount = 0
for index in 0 ..< selfArray.count {
let currentChar = selfArray[index]
ripCheck: if currentChar == arrSeparator.first {
let range = index ..< (Swift.min(index + arrSeparator.count, selfArray.count))
let ripped = Array(selfArray[range])
sleepCount = range.count
if ripped != arrSeparator {
if sleepCount < 1 {
buffer.append(contentsOf: ripped.compactMap { Unicode.Scalar($0) })
}
break ripCheck
} else {
result.append(buffer.map { String($0) }.joined())
buffer.removeAll()
}
}
if sleepCount < 1 {
buffer.append(currentChar)
}
sleepCount -= 1
}
result.append(buffer.map { String($0) }.joined())
buffer.removeAll()
return result
}

func swapping(_ target: String, with newString: String) -> String {
let selfArray = Array(unicodeScalars)
let arrTarget = Array(target.description.unicodeScalars)
var result = ""
var buffer: [Unicode.Scalar] = []
var sleepCount = 0
for index in 0 ..< selfArray.count {
let currentChar = selfArray[index]
ripCheck: if currentChar == arrTarget.first {
let range = index ..< (Swift.min(index + arrTarget.count, selfArray.count))
let ripped = Array(selfArray[range])
if ripped.isEmpty { continue }
sleepCount = ripped.count
if ripped != arrTarget {
result.append(buffer.map { String($0) }.joined())
if sleepCount < 1 {
result.append(ripped.map { String($0) }.joined())
}
buffer.removeAll()
break ripCheck
} else {
result.append(buffer.map { String($0) }.joined())
result.append(newString)
buffer.removeAll()
}
}
if sleepCount < 1 {
buffer.append(currentChar)
}
sleepCount -= 1
}
result.append(buffer.map { String($0) }.joined())
buffer.removeAll()
return result
}
}

0 comments on commit b953973

Please sign in to comment.