dcaee6
To: vim_dev@googlegroups.com
dcaee6
Subject: Patch 7.4.057
dcaee6
Fcc: outbox
dcaee6
From: Bram Moolenaar <Bram@moolenaar.net>
dcaee6
Mime-Version: 1.0
dcaee6
Content-Type: text/plain; charset=UTF-8
dcaee6
Content-Transfer-Encoding: 8bit
dcaee6
------------
dcaee6
dcaee6
Patch 7.4.057                                 
dcaee6
Problem:    byteidx() does not work for composing characters.
dcaee6
Solution:   Add byteidxcomp().
dcaee6
Files:      src/eval.c, src/testdir/test69.in, src/testdir/test69.ok,
dcaee6
            runtime/doc/eval.txt
dcaee6
dcaee6
dcaee6
*** ../vim-7.4.056/src/eval.c	2013-10-02 16:46:23.000000000 +0200
dcaee6
--- src/eval.c	2013-11-02 22:30:08.000000000 +0100
dcaee6
***************
dcaee6
*** 474,480 ****
dcaee6
--- 474,482 ----
dcaee6
  static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
  static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
  static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
+ static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
dcaee6
  static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
+ static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
  static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
  #ifdef FEAT_FLOAT
dcaee6
  static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
dcaee6
***************
dcaee6
*** 7861,7866 ****
dcaee6
--- 7863,7869 ----
dcaee6
      {"bufwinnr",	1, 1, f_bufwinnr},
dcaee6
      {"byte2line",	1, 1, f_byte2line},
dcaee6
      {"byteidx",		2, 2, f_byteidx},
dcaee6
+     {"byteidxcomp",	2, 2, f_byteidxcomp},
dcaee6
      {"call",		2, 3, f_call},
dcaee6
  #ifdef FEAT_FLOAT
dcaee6
      {"ceil",		1, 1, f_ceil},
dcaee6
***************
dcaee6
*** 9177,9189 ****
dcaee6
  #endif
dcaee6
  }
dcaee6
  
dcaee6
- /*
dcaee6
-  * "byteidx()" function
dcaee6
-  */
dcaee6
      static void
dcaee6
! f_byteidx(argvars, rettv)
dcaee6
      typval_T	*argvars;
dcaee6
      typval_T	*rettv;
dcaee6
  {
dcaee6
  #ifdef FEAT_MBYTE
dcaee6
      char_u	*t;
dcaee6
--- 9180,9190 ----
dcaee6
  #endif
dcaee6
  }
dcaee6
  
dcaee6
      static void
dcaee6
! byteidx(argvars, rettv, comp)
dcaee6
      typval_T	*argvars;
dcaee6
      typval_T	*rettv;
dcaee6
+     int		comp;
dcaee6
  {
dcaee6
  #ifdef FEAT_MBYTE
dcaee6
      char_u	*t;
dcaee6
***************
dcaee6
*** 9203,9209 ****
dcaee6
      {
dcaee6
  	if (*t == NUL)		/* EOL reached */
dcaee6
  	    return;
dcaee6
! 	t += (*mb_ptr2len)(t);
dcaee6
      }
dcaee6
      rettv->vval.v_number = (varnumber_T)(t - str);
dcaee6
  #else
dcaee6
--- 9204,9213 ----
dcaee6
      {
dcaee6
  	if (*t == NUL)		/* EOL reached */
dcaee6
  	    return;
dcaee6
! 	if (enc_utf8 && comp)
dcaee6
! 	    t += utf_ptr2len(t);
dcaee6
! 	else
dcaee6
! 	    t += (*mb_ptr2len)(t);
dcaee6
      }
dcaee6
      rettv->vval.v_number = (varnumber_T)(t - str);
dcaee6
  #else
dcaee6
***************
dcaee6
*** 9212,9217 ****
dcaee6
--- 9216,9243 ----
dcaee6
  #endif
dcaee6
  }
dcaee6
  
dcaee6
+ /*
dcaee6
+  * "byteidx()" function
dcaee6
+  */
dcaee6
+     static void
dcaee6
+ f_byteidx(argvars, rettv)
dcaee6
+     typval_T	*argvars;
dcaee6
+     typval_T	*rettv;
dcaee6
+ {
dcaee6
+     byteidx(argvars, rettv, FALSE);
dcaee6
+ }
dcaee6
+ 
dcaee6
+ /*
dcaee6
+  * "byteidxcomp()" function
dcaee6
+  */
dcaee6
+     static void
dcaee6
+ f_byteidxcomp(argvars, rettv)
dcaee6
+     typval_T	*argvars;
dcaee6
+     typval_T	*rettv;
dcaee6
+ {
dcaee6
+     byteidx(argvars, rettv, TRUE);
dcaee6
+ }
dcaee6
+ 
dcaee6
      int
dcaee6
  func_call(name, args, selfdict, rettv)
dcaee6
      char_u	*name;
dcaee6
*** ../vim-7.4.056/src/testdir/test69.in	2013-03-07 18:30:50.000000000 +0100
dcaee6
--- src/testdir/test69.in	2013-11-02 22:46:02.000000000 +0100
dcaee6
***************
dcaee6
*** 1,6 ****
dcaee6
--- 1,7 ----
dcaee6
  Test for multi-byte text formatting.
dcaee6
  Also test, that 'mps' with multibyte chars works.
dcaee6
  And test "ra" on multi-byte characters.
dcaee6
+ Also test byteidx() and byteidxcomp()
dcaee6
  
dcaee6
  STARTTEST
dcaee6
  :so mbyte.vim
dcaee6
***************
dcaee6
*** 154,159 ****
dcaee6
--- 155,175 ----
dcaee6
  aab
dcaee6
  
dcaee6
  STARTTEST
dcaee6
+ :let a = '.é.' " one char of two bytes
dcaee6
+ :let b = '.é.' " normal e with composing char
dcaee6
+ /^byteidx
dcaee6
+ :put =string([byteidx(a, 0), byteidx(a, 1), byteidx(a, 2), byteidx(a, 3), byteidx(a, 4)])
dcaee6
+ :put =string([byteidx(b, 0), byteidx(b, 1), byteidx(b, 2), byteidx(b, 3), byteidx(b, 4)])
dcaee6
+ /^byteidxcomp
dcaee6
+ :put =string([byteidxcomp(a, 0), byteidxcomp(a, 1), byteidxcomp(a, 2), byteidxcomp(a, 3), byteidxcomp(a, 4)])
dcaee6
+ :let b = '.é.'
dcaee6
+ :put =string([byteidxcomp(b, 0), byteidxcomp(b, 1), byteidxcomp(b, 2), byteidxcomp(b, 3), byteidxcomp(b, 4), byteidxcomp(b, 5)])
dcaee6
+ ENDTEST
dcaee6
+ 
dcaee6
+ byteidx
dcaee6
+ byteidxcomp
dcaee6
+ 
dcaee6
+ STARTTEST
dcaee6
  :g/^STARTTEST/.,/^ENDTEST/d
dcaee6
  :1;/^Results/,$wq! test.out
dcaee6
  ENDTEST
dcaee6
*** ../vim-7.4.056/src/testdir/test69.ok	2013-03-07 18:31:32.000000000 +0100
dcaee6
--- src/testdir/test69.ok	2013-11-02 22:43:25.000000000 +0100
dcaee6
***************
dcaee6
*** 149,151 ****
dcaee6
--- 149,159 ----
dcaee6
  aaaa
dcaee6
  aaa
dcaee6
  
dcaee6
+ 
dcaee6
+ byteidx
dcaee6
+ [0, 1, 3, 4, -1]
dcaee6
+ [0, 1, 4, 5, -1]
dcaee6
+ byteidxcomp
dcaee6
+ [0, 1, 3, 4, -1]
dcaee6
+ [0, 1, 2, 4, 5, -1]
dcaee6
+ 
dcaee6
*** ../vim-7.4.056/runtime/doc/eval.txt	2013-08-10 13:24:53.000000000 +0200
dcaee6
--- runtime/doc/eval.txt	2013-11-02 23:27:24.000000000 +0100
dcaee6
***************
dcaee6
*** 1712,1717 ****
dcaee6
--- 1713,1719 ----
dcaee6
  bufwinnr( {expr})		Number	window number of buffer {expr}
dcaee6
  byte2line( {byte})		Number	line number at byte count {byte}
dcaee6
  byteidx( {expr}, {nr})		Number	byte index of {nr}'th char in {expr}
dcaee6
+ byteidxcomp( {expr}, {nr})	Number	byte index of {nr}'th char in {expr}
dcaee6
  call( {func}, {arglist} [, {dict}])
dcaee6
  				any	call {func} with arguments {arglist}
dcaee6
  ceil( {expr})			Float	round {expr} up
dcaee6
***************
dcaee6
*** 2260,2266 ****
dcaee6
  		{expr}.  Use zero for the first character, it returns zero.
dcaee6
  		This function is only useful when there are multibyte
dcaee6
  		characters, otherwise the returned value is equal to {nr}.
dcaee6
! 		Composing characters are counted as a separate character.
dcaee6
  		Example : >
dcaee6
  			echo matchstr(str, ".", byteidx(str, 3))
dcaee6
  <		will display the fourth character.  Another way to do the
dcaee6
--- 2262,2271 ----
dcaee6
  		{expr}.  Use zero for the first character, it returns zero.
dcaee6
  		This function is only useful when there are multibyte
dcaee6
  		characters, otherwise the returned value is equal to {nr}.
dcaee6
! 		Composing characters are not counted separately, their byte
dcaee6
! 		length is added to the preceding base character.  See
dcaee6
! 		|byteidxcomp()| below for counting composing characters
dcaee6
! 		separately.
dcaee6
  		Example : >
dcaee6
  			echo matchstr(str, ".", byteidx(str, 3))
dcaee6
  <		will display the fourth character.  Another way to do the
dcaee6
***************
dcaee6
*** 2269,2275 ****
dcaee6
  			echo strpart(s, 0, byteidx(s, 1))
dcaee6
  <		If there are less than {nr} characters -1 is returned.
dcaee6
  		If there are exactly {nr} characters the length of the string
dcaee6
! 		is returned.
dcaee6
  
dcaee6
  call({func}, {arglist} [, {dict}])			*call()* *E699*
dcaee6
  		Call function {func} with the items in |List| {arglist} as
dcaee6
--- 2274,2293 ----
dcaee6
  			echo strpart(s, 0, byteidx(s, 1))
dcaee6
  <		If there are less than {nr} characters -1 is returned.
dcaee6
  		If there are exactly {nr} characters the length of the string
dcaee6
! 		in bytes is returned.
dcaee6
! 
dcaee6
! byteidxcomp({expr}, {nr})					*byteidxcomp()*
dcaee6
! 		Like byteidx(), except that a composing character is counted
dcaee6
! 		as a separate character.  Example: >
dcaee6
! 			let s = 'e' . nr2char(0x301)
dcaee6
! 			echo byteidx(s, 1)
dcaee6
! 			echo byteidxcomp(s, 1)
dcaee6
! 			echo byteidxcomp(s, 2)
dcaee6
! <		The first and third echo result in 3 ('e' plus composing
dcaee6
! 		character is 3 bytes), the second echo results in 1 ('e' is
dcaee6
! 		one byte).
dcaee6
! 		Only works different from byteidx() when 'encoding' is set to
dcaee6
! 		a Unicode encoding.
dcaee6
  
dcaee6
  call({func}, {arglist} [, {dict}])			*call()* *E699*
dcaee6
  		Call function {func} with the items in |List| {arglist} as
dcaee6
*** ../vim-7.4.056/src/version.c	2013-11-02 21:49:28.000000000 +0100
dcaee6
--- src/version.c	2013-11-02 22:45:13.000000000 +0100
dcaee6
***************
dcaee6
*** 740,741 ****
dcaee6
--- 740,743 ----
dcaee6
  {   /* Add new patch number below this line */
dcaee6
+ /**/
dcaee6
+     57,
dcaee6
  /**/
dcaee6
dcaee6
-- 
dcaee6
Any sufficiently advanced technology is indistinguishable from magic.
dcaee6
					Arthur C. Clarke
dcaee6
Any sufficiently advanced bug is indistinguishable from a feature.
dcaee6
                                        Rich Kulawiec
dcaee6
dcaee6
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
dcaee6
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
dcaee6
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
dcaee6
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///