Hi lith!
On So, 11 Nov 2012, lith wrote:
> Am Sonntag, 11. November 2012 10:03:49 UTC+1 schrieb ZyX:
> > > Is this intended? Is there an (easy) way to make printf() respect
> > > multi-byte encodings?
> > It is clearly stated in documentation that printf() operates with
> > bytes.
>
> My question rather was am I missing something since I personally find
> this behaviour rather useless in the context of a text editor.
>
> A formatted string usually is something that should eventually be
> displayed in a vim buffer. When I want to get a string padded with
> whitespace, I'm almost always rather interested in its display width
> than in its size in bytes -- and I can hardly imagine a use case where
> it would be otherwise.
>
> I can work around this problem by adjusting the width dependent on the
> difference between len(s) and strwidth(s) but I personally find this
> unnecessarily complicated.
I guess, we could use the 'S' type for printf() to specify length is
given in char (see patch).
regards,
Christian
--
--
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
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -4451,6 +4451,7 @@
Often used items are:
%s string
+ %6S string right-aligned in 6 chars
%6s string right-aligned in 6 bytes
%.9s string truncated to 9 bytes
%c single byte
@@ -4565,6 +4566,9 @@
s The text of the String argument is used. If a
precision is specified, no more bytes than the number
specified are used.
+ S The text of the String argument is used. If a
+ precision is specified, no more chars than the number
+ specified are used.
*printf-f* *E807*
f The Float argument is converted into a string of the
diff --git a/src/message.c b/src/message.c
--- a/src/message.c
+++ b/src/message.c
@@ -4321,6 +4321,9 @@
case '%':
case 'c':
case 's':
+#ifdef FEAT_MBYTE
+ case 'S':
+#endif
length_modifier = '\0';
str_arg_l = 1;
switch (fmt_spec)
@@ -4349,6 +4352,9 @@
}
case 's':
+#ifdef FEAT_MBYTE
+ case 'S':
+#endif
str_arg =
#ifndef HAVE_STDARG_H
(char *)get_a_arg(arg_idx);
@@ -4385,6 +4391,23 @@
str_arg_l = (q == NULL) ? precision
: (size_t)(q - str_arg);
}
+#ifdef FEAT_MBYTE
+ if (fmt_spec == 'S')
+ {
+ if (min_field_width)
+ min_field_width += STRLEN(str_arg) - mb_string2cells((char_u*) str_arg, -1);
+ if (precision)
+ {
+ char_u *p1 = (char_u*) str_arg;
+ int i;
+
+ for (i = 0; i < precision && *p1; i++)
+ p1 += mb_ptr2len(p1);
+
+ str_arg_l = precision = p1 - (char_u *) str_arg;
+ }
+ }
+#endif
break;
default: