@@ -40,19 +40,33 @@ public struct STPathNameComponents {
4040 public var isHidden : Bool { name. hasPrefix ( " . " ) }
4141
4242 public init ( _ name: String ) {
43- let lastPathComponent = name. split ( separator: " / " , omittingEmptySubsequences: true ) . last ?? " "
44- let components = lastPathComponent. split ( separator: " . " , omittingEmptySubsequences: false )
45-
46- // [en] If it is a hidden file and has only one dot, then the entire lastPathComponent is the file name.
47- // [zh] 如果是隐藏文件且只有一个点,那么整个 lastPathComponent 是文件名。
48- if components. count == 1 {
49- self . name = String ( lastPathComponent)
43+ let path = name
44+ let pathComponents = path. split ( separator: " / " , omittingEmptySubsequences: true )
45+ let lastPathComponent = pathComponents. last. map ( String . init) ?? " "
46+ if lastPathComponent. isEmpty {
47+ self . name = " "
5048 self . extension = nil
49+ } else if lastPathComponent. hasPrefix ( " . " ) {
50+ // Hidden file, check for multiple dots
51+ let dotComponents = lastPathComponent. dropFirst ( ) . split ( separator: " . " , omittingEmptySubsequences: false )
52+ if dotComponents. count <= 1 {
53+ self . name = lastPathComponent
54+ self . extension = nil
55+ } else {
56+ let ext = dotComponents. last. map ( String . init)
57+ let namePart = " . " + dotComponents. dropLast ( ) . joined ( separator: " . " )
58+ self . name = namePart
59+ self . extension = ext
60+ }
5161 } else {
52- // [en] Otherwise, the part after the first dot is the file name, and the second part is the extension.
53- // [zh] 否则,除去第一个点的剩余部分是文件名,第二部分是扩展名。
54- self . name = ( lastPathComponent. hasPrefix ( " . " ) ? " . " : " " ) + components. dropLast ( ) . joined ( separator: " . " )
55- self . extension = components. last? . description
62+ let dotComponents = lastPathComponent. split ( separator: " . " , omittingEmptySubsequences: false )
63+ if dotComponents. count == 1 {
64+ self . name = lastPathComponent
65+ self . extension = nil
66+ } else {
67+ self . name = dotComponents. dropLast ( ) . joined ( separator: " . " )
68+ self . extension = dotComponents. last. map ( String . init)
69+ }
5670 }
5771 }
5872}
0 commit comments