From 6e102e81b2f6f75f408be3207a467718f0b2fbf1 Mon Sep 17 00:00:00 2001 From: Jihoonahn Date: Sat, 1 Apr 2023 02:54:33 +0900 Subject: [PATCH] Documentation --- Sources/PLFile/PLFile.swift | 1 + Sources/PLFile/Type/PLFile+Type.swift | 5 +++- Sources/PLFile/Util/DateExt.swift | 17 ++++++++++--- Sources/PLFile/Util/FileHandleExt.swift | 24 +++++++++++++++--- Sources/PLFile/Util/StringExt.swift | 33 +++++++++++++++++-------- 5 files changed, 62 insertions(+), 18 deletions(-) diff --git a/Sources/PLFile/PLFile.swift b/Sources/PLFile/PLFile.swift index 5afdf96b..4fb603eb 100644 --- a/Sources/PLFile/PLFile.swift +++ b/Sources/PLFile/PLFile.swift @@ -1,3 +1,4 @@ import Foundation +/// A representation of a file in the library. public struct PLFile {} diff --git a/Sources/PLFile/Type/PLFile+Type.swift b/Sources/PLFile/Type/PLFile+Type.swift index 477ded8e..639f6018 100644 --- a/Sources/PLFile/Type/PLFile+Type.swift +++ b/Sources/PLFile/Type/PLFile+Type.swift @@ -1,5 +1,8 @@ -/// PLFile Type +/// The type of a file or folder. public enum PLFileType { + /// A file. case file + + /// A folder. case folder } diff --git a/Sources/PLFile/Util/DateExt.swift b/Sources/PLFile/Util/DateExt.swift index 8b8000d4..23cbaf7d 100644 --- a/Sources/PLFile/Util/DateExt.swift +++ b/Sources/PLFile/Util/DateExt.swift @@ -1,8 +1,17 @@ import Foundation extension Data { - func stringEncoding(encoding: String.Encoding = .utf8) -> String { - guard let string = String(data: self, encoding: encoding) else { return "" } - return string - } + + /** + Converts the `Data` object to a `String` using the specified encoding. + + - Parameter encoding: The string encoding to use. Default is `.utf8`. + + - Returns: A `String` representation of the `Data` object, or an empty string if the conversion fails. + */ + func stringEncoding(encoding: String.Encoding = .utf8) -> String { + guard let string = String(data: self, encoding: encoding) else { return "" } + return string + } } + diff --git a/Sources/PLFile/Util/FileHandleExt.swift b/Sources/PLFile/Util/FileHandleExt.swift index c38a1513..fc30befe 100644 --- a/Sources/PLFile/Util/FileHandleExt.swift +++ b/Sources/PLFile/Util/FileHandleExt.swift @@ -1,7 +1,14 @@ import Foundation public extension FileHandle { - /// seekToEndOfFile is Deprecated in 10.15.4, So use seekToEndFactory + + /** + Seeks to the end of the file handle. + + - Returns: The current offset from the beginning of the file. + + - Note: `seekToEndOfFile()` is deprecated in macOS 10.15.4, so this method provides a replacement. + */ func seekToEndFactory() -> UInt64 { if #available(macOS 10.15.4, *) { do { @@ -14,7 +21,13 @@ public extension FileHandle { } } - /// write(_ data: Data) is Deprecated in 10.15.4, So use writeFactory + /** + Writes the given data to the file handle. + + - Parameter data: The data to write. + + - Note: `write(_ data: Data)` is deprecated in macOS 10.15.4, so this method provides a replacement. + */ func writeFactory(_ data: Data) { if #available(macOS 10.15.4, *) { do { @@ -27,7 +40,11 @@ public extension FileHandle { } } - /// closeFile() is Deprecated in 10.15, So use closeFileFactory + /** + Closes the file handle. + + - Note: `closeFile()` is deprecated in macOS 10.15, so this method provides a replacement. + */ func closeFileFactory() { if #available(macOS 10.15, *) { do { @@ -38,3 +55,4 @@ public extension FileHandle { } } } + diff --git a/Sources/PLFile/Util/StringExt.swift b/Sources/PLFile/Util/StringExt.swift index cc00f4a1..ea187b76 100644 --- a/Sources/PLFile/Util/StringExt.swift +++ b/Sources/PLFile/Util/StringExt.swift @@ -1,12 +1,25 @@ -import Foundation - extension String { - func appendSafeSuffix(_ suffix: String) -> String { - guard !hasSuffix(suffix) else { return self } - return appending(suffix) - } - func removeSafePrefix(_ prefix: String) -> String { - guard hasPrefix(prefix) else { return self } - return String(dropFirst(prefix.count)) - } + /** + Appends a suffix to the string, but only if the string does not already end with that suffix. + + - Parameter suffix: The suffix to append. + + - Returns: A new string with the suffix appended, or the original string if it already ends with the suffix. + */ + func appendSafeSuffix(_ suffix: String) -> String { + guard !hasSuffix(suffix) else { return self } + return appending(suffix) + } + + /** + Removes a prefix from the string, but only if the string starts with that prefix. + + - Parameter prefix: The prefix to remove. + + - Returns: A new string with the prefix removed, or the original string if it does not start with the prefix. + */ + func removeSafePrefix(_ prefix: String) -> String { + guard hasPrefix(prefix) else { return self } + return String(dropFirst(prefix.count)) + } }