Zig 0.17.0-dev (Split by item) This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig standard library.
std › tar › sanitizePath Private Function sanitizePath Doc Source Private Function std.tar.sanitizePath
fn sanitizePath (buffer : []u8 , path : []const u8 , strip_components : u32 ) error Params buffer : []u8 path : []const u8 strip_components : u32 File tar.zig: 676
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705 fn sanitizePath (buffer : []u8 , path : []const u8 , strip_components : u32 ) error {Invalid }!usize {
if (path .len == 0 or path [0 ] == '/' ) return error .Invalid ;
var i : usize = 0 ;
var c = strip_components ;
var it = std .mem .tokenizeScalar (u8 , path , '/' );
while (it .next ()) |component | {
if (std .mem .eql (u8 , component , "." )) continue ;
if (std .mem .eql (u8 , component , ".." )) {
if (i == 0 ) return error .Invalid ;
while (true ) {
const ends_with_slash = buffer [i - 1 ] == '/' ;
i -= 1 ;
if (ends_with_slash or i == 0 ) break ;
}
continue ;
}
if (c > 0 ) {
c -= 1 ;
continue ;
}
if (i > 0 ) {
buffer [i ] = '/' ;
i += 1 ;
}
@memcpy (buffer [i ..][0 ..component .len ], component );
i += component .len ;
}
if (c > 0 ) return error .Invalid ;
return i ;
}