Skip to content

Commit

Permalink
std/os: add File.Str, File.IsDir, File.IsRegular, File.Type
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Feb 19, 2025
1 parent c21a2dc commit 3f2d2cd
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions std/os/types.jule
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,51 @@ const ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice
const ModePerm = 0777

impl FileMode {
fn Str(self): str {
const s = "dalTLDpSugct?"
let mut buf: [32]byte // Mode is uint32.
mut w := 0
for i, c in s {
if self&(1<<uint(32-1-i)) != 0 {
buf[w] = byte(c)
w++
}
}
if w == 0 {
buf[w] = '-'
w++
}
const rwx = "rwxrwxrwx"
for i, c in rwx {
if self&(1<<uint(9-1-i)) != 0 {
buf[w] = byte(c)
} else {
buf[w] = '-'
}
w++
}
ret str(buf[:w])
}

// Reports whether self describes a directory.
// That is, it tests for the [ModeDir] bit being set in self.
fn IsDir(self): bool {
ret self&ModeDir != 0
}

// Reports whether self describes a regular file.
// That is, it tests that no mode type bits are set.
fn IsRegular(self): bool {
ret self&ModeType == 0
}

// Returns the Unix permission bits in self (self & [ModePerm]).
fn Perm(self): FileMode {
ret self & ModePerm
}

// Returns type bits in self (self & [ModeType]).
fn Type(self): FileMode {
ret self & ModeType
}
}

0 comments on commit 3f2d2cd

Please sign in to comment.