On 09/09/2013 10:56, Nikolay Pavlov wrote:
On Sep 9, 2013 1:40 PM, "Mike Williams" <[email protected]>
wrote:

Hi,

I have hit a difference in behaviour between unix and windows with the
regex comparison operator (7.4p22 on both).  Save the following viml code
to a file and do source %.  On windows the second function will do the
throw, on unix it doesn't.  The first function is a simpler analogy just to
check my sanity with the function logic.  (The aim of the second function
is to ensure that a relative path does not go above a parent directory.)

let s:eg1 = "abc"
func! s:strtest(altstr)
         let l:s = s:eg1 . a:altstr
         echo l:s . " -- " . s:eg1
         if l:s !~ "^" . s:eg1
                 throw "Danger"
         endif
         return l:s
endfunc
echo s:strtest('d')

let s:dir1 = getcwd()
func! s:dirtest(subdir)
         let l:d = fnamemodify(s:dir1 . "/" . a:subdir, ":p")
         echo l:d . " -- " . s:dir1
         if l:d !~ "^" . s:dir1
                 throw "Awooga"
         endif
         return l:d
endfunc
echo s:dirtest('fred')

Have I missed some subtlety in regexp comparison, windows file handling,
or fnamemodify() behaviour?  Should it work on windows and unix?  I tried
using !~? in case it was something to do with case insensitive windows file
system, no different.  I have also tried escape(..., '\\') in case it is
the back slashes in the file name.

Bashes with a clue stick welcome.  If someone has pointers to VIM
internals I am happy to poke around but I have no idea currently where to
start with (well I guess the !~ operator to see what it is seeing).

If you echo both names the difference should become obvious: in fnamemodify
call you use forward slash. To determine current path separator you can use
fnamemodify('.', ':p')[-1:].

Yep, the difference between unix and windows is obvious. Using a forward slash should make no difference since it will appear after s:dir1 which is all that I care about here.

Also note that
a) you should not compare directories this way: you need to escape all
metacharacters (normally done by escaping backslash and prepending \V, see
:h /\V.

I had tried calls to escape(..., '\') and that made no difference. Changing the test

        if l:d !~ "^\V" . s:dir1

makes no difference.

If I substitute all back slashes for forward slashes then it works as I expect. So basically I tripped and fallen into backslash hell, again.

b) you do not need regular expressions at all, use l:d[:len(s:dir1)-1] is#
s:dir1 (use is? for windows).

Which does work (toss up between using is# and ==# in this case) - thanks :-)

TIA - TTFN

Mike
--
Old enough to know better, but young enough to not care.

--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google
Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.



Mike
--
Old enough to know better, but young enough to not care.

--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Raspunde prin e-mail lui