Ответ на сообщение «Re: col() in lines with apostrophe characters», 
присланное в 20:15:39 14 марта 2010, Воскресенье,
отправитель Ingo Karkat:

>      let len = strlen(substitute(str, ".", "x", "g"))
Never use this. It is three orders of magnitude slower than
    let len=len(split(str, '\zs'))

You can test this by launching
    vim -u NONE -c 'source test.vim'
My results are located in «strprofile» file.

Note that both methods assume that one symbol is symbol+combining diacritics 
(if 
any).

Текст сообщения:
> On 14-Mar-2010 17:49, Gregor Uhlenheuer wrote:
> > Hello,
> > I think the col() function does not work properly for lines with
> > apostrophe characters (`´).
> >
> > :echo col([line('.'), '$']) returns 6 on the line below:
> >
> > ´foo
> >
> > It should return 5 I think.
> 
> It depends on the encoding of the apostrophe character, as col() returns
>  byte indices, not logical characters. You assume that the apostrophe uses
>  only one byte, whereas it seems that it actually uses two bytes.
> 
> To investigate, check the 'encoding' and 'fileencoding' settings, and use
>  the g8 command on the apostrophe character; it'll return the (UTF-8) byte
>  sequence used to encode it. (This is a somewhat simplified explanation,
>  it's a quite complex matter.)
> 
> For many scripting uses, Vim's byte-orientation makes it difficult to
>  properly deal with multi-byte strings, but there are ways to cope. For
>  example, to count the number of characters, use
>      let len = strlen(substitute(str, ".", "x", "g"))
> 
> -- regards, ingo
> 
FUNCTION  Test1()
Called 1 time
Total time:   2.502438
 Self time:   2.502438

count  total (s)   self (s)
    1              2.502431     return len(substitute(a:str, '.', '.', 'g'))

FUNCTION  Test2()
Called 1 time
Total time:   0.036501
 Self time:   0.036501

count  total (s)   self (s)
    1              0.036494     return len(split(a:str, '\zs'))

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
    1   2.502438             Test1()
    1   0.036501             Test2()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
    1              2.502438  Test1()
    1              0.036501  Test2()

let s:str=string(range(1, 10000))
function Test1(str)
    return len(substitute(a:str, '.', '.', 'g'))
endfunction
function Test2(str)
    return len(split(a:str, '\zs'))
endfunction
profile start strprofile
profile func *
call Test1(s:str)
call Test2(s:str)
q!

Attachment: signature.asc
Description: This is a digitally signed message part.

Reply via email to