Skip to content

Commit 21ce3c6

Browse files
committed
Add extra API documentation.
1 parent 001a7d6 commit 21ce3c6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Sources/Megrez/9_SwiftImpl_Internals.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
// This package is trying to deprecate its dependency of Foundation, hence this file.
66

77
extension 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] = []
@@ -45,6 +66,19 @@ extension StringProtocol {
4566
}
4667

4768
func swapping(_ target: String, with newString: String) -> String {
69+
/// 以純 Swift 方法將字串中的指定子字串替換為另一字串。
70+
///
71+
/// - Parameters:
72+
/// - target: 要被替換的子字串。
73+
/// - newString: 替換為的新字串。
74+
/// - Returns: 替換完成的字串。
75+
///
76+
/// 注意:此函式使用 Unicode Scalar 做逐個比對,不會使用 Foundation 的 replace API,
77+
/// 因此在面對某些特殊 Unicode 合字組合時,行為可能與 Foundation 產生差異。
78+
///
79+
/// 範例:
80+
/// "a-b-c".swapping("-","+") => "a+b+c"
81+
func swapping(_ target: String, with newString: String) -> String {
4882
let selfArray = Array(unicodeScalars)
4983
let arrTarget = Array(target.description.unicodeScalars)
5084
var result = ""
@@ -162,6 +196,14 @@ public struct FIUUID: Hashable, Codable, Sendable {
162196
public let lowBits: UInt64
163197

164198
/// 以標準 UUID 字串形式返回識別值(預設為大寫)。
199+
/// 以標準 UUID 格式(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)返回識別字串。
200+
///
201+
/// - Parameters:
202+
/// - uppercase: 是否回傳大寫字母(預設 true)。
203+
/// - Returns: 符合 UUID 標準格式的字串表示。
204+
///
205+
/// 範例:
206+
/// FIUUID().uuidString() => "AABBCCDD-..."
165207
public func uuidString(uppercase: Bool = true) -> String {
166208
var bytes = [UInt8](repeating: 0, count: 16)
167209
Self.write(bigEndian: highBits, to: &bytes, offset: 0)
@@ -207,6 +249,9 @@ public struct FIUUID: Hashable, Codable, Sendable {
207249
return result
208250
}
209251

252+
/// 取得 16 進位字元的數值表示,支援大小寫字母與數字 0~9。
253+
/// - Parameter character: 要轉換的 16 進位字元。
254+
/// - Returns: 對應的數值(0..15),若非合法 16 進位字元則回傳 nil。
210255
private static func hexValue(of character: Character) -> UInt8? {
211256
switch character {
212257
case "0" ... "9":

0 commit comments

Comments
 (0)