optimized dirname and basename method