mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
GH-1041: Move compress_path to a bif that uses normalize_path
This commit is contained in:
parent
3d3d5e7eb4
commit
560ee0c05e
4 changed files with 22 additions and 39 deletions
|
@ -21,45 +21,6 @@ function extract_path(input: string): string
|
|||
return parts[1];
|
||||
}
|
||||
|
||||
## Compresses a given path by removing '..'s and the parent directory it
|
||||
## references and also removing dual '/'s and extraneous '/./'s.
|
||||
##
|
||||
## dir: a path string, either relative or absolute.
|
||||
##
|
||||
## Returns: a compressed version of the input path.
|
||||
function compress_path(dir: string): string
|
||||
{
|
||||
const cdup_sep = /((\/)*([^\/]|\\\/)+)?((\/)+\.\.(\/)*)/;
|
||||
|
||||
local parts = split_string_n(dir, cdup_sep, T, 1);
|
||||
if ( |parts| > 1 )
|
||||
{
|
||||
# reaching a point with two parent dir references back-to-back means
|
||||
# we don't know about anything higher in the tree to pop off
|
||||
if ( parts[1] == "../.." )
|
||||
return join_string_vec(parts, "");
|
||||
if ( sub_bytes(parts[1], 0, 1) == "/" )
|
||||
parts[1] = "/";
|
||||
else
|
||||
parts[1] = "";
|
||||
dir = join_string_vec(parts, "");
|
||||
return compress_path(dir);
|
||||
}
|
||||
|
||||
const multislash_sep = /(\/\.?){2,}/;
|
||||
parts = split_string_all(dir, multislash_sep);
|
||||
for ( i in parts )
|
||||
if ( i % 2 == 1 )
|
||||
parts[i] = "/";
|
||||
dir = join_string_vec(parts, "");
|
||||
|
||||
# remove trailing slashes from path
|
||||
if ( |dir| > 1 && sub_bytes(dir, |dir|, 1) == "/" )
|
||||
dir = sub_bytes(dir, 0, |dir| - 1);
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
||||
## Constructs a path to a file given a directory and a file name.
|
||||
##
|
||||
## dir: the directory in which the file lives.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue