55// This package is trying to deprecate its dependency of Foundation, hence this file.
66
77extension StringProtocol {
8+ /// 檢查字串中是否包含指定子字串。
9+ ///
10+ /// - 注意:此方法不仰賴 Foundation API,而是以 Unicode Scalar 為單位執行逐位比對。
11+ /// 因此在處理複雜的 Compose 字元(如合字)時結果可能與 Foundation 的 substring
12+ /// 搜尋略有差異;但該實作在純 Swift 環境下效能良好且不會引入 Foundation 的相依性。
13+ /// - Parameters:
14+ /// - target: 要查找的子字串(可接受任何符合 StringProtocol 的型別)。
15+ /// - Returns: 如果找到則回傳 true,否則回傳 false。
16+ ///
17+ /// 範例:
18+ /// "abcd".has(string: "bc") => true
819 func has( string target: any StringProtocol ) -> Bool {
920 let selfArray = Array ( unicodeScalars)
1021 let targetArray = Array ( target. description. unicodeScalars)
@@ -19,6 +30,16 @@ extension StringProtocol {
1930 }
2031
2132 func sliced( by separator: any StringProtocol = " " ) -> [ String ] {
33+ /// 以指定分界字元拆分字串,回傳字串陣列;等同於 `split` 的功能,但不使用 Foundation。
34+ ///
35+ /// - 注意:該實作會將分隔符視為完整字串進行比對(以 Unicode Scalar 為基準),
36+ /// 若分隔符為空字串,則會視為不做分割。
37+ /// - Parameters:
38+ /// - separator: 作為斷詞分界的字串,預設為空字串。
39+ /// - Returns: 拆分後的字串陣列,若輸入為空回傳空陣列(視實作情況)。
40+ ///
41+ /// 範例:
42+ /// "a-b-c".sliced(by: "-") => ["a","b","c"]
2243 let selfArray = Array ( unicodeScalars)
2344 let arrSeparator = Array ( separator. description. unicodeScalars)
2445 var result : [ String ] = [ ]
@@ -44,6 +65,18 @@ extension StringProtocol {
4465 return result
4566 }
4667
68+ /// 以純 Swift 方法將字串中的指定子字串替換為另一字串。
69+ ///
70+ /// - Parameters:
71+ /// - target: 要被替換的子字串。
72+ /// - newString: 替換為的新字串。
73+ /// - Returns: 替換完成的字串。
74+ ///
75+ /// 注意:此函式使用 Unicode Scalar 做逐個比對,不會使用 Foundation 的 replace API,
76+ /// 因此在面對某些特殊 Unicode 合字組合時,行為可能與 Foundation 產生差異。
77+ ///
78+ /// 範例:
79+ /// "a-b-c".swapping("-","+") => "a+b+c"
4780 func swapping( _ target: String , with newString: String ) -> String {
4881 let selfArray = Array ( unicodeScalars)
4982 let arrTarget = Array ( target. description. unicodeScalars)
@@ -161,7 +194,14 @@ public struct FIUUID: Hashable, Codable, Sendable {
161194 public let highBits : UInt64
162195 public let lowBits : UInt64
163196
164- /// 以標準 UUID 字串形式返回識別值(預設為大寫)。
197+ /// 以標準 UUID 格式(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)返回識別字串。
198+ ///
199+ /// - Parameters:
200+ /// - uppercase: 是否回傳大寫字母(預設 true)。
201+ /// - Returns: 符合 UUID 標準格式的字串表示。
202+ ///
203+ /// 範例:
204+ /// FIUUID().uuidString() => "AABBCCDD-..."
165205 public func uuidString( uppercase: Bool = true ) -> String {
166206 var bytes = [ UInt8] ( repeating: 0 , count: 16 )
167207 Self . write ( bigEndian: highBits, to: & bytes, offset: 0 )
@@ -207,6 +247,9 @@ public struct FIUUID: Hashable, Codable, Sendable {
207247 return result
208248 }
209249
250+ /// 取得 16 進位字元的數值表示,支援大小寫字母與數字 0~9。
251+ /// - Parameter character: 要轉換的 16 進位字元。
252+ /// - Returns: 對應的數值(0..15),若非合法 16 進位字元則回傳 nil。
210253 private static func hexValue( of character: Character ) -> UInt8 ? {
211254 switch character {
212255 case " 0 " ... " 9 " :
0 commit comments