@@ -648,18 +648,18 @@ fn testResolvePosix(paths: []const []const u8) []u8 {
648648}
649649
650650/// If the path is a file in the current directory (no directory component)
651- /// then the returned slice has .len = 0.
652- pub fn dirname (path : []const u8 ) []const u8 {
651+ /// then returns null
652+ pub fn dirname (path : []const u8 ) ? []const u8 {
653653 if (is_windows ) {
654654 return dirnameWindows (path );
655655 } else {
656656 return dirnamePosix (path );
657657 }
658658}
659659
660- pub fn dirnameWindows (path : []const u8 ) []const u8 {
660+ pub fn dirnameWindows (path : []const u8 ) ? []const u8 {
661661 if (path .len == 0 )
662- return path [0 .. 0] ;
662+ return null ;
663663
664664 const root_slice = diskDesignatorWindows (path );
665665 if (path .len == root_slice .len )
@@ -671,26 +671,29 @@ pub fn dirnameWindows(path: []const u8) []const u8 {
671671
672672 while ((path [end_index ] == '/' or path [end_index ] == '\\ ' ) and end_index > root_slice .len ) {
673673 if (end_index == 0 )
674- return path [0 .. 0] ;
674+ return null ;
675675 end_index -= 1 ;
676676 }
677677
678678 while (path [end_index ] != '/' and path [end_index ] != '\\ ' and end_index > root_slice .len ) {
679679 if (end_index == 0 )
680- return path [0 .. 0] ;
680+ return null ;
681681 end_index -= 1 ;
682682 }
683683
684684 if (have_root_slash and end_index == root_slice .len ) {
685685 end_index += 1 ;
686686 }
687687
688+ if (end_index == 0 )
689+ return null ;
690+
688691 return path [0.. end_index ];
689692}
690693
691- pub fn dirnamePosix (path : []const u8 ) []const u8 {
694+ pub fn dirnamePosix (path : []const u8 ) ? []const u8 {
692695 if (path .len == 0 )
693- return path [0 .. 0] ;
696+ return null ;
694697
695698 var end_index : usize = path .len - 1 ;
696699 while (path [end_index ] == '/' ) {
@@ -701,13 +704,16 @@ pub fn dirnamePosix(path: []const u8) []const u8 {
701704
702705 while (path [end_index ] != '/' ) {
703706 if (end_index == 0 )
704- return path [0 .. 0] ;
707+ return null ;
705708 end_index -= 1 ;
706709 }
707710
708711 if (end_index == 0 and path [end_index ] == '/' )
709712 return path [0.. 1];
710713
714+ if (end_index == 0 )
715+ return null ;
716+
711717 return path [0.. end_index ];
712718}
713719
@@ -717,10 +723,10 @@ test "os.path.dirnamePosix" {
717723 testDirnamePosix ("/a" , "/" );
718724 testDirnamePosix ("/" , "/" );
719725 testDirnamePosix ("////" , "/" );
720- testDirnamePosix ("" , "" );
721- testDirnamePosix ("a" , "" );
722- testDirnamePosix ("a/" , "" );
723- testDirnamePosix ("a//" , "" );
726+ testDirnamePosix ("" , null );
727+ testDirnamePosix ("a" , null );
728+ testDirnamePosix ("a/" , null );
729+ testDirnamePosix ("a//" , null );
724730}
725731
726732test "os.path.dirnameWindows" {
@@ -742,7 +748,7 @@ test "os.path.dirnameWindows" {
742748 testDirnameWindows ("c:foo\\ bar" , "c:foo" );
743749 testDirnameWindows ("c:foo\\ bar\\ " , "c:foo" );
744750 testDirnameWindows ("c:foo\\ bar\\ baz" , "c:foo\\ bar" );
745- testDirnameWindows ("file:stream" , "" );
751+ testDirnameWindows ("file:stream" , null );
746752 testDirnameWindows ("dir\\ file:stream" , "dir" );
747753 testDirnameWindows ("\\\\ unc\\ share" , "\\\\ unc\\ share" );
748754 testDirnameWindows ("\\\\ unc\\ share\\ foo" , "\\\\ unc\\ share\\ " );
@@ -753,18 +759,26 @@ test "os.path.dirnameWindows" {
753759 testDirnameWindows ("/a/b/" , "/a" );
754760 testDirnameWindows ("/a/b" , "/a" );
755761 testDirnameWindows ("/a" , "/" );
756- testDirnameWindows ("" , "" );
762+ testDirnameWindows ("" , null );
757763 testDirnameWindows ("/" , "/" );
758764 testDirnameWindows ("////" , "/" );
759- testDirnameWindows ("foo" , "" );
765+ testDirnameWindows ("foo" , null );
760766}
761767
762- fn testDirnamePosix (input : []const u8 , expected_output : []const u8 ) void {
763- assert (mem .eql (u8 , dirnamePosix (input ), expected_output ));
768+ fn testDirnamePosix (input : []const u8 , expected_output : ? []const u8 ) void {
769+ if (dirnamePosix (input )) | output | {
770+ assert (mem .eql (u8 , output , expected_output .? ));
771+ } else {
772+ assert (expected_output == null );
773+ }
764774}
765775
766- fn testDirnameWindows (input : []const u8 , expected_output : []const u8 ) void {
767- assert (mem .eql (u8 , dirnameWindows (input ), expected_output ));
776+ fn testDirnameWindows (input : []const u8 , expected_output : ? []const u8 ) void {
777+ if (dirnameWindows (input )) | output | {
778+ assert (mem .eql (u8 , output , expected_output .? ));
779+ } else {
780+ assert (expected_output == null );
781+ }
768782}
769783
770784pub fn basename (path : []const u8 ) []const u8 {
0 commit comments