diff options
author | vincentdarley <vincentdarley> | 2003-09-30 14:05:44 (GMT) |
---|---|---|
committer | vincentdarley <vincentdarley> | 2003-09-30 14:05:44 (GMT) |
commit | 62fbe6d3ac206f0bb0cfbcf84f9b60cc4a703970 (patch) | |
tree | 455fcf50a6067805d93a96cc25439f6ac2b2137f /generic/tclPathObj.c | |
parent | 7ead8280ab4a917062c6de8aec6234eace8f610a (diff) | |
download | tcl-62fbe6d3ac206f0bb0cfbcf84f9b60cc4a703970.zip tcl-62fbe6d3ac206f0bb0cfbcf84f9b60cc4a703970.tar.gz tcl-62fbe6d3ac206f0bb0cfbcf84f9b60cc4a703970.tar.bz2 |
fixed inconsistent handling of file separators in file join
Diffstat (limited to 'generic/tclPathObj.c')
-rw-r--r-- | generic/tclPathObj.c | 87 |
1 files changed, 53 insertions, 34 deletions
diff --git a/generic/tclPathObj.c b/generic/tclPathObj.c index 7283608..a86eed8 100644 --- a/generic/tclPathObj.c +++ b/generic/tclPathObj.c @@ -10,7 +10,7 @@ * See the file "license.terms" for information on usage and redistribution * of this file, and for a DISCLAIMER OF ALL WARRANTIES. * - * RCS: @(#) $Id: tclPathObj.c,v 1.9 2003/09/29 22:38:21 dkf Exp $ + * RCS: @(#) $Id: tclPathObj.c,v 1.10 2003/09/30 14:05:44 vincentdarley Exp $ */ #include "tclInt.h" @@ -338,25 +338,34 @@ Tcl_FSJoinPath(listObj, elements) } } - if (elements == 2) { - /* - * This is a special case where we can be much more - * efficient - */ - Tcl_Obj *base; + res = Tcl_NewObj(); + + for (i = 0; i < elements; i++) { + Tcl_Obj *elt; + int driveNameLength; + Tcl_PathType type; + char *strElt; + int strEltLen; + int length; + char *ptr; + Tcl_Obj *driveName = NULL; + + Tcl_ListObjIndex(NULL, listObj, i, &elt); - Tcl_ListObjIndex(NULL, listObj, 0, &base); /* - * There is only any value in doing this if the first object is - * of path type, otherwise we'll never actually get any - * efficiency benefit elsewhere in the code (from re-using the - * normalized representation of the base object). + * This is a special case where we can be much more + * efficient, where we are joining a single relative path + * onto an object that is already of path type. The + * 'TclNewFSPathObj' call below creates an object which + * can be normalized more efficiently. Currently we only + * use the special case when we have exactly two elements, + * but we could expand that in the future. */ - if (base->typePtr == &tclFsPathType - && !(base->bytes != NULL && base->bytes[0] == '\0')) { + if ((i == (elements-2)) && (i == 0) && (elt->typePtr == &tclFsPathType) + && !(elt->bytes != NULL && (elt->bytes[0] == '\0'))) { Tcl_Obj *tail; Tcl_PathType type; - Tcl_ListObjIndex(NULL, listObj, 1, &tail); + Tcl_ListObjIndex(NULL, listObj, i+1, &tail); type = TclGetPathType(tail, NULL, NULL, NULL); if (type == TCL_PATH_RELATIVE) { CONST char *str; @@ -368,10 +377,20 @@ Tcl_FSJoinPath(listObj, elements) * '/'. There's no need to return a special path * object, when the base itself is just fine! */ - return base; + return elt; } - if (str[0] != '.') { - return TclNewFSPathObj(base, str, len); + /* + * If it doesn't begin with '.' and is a mac or unix + * path or it a windows path without backslashes, then we + * can be very efficient here. (In fact even a windows + * path with backslashes can be joined efficiently, but + * the path object would not have forward slashes only, + * and this would therefore contradict our 'file join' + * documentation). + */ + if (str[0] != '.' && ((tclPlatform != TCL_PLATFORM_WINDOWS) + || (strchr(str, '\\') == NULL))) { + return TclNewFSPathObj(elt, str, len); } /* * Otherwise we don't have an easy join, and @@ -379,24 +398,24 @@ Tcl_FSJoinPath(listObj, elements) * things */ } else { - return tail; + if (tclPlatform == TCL_PLATFORM_UNIX) { + return tail; + } else { + CONST char *str; + int len; + str = Tcl_GetStringFromObj(tail,&len); + if (tclPlatform == TCL_PLATFORM_WINDOWS) { + if (strchr(str, '\\') == NULL) { + return tail; + } + } else if (tclPlatform == TCL_PLATFORM_MAC) { + if (strchr(str, '/') == NULL) { + return tail; + } + } + } } } - } - - res = Tcl_NewObj(); - - for (i = 0; i < elements; i++) { - Tcl_Obj *elt; - int driveNameLength; - Tcl_PathType type; - char *strElt; - int strEltLen; - int length; - char *ptr; - Tcl_Obj *driveName = NULL; - - Tcl_ListObjIndex(NULL, listObj, i, &elt); strElt = Tcl_GetStringFromObj(elt, &strEltLen); type = TclGetPathType(elt, &fsPtr, &driveNameLength, &driveName); if (type != TCL_PATH_RELATIVE) { |