WITH_SUFFIX switch file extension

Contents

Inputs

Outputs

function f = with_suffix(p, suffix)
arguments
  p (1,1) string
  suffix (1,1) string
end


r = stdlib.parent(p);
s = stdlib.stem(p);

if stdlib.len(s) == 0
  f = stdlib.join(p, suffix);
  return
end

if strcmp(r, '.')
  f = s;
else
  f = strcat(r, "/", s);
end

f = strcat(f, suffix);

end

%!assert(with_suffix("ab.h5", ".nc"), "ab.nc")
%!assert(with_suffix("ab", ".nc"), "ab.nc")
%!assert(with_suffix("ab.h5", ""), "ab")
%!assert(with_suffix("ab", ""), "ab")
%!assert(with_suffix("ab/.h5", ".nc"), "ab/.h5.nc")