@@ -258,56 +258,59 @@ bool Path::rename (const std::string& new_name)
258258// ~ --> /home/user
259259// ~foo/x --> /home/foo/s
260260// ~/x --> /home/foo/x
261+ // . --> $PWD
261262// ./x --> $PWD/x
262263// x --> $PWD/x
263264std::string Path::expand (const std::string& in)
264265{
265266 std::string copy = in;
266267
267- auto tilde = copy.find (' ~' );
268268 std::string::size_type slash;
269269
270- if (tilde != std::string::npos)
270+ if (in.empty ())
271+ { }
272+
273+ else if (in.front () == ' ~' )
271274 {
272- const char * home = getenv (" HOME" );
275+ const char * home = getenv (" HOME" );
273276 if (home == nullptr )
274277 {
275278 struct passwd * pw = getpwuid (getuid ());
276279 home = pw->pw_dir ;
277280 }
278281
279282 // Convert: ~ --> /home/user
280- if (copy .length () == 1 )
283+ if (in .length () == 1 )
281284 copy = home;
282285
283286 // Convert: ~/x --> /home/user/x
284- else if (copy.length () > tilde + 1 &&
285- copy[tilde + 1 ] == ' /' )
286- {
287- copy.replace (tilde, 1 , home);
288- }
287+ else if (in.at (1 ) == ' /' )
288+ copy = home + in.substr (1 );
289289
290290 // Convert: ~foo/x --> /home/foo/x
291- else if ((slash = copy .find (' /' , tilde )) != std::string::npos)
291+ else if ((slash = in .find (' /' , 1 )) != std::string::npos)
292292 {
293- std::string name = copy .substr (tilde + 1 , slash - tilde - 1 );
293+ std::string name = in .substr (1 , slash - 1 );
294294 struct passwd * pw = getpwnam (name.c_str ());
295295 if (pw)
296- copy. replace (tilde, slash - tilde, pw->pw_dir );
296+ copy = pw->pw_dir + in. substr (slash );
297297 }
298298 }
299299
300300 // Relative paths
301- else if (in.length () > 2 &&
302- in.substr (0 , 2 ) == " ./" )
303- {
304- copy = Directory::cwd () + in.substr (1 );
305- }
306- else if (in.length () > 1 &&
307- in[0 ] != ' .' &&
308- in[0 ] != ' /' )
301+ else if (in.front () != ' /' )
309302 {
310- copy = Directory::cwd () + ' /' + in;
303+ // Convert: ., ./ --> $PWD
304+ if (in == " ." )
305+ copy = Directory::cwd ();
306+
307+ // Convert: ./x --> $PWD/x
308+ else if (in.substr (0 , 2 ) == " ./" )
309+ copy = Directory::cwd () + in.substr (1 );
310+
311+ // Convert: x --> $PWD/x
312+ else
313+ copy = Directory::cwd () + ' /' + in;
311314 }
312315
313316 return copy;
0 commit comments