Tk Library Source Code

View Ticket
Login
Ticket UUID: 2872536
Title: stripPath checks partial path components
Type: Bug Version: None
Submitter: apnadkarni Created on: 2009-10-04 12:17:07
Subsystem: fileutil Assigned To: andreas_kupries
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2009-10-28 02:18:44
Resolution: Fixed Closed By: andreas_kupries
    Closed on: 2009-10-27 19:18:44
Description:
Example:
(woof) 51 % fileutil::stripPath c:/temp c:/tempx/foo/bar
foo/bar

Should return c:/tempx/foo/bar

My guess is it does a prefix string comparison without checking if next char is a dir seprator or not.
User Comments: andreas_kupries added on 2009-10-28 02:18:44:

allow_comments - 1

andreas_kupries added on 2009-10-28 02:18:42:
Fixed. Both issues. Committed to head, with extended testsuite. The \\bar issue was fixed by moving the 'string equal' after the file split, forcing comparison of the canonical list representation of the paths, without an for- and backward slashes. Version bumped to 1.14.1.

andreas_kupries added on 2009-10-07 02:27:46:
I added a different patch. Basically adding a space between ${prefix} and the '*', to account for the separation of the list elements returned by 'file split'. This should fix the basic issue.

Not sure if that helps with the ...\\bar case as well.

I still have to test this on windows. Unix had the same basic issue, there my patch is a fix.

Note: It is better to attach changes to the report by 'add'ing files. Inlined code as you provide in your comments gets badly formatted.

andreas_kupries added on 2009-10-07 02:24:40:

File Added - 345544: sf-2872536.patch

apnadkarni added on 2009-10-04 20:49:06:
Below is an alternative implementation that I think fixes both above problems. But note the DANGER comment about other platforms. And only tried on 8.6 though I believe should work with 8.4

---
proc ::fileutil::stripPath {prefix path} {
# [file split] is used to generate a canonical form for both
# paths, for easy comparison, and also one which is easy to modify
# using list commands.

# Convert \ -> /
set prefix [file join $prefix]
set jpath [file join $path]
set prefix_len [string length $prefix]
if {[string equal -nocase -length $prefix_len $prefix $jpath]} {
    set jpath_len [string length $jpath]
    # Optimize for common case where path is a prefix
    if {$jpath_len > $prefix_len} {
# DANGER DANGER - is path separator after a join (above)
# a "/" on ALL platforms ? True on Win, Unix and OS X. Others?
if {[string index $jpath $prefix_len] eq "/"} {
    return [string range $jpath [incr prefix_len] end]
}
    }
    if {$jpath_len == $prefix_len} {return "."}
}
return $path
}

apnadkarni added on 2009-10-04 19:50:44:
Another problem -
fileutil::stripPath c:/temp/foo/bar c:/temp/foo\\bar
generates a Tcl error as it executes a file join with no arguments.

Attachments: