hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit bc081d3e717425f7097eb31f024939b4165cc203
parent 0fc923bac767613bd29e714857bd04cd53094ed5
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed, 24 Feb 2021 15:48:03 -0500

fs::mode_str: rewrite without fmt

Diffstat:
Mfs/util.ha | 71++++++++++++++++++++++++++++++++++-------------------------------------
1 file changed, 34 insertions(+), 37 deletions(-)

diff --git a/fs/util.ha b/fs/util.ha @@ -1,5 +1,5 @@ use io; -use strio; +use strings; // Returns a human-friendly representation of an error. export fn errstr(err: error) const str = match (err) { @@ -12,44 +12,41 @@ export fn errstr(err: error) const str = match (err) { // is statically allocated, use [strings::dup] to duplicate it or it will be // overwritten on subsequent calls. export fn mode_str(m: mode) const str = { - // TODO: Rewrite me to avoid circular dependency on fmt - abort(); - //static let buf: [11]u8 = [0...]; - //let sink = strio::fixed(buf); - //fmt::fprintf(sink, "{}{}{}{}{}{}{}{}{}{}", - // if (m & mode::DIR == mode::DIR) "d" - // else if (m & mode::FIFO == mode::FIFO) "p" - // else if (m & mode::SOCK == mode::SOCK) "s" - // else if (m & mode::BLK == mode::BLK) "b" - // else if (m & mode::LINK == mode::LINK) "l" - // else if (m & mode::CHR == mode::CHR) "c" - // else "-", - // if (m & mode::USER_R == mode::USER_R) "r" else "-", - // if (m & mode::USER_W == mode::USER_W) "w" else "-", - // if (m & mode::SETUID == mode::SETUID) "s" - // else if (m & mode::USER_X == mode::USER_X) "x" - // else "-", - // if (m & mode::GROUP_R == mode::GROUP_R) "r" else "-", - // if (m & mode::GROUP_W == mode::GROUP_W) "w" else "-", - // if (m & mode::SETGID == mode::SETGID) "s" - // else if (m & mode::GROUP_X == mode::GROUP_X) "x" - // else "-", - // if (m & mode::OTHER_R == mode::OTHER_R) "r" else "-", - // if (m & mode::OTHER_W == mode::OTHER_W) "w" else "-", - // if (m & mode::STICKY == mode::STICKY) "t" - // else if (m & mode::OTHER_X == mode::OTHER_X) "x" - // else "-", - // ); - //return strio::string(sink); + static let buf: [10]u8 = [0...]; + buf = [ + (if (m & mode::DIR == mode::DIR) 'd' + else if (m & mode::FIFO == mode::FIFO) 'p' + else if (m & mode::SOCK == mode::SOCK) 's' + else if (m & mode::BLK == mode::BLK) 'b' + else if (m & mode::LINK == mode::LINK) 'l' + else if (m & mode::CHR == mode::CHR) 'c' + else '-'): u32: u8, + (if (m & mode::USER_R == mode::USER_R) 'r' else '-'): u32: u8, + (if (m & mode::USER_W == mode::USER_W) 'w' else '-'): u32: u8, + (if (m & mode::SETUID == mode::SETUID) 's' + else if (m & mode::USER_X == mode::USER_X) 'x' + else '-'): u32: u8, + (if (m & mode::GROUP_R == mode::GROUP_R) 'r' else '-'): u32: u8, + (if (m & mode::GROUP_W == mode::GROUP_W) 'w' else '-'): u32: u8, + (if (m & mode::SETGID == mode::SETGID) 's' + else if (m & mode::GROUP_X == mode::GROUP_X) 'x' + else '-'): u32: u8, + (if (m & mode::OTHER_R == mode::OTHER_R) 'r' else '-'): u32: u8, + (if (m & mode::OTHER_W == mode::OTHER_W) 'w' else '-'): u32: u8, + (if (m & mode::STICKY == mode::STICKY) 't' + else if (m & mode::OTHER_X == mode::OTHER_X) 'x' + else '-'): u32: u8, + ]; + return strings::from_utf8(buf); }; -//@test fn mode_str() void = { -// assert(mode_str(0o777: mode) == "-rwxrwxrwx"); -// assert(mode_str(mode::DIR | 0o755: mode) == "drwxr-xr-x"); -// assert(mode_str(0o755: mode | mode::SETUID) == "-rwsr-xr-x"); -// assert(mode_str(0o644: mode) == "-rw-r--r--"); -// assert(mode_str(0: mode) == "----------"); -//}; +@test fn mode_str() void = { + assert(mode_str(0o777: mode) == "-rwxrwxrwx"); + assert(mode_str(mode::DIR | 0o755: mode) == "drwxr-xr-x"); + assert(mode_str(0o755: mode | mode::SETUID) == "-rwsr-xr-x"); + assert(mode_str(0o644: mode) == "-rw-r--r--"); + assert(mode_str(0: mode) == "----------"); +}; // Returns the permission bits of a file mode. export fn mode_perm(m: mode) mode = (m: uint & 0o777u): mode;