add is_absolute and absolute methods to path::Path (#882)

* lib/std/io/os: remove unnecessary dup in native_ls

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/core: add String.index_of_char and String.rindex_of_char

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/io: add Path.is_absolute

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std/io: add Path.absolute

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

* lib/std: fix Path.normalize on files starting with `.`; add Path.walk

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>

---------

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
This commit is contained in:
Pierre Curto
2023-07-25 23:23:56 +02:00
committed by GitHub
parent b74b62e242
commit 242006d05d
5 changed files with 145 additions and 23 deletions

View File

@@ -202,6 +202,42 @@ fn bool String.contains(s, String needle)
return @ok(s.index_of(needle));
}
/**
* Find the index of the first incidence of a string.
*
* @param [in] s
* @pure
* @ensure return < s.len
* @return "the index of the needle"
* @return! SearchResult.MISSING "if the needle cannot be found"
**/
fn usz! String.index_of_char(s, char needle)
{
foreach (i, c : s)
{
if (c == needle) return i;
}
return SearchResult.MISSING?;
}
/**
* Find the index of the first incidence of a string.
*
* @param [in] s
* @pure
* @ensure return < s.len
* @return "the index of the needle"
* @return! SearchResult.MISSING "if the needle cannot be found"
**/
fn usz! String.rindex_of_char(s, char needle)
{
foreach_r (i, c : s)
{
if (c == needle) return i;
}
return SearchResult.MISSING?;
}
/**
* Find the index of the first incidence of a string.
*
@@ -215,10 +251,11 @@ fn bool String.contains(s, String needle)
**/
fn usz! String.index_of(s, String needle)
{
usz match = 0;
usz needed = needle.len;
usz index_start = 0;
char search = needle[0];
if (needed == 1) return s.index_of_char(search);
usz match = 0;
usz index_start = 0;
foreach (usz i, char c : s)
{
if (c == search)
@@ -251,10 +288,11 @@ fn usz! String.index_of(s, String needle)
**/
fn usz! String.rindex_of(s, String needle)
{
usz match = 0;
usz needed = needle.len;
usz index_start = 0;
char search = needle[^1];
if (needed == 1) return s.rindex_of_char(search);
usz match;
usz index_start;
foreach_r (usz i, char c : s)
{
if (c == search)