@@ -703,28 +703,24 @@ RAIIIsolate::RAIIIsolate(const SnapshotData* data)
703703RAIIIsolate::~RAIIIsolate () {}
704704
705705#ifdef _WIN32
706- // windows
707-
708- bool IsPathSeparator (const char c) {
706+ bool IsPathSeparator (const char c) noexcept {
709707 return c == ' \\ ' || c == ' /' ;
710708}
711- #else
712- // posix
713-
714- bool IsPathSeparator (const char c) {
709+ #else // POSIX
710+ bool IsPathSeparator (const char c) noexcept {
715711 return c == ' /' ;
716712}
717- #endif
713+ #endif // _WIN32
718714
719- std::string NormalizeString (const std::string path,
715+ std::string NormalizeString (const std::string_view path,
720716 bool allowAboveRoot,
721- const std::string separator) {
722- std::string res = " " ;
717+ const std::string_view separator) {
718+ std::string res;
723719 int lastSegmentLength = 0 ;
724720 int lastSlash = -1 ;
725721 int dots = 0 ;
726722 char code;
727- const auto pathLen = path.length ();
723+ const auto pathLen = path.size ();
728724 for (uint8_t i = 0 ; i <= pathLen; ++i) {
729725 if (i < pathLen) {
730726 code = path[i];
@@ -764,12 +760,13 @@ std::string NormalizeString(const std::string path,
764760 }
765761
766762 if (allowAboveRoot) {
767- res += res.length () > 0 ? separator + " .." : " .." ;
763+ res += res.length () > 0 ? std::string ( separator) + " .." : " .." ;
768764 lastSegmentLength = 2 ;
769765 }
770766 } else {
771- if (res.length () > 0 ) {
772- res += separator + path.substr (lastSlash + 1 , i - (lastSlash + 1 ));
767+ if (!res.empty ()) {
768+ res += std::string (separator) +
769+ std::string (path.substr (lastSlash + 1 , i - (lastSlash + 1 )));
773770 } else {
774771 res = path.substr (lastSlash + 1 , i - (lastSlash + 1 ));
775772 }
@@ -788,9 +785,7 @@ std::string NormalizeString(const std::string path,
788785}
789786
790787#ifdef _WIN32
791- // windows
792-
793- bool IsWindowsDeviceRoot (const char c) {
788+ bool IsWindowsDeviceRoot (const char c) const noexcept {
794789 return (c >= ' a' && c <= ' z' ) || (c >= ' A' && c <= ' Z' );
795790}
796791
@@ -800,14 +795,14 @@ std::string PathResolve(Environment* env,
800795 std::string resolvedTail = " " ;
801796 bool resolvedAbsolute = false ;
802797 const size_t numArgs = paths.size ();
798+ auto cwd = env->GetCwd (env->exec_path ());
803799
804800 for (int i = numArgs - 1 ; i >= -1 && !resolvedAbsolute; i--) {
805- std::string path = " " ;
801+ std::string path;
806802 if (i >= 0 ) {
807803 path = std::string (paths[i]);
808-
809804 } else if (resolvedDevice.empty ()) {
810- path = env-> GetCwd () ;
805+ path = cwd ;
811806 } else {
812807 // Windows has the concept of drive-specific current working
813808 // directories. If we've resolved a drive letter but not yet an
@@ -817,7 +812,7 @@ std::string PathResolve(Environment* env,
817812 std::string resolvedDevicePath;
818813 const std::string envvar = " =" + resolvedDevice;
819814 credentials::SafeGetenv (envvar.c_str (), &resolvedDevicePath);
820- path = resolvedDevicePath.empty () ? env-> GetCwd () : resolvedDevicePath;
815+ path = resolvedDevicePath.empty () ? cwd : resolvedDevicePath;
821816
822817 // Verify that a cwd was found and that it actually points
823818 // to our drive. If not, default to the drive's root.
@@ -932,32 +927,39 @@ std::string PathResolve(Environment* env,
932927
933928 return " ." ;
934929}
935- #else
936- // posix
930+ #else // _WIN32
937931std::string PathResolve (Environment* env,
938932 const std::vector<std::string_view>& paths) {
939- std::string resolvedPath = " " ;
933+ std::string resolvedPath;
940934 bool resolvedAbsolute = false ;
941- const size_t numArgs = paths. size ( );
935+ auto cwd = env-> GetCwd (env-> exec_path () );
942936
943937 for (int i = numArgs - 1 ; i >= -1 && !resolvedAbsolute; i--) {
944938 const std::string& path = (i >= 0 ) ? std::string (paths[i]) : env->GetCwd (env->exec_path ());
945939 /* validateString(path, "paths[" + std::to_string(i) + "]"); */
946940
947941 if (!path.empty ()) {
948- resolvedPath = std::string (path + " /" + resolvedPath);
949- resolvedAbsolute = (path[0 ] == ' /' );
942+ resolvedPath = std::string (path) + " /" + resolvedPath;
943+
944+ if (path.front () == ' /' ) {
945+ resolvedAbsolute = true ;
946+ break ;
947+ }
950948 }
951949 }
952950
953951 // Normalize the path
954- resolvedPath = NormalizeString (resolvedPath, !resolvedAbsolute, " /" );
952+ auto normalizedPath = NormalizeString (resolvedPath, !resolvedAbsolute, " /" );
955953
956954 if (resolvedAbsolute) {
957- return " /" + resolvedPath;
955+ return " /" + normalizedPath;
956+ }
957+
958+ if (normalizedPath.empty ()) {
959+ return " ." ;
958960 }
959- return (!resolvedPath.empty ()) ? resolvedPath : " ." ;
960- }
961- #endif
962961
962+ return normalizedPath;
963+ }
964+ #endif // _WIN32
963965} // namespace node
0 commit comments