@@ -90,10 +90,10 @@ struct block_def {
90
90
colnr_T start_char_vcols ; /* number of vcols of pre-block char */
91
91
};
92
92
93
-
94
93
#ifdef INCLUDE_GENERATED_DECLARATIONS
95
94
# include "ops.c.generated.h"
96
95
#endif
96
+
97
97
/*
98
98
* The names of operators.
99
99
* IMPORTANT: Index must correspond with defines in vim.h!!!
@@ -4704,29 +4704,48 @@ get_reg_contents (
4704
4704
return retval ;
4705
4705
}
4706
4706
4707
- /*
4708
- * Store string "str" in register "name".
4709
- * "maxlen" is the maximum number of bytes to use, -1 for all bytes.
4710
- * If "must_append" is TRUE, always append to the register. Otherwise append
4711
- * if "name" is an uppercase letter.
4712
- * Note: "maxlen" and "must_append" don't work for the "/" register.
4713
- * Careful: 'str' is modified, you may have to use a copy!
4714
- * If "str" ends in '\n' or '\r', use linewise, otherwise use characterwise.
4715
- */
4716
- void write_reg_contents (int name , char_u * str , int maxlen , int must_append )
4707
+ /// write_reg_contents - store `str` in register `name`
4708
+ ///
4709
+ /// @see write_reg_contents_ex
4710
+ void write_reg_contents (int name ,
4711
+ const char_u * str ,
4712
+ ssize_t len ,
4713
+ int must_append )
4717
4714
{
4718
- write_reg_contents_ex (name , str , maxlen , must_append , MAUTO , 0L );
4715
+ write_reg_contents_ex (name , str , len , must_append , MAUTO , 0L );
4719
4716
}
4720
4717
4721
- void write_reg_contents_ex (int name , char_u * str , int maxlen , int must_append , int yank_type , long block_len )
4718
+ /// write_reg_contents_ex - store `str` in register `name`
4719
+ ///
4720
+ /// If `str` ends in '\n' or '\r', use linewise, otherwise use
4721
+ /// characterwise.
4722
+ ///
4723
+ /// @warning when `name` is '/', `len` and `must_append` are ignored. This
4724
+ /// means that `str` MUST be NUL-terminated.
4725
+ ///
4726
+ /// @param name The name of the register
4727
+ /// @param str The contents to write
4728
+ /// @param len If >= 0, write `len` bytes of `str`. Otherwise, write
4729
+ /// `strlen(str)` bytes. If `len` is larger than the
4730
+ /// allocated size of `src`, the behaviour is undefined.
4731
+ /// @param must_append If true, append the contents of `str` to the current
4732
+ /// contents of the register. Note that regardless of
4733
+ /// `must_append`, this function will append when `name`
4734
+ /// is an uppercase letter.
4735
+ /// @param yank_type MCHAR, MLINE, MBLOCK or MAUTO
4736
+ /// @param block_len width of visual block
4737
+ void write_reg_contents_ex (int name ,
4738
+ const char_u * str ,
4739
+ ssize_t len ,
4740
+ int must_append ,
4741
+ int yank_type ,
4742
+ long block_len )
4722
4743
{
4723
4744
struct yankreg * old_y_previous , * old_y_current ;
4724
- long len ;
4725
4745
4726
- if (maxlen >= 0 )
4727
- len = maxlen ;
4728
- else
4729
- len = (long )STRLEN (str );
4746
+ if (len < 0 ) {
4747
+ len = (ssize_t ) STRLEN (str );
4748
+ }
4730
4749
4731
4750
/* Special case: '/' search pattern */
4732
4751
if (name == '/' ) {
@@ -4735,17 +4754,25 @@ void write_reg_contents_ex(int name, char_u *str, int maxlen, int must_append, i
4735
4754
}
4736
4755
4737
4756
if (name == '=' ) {
4738
- char_u * p , * s ;
4739
-
4740
- p = vim_strnsave (str , (int )len );
4757
+ size_t offset = 0 ;
4758
+ size_t totlen = (size_t ) len ;
4741
4759
4742
- if (must_append ) {
4743
- s = concat_str ( get_expr_line_src (), p );
4744
- free ( p );
4745
- p = s ;
4760
+ if (must_append && expr_line ) {
4761
+ // append has been specified and expr_line already exists, so we'll
4762
+ // append the new string to expr_line.
4763
+ size_t exprlen = STRLEN ( expr_line ) ;
4746
4764
4765
+ totlen += exprlen ;
4766
+ offset = exprlen ;
4747
4767
}
4748
- set_expr_line (p );
4768
+
4769
+ // modify the global expr_line, extend/shrink it if necessary (realloc).
4770
+ // Copy the input string into the adjusted memory at the specified
4771
+ // offset.
4772
+ expr_line = xrealloc (expr_line , totlen + 1 );
4773
+ memcpy (expr_line + offset , str , (size_t ) len );
4774
+ expr_line [totlen ] = NUL ;
4775
+
4749
4776
return ;
4750
4777
}
4751
4778
@@ -4773,18 +4800,20 @@ void write_reg_contents_ex(int name, char_u *str, int maxlen, int must_append, i
4773
4800
y_current = old_y_current ;
4774
4801
}
4775
4802
4776
- /*
4777
- * Put a string into a register. When the register is not empty, the string
4778
- * is appended.
4779
- */
4780
- static void
4781
- str_to_reg (
4782
- struct yankreg * y_ptr , /* pointer to yank register */
4783
- int yank_type , /* MCHAR, MLINE, MBLOCK, MAUTO */
4784
- char_u * str , /* string to put in register */
4785
- long len , /* length of string */
4786
- long blocklen /* width of Visual block */
4787
- )
4803
+ /// str_to_reg - Put a string into a register.
4804
+ ///
4805
+ /// When the register is not empty, the string is appended.
4806
+ ///
4807
+ /// @param y_ptr pointer to yank register
4808
+ /// @param yank_type MCHAR, MLINE, MBLOCK or MAUTO
4809
+ /// @param str string to put in register
4810
+ /// @param len length of the string
4811
+ /// @param blocklen width of visual block
4812
+ static void str_to_reg (struct yankreg * y_ptr ,
4813
+ int yank_type ,
4814
+ const char_u * str ,
4815
+ long len ,
4816
+ long blocklen )
4788
4817
{
4789
4818
int type ; /* MCHAR, MLINE or MBLOCK */
4790
4819
int lnum ;
0 commit comments