EXPANDUSER expands tilde ~ into user home directory

Useful for Matlab functions that can't handle ~

Contents

Inputs

Outputs

function e = expanduser(p)
arguments
  p {mustBeTextScalar}
end

e = stdlib.posix(char(p));

L = length(e);
if L == 0 || e(1) ~= '~' || (L > 1 && ~strcmp(e(1:2), '~/'))
  % noop
else
  home = stdlib.homedir();
  if isempty(home)
    % noop
  elseif L < 2
    e = home;
  else
    e = strcat(home, '/', e(3:end));
  end
end

if isstring(p)
  e = string(e);
end

end


%!assert(expanduser(''), '')
%!assert(expanduser("~"), homedir())
%!assert(expanduser("~/"), homedir())
%!assert(expanduser("~user"), "~user")
%!assert(expanduser("~user/"), "~user")
%!assert(expanduser("~///c"), strcat(homedir(), "/c"))