Skip to content

Commit

Permalink
Documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihoonahn committed Mar 31, 2023
1 parent 6e8f5b3 commit 6e102e8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
1 change: 1 addition & 0 deletions Sources/PLFile/PLFile.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation

/// A representation of a file in the library.
public struct PLFile {}
5 changes: 4 additions & 1 deletion Sources/PLFile/Type/PLFile+Type.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// PLFile Type
/// The type of a file or folder.
public enum PLFileType {
/// A file.
case file

/// A folder.
case folder
}
17 changes: 13 additions & 4 deletions Sources/PLFile/Util/DateExt.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

24 changes: 21 additions & 3 deletions Sources/PLFile/Util/FileHandleExt.swift
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -38,3 +55,4 @@ public extension FileHandle {
}
}
}

33 changes: 23 additions & 10 deletions Sources/PLFile/Util/StringExt.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 6e102e8

Please sign in to comment.