[systemd-devel] [PATCH] util: ellipsize_mem: do not print partial utf-8 characters

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed Aug 28 13:16:42 PDT 2013


On Wed, Aug 28, 2013 at 12:36:10PM -0700, Shawn Landden wrote:
> ---
>  src/shared/util.c | 13 ++++++++++---
>  1 file changed, 10 insertions(+), 3 deletions(-)
> 
> diff --git a/src/shared/util.c b/src/shared/util.c
> index 1dde8af..9af99aa 100644
> --- a/src/shared/util.c
> +++ b/src/shared/util.c
> @@ -3286,7 +3286,7 @@ int running_in_chroot(void) {
>  }
>  
>  char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigned percent) {
> -        size_t x;
> +        size_t x, j;
>          char *r;
>  
>          assert(s);
> @@ -3305,13 +3305,20 @@ char *ellipsize_mem(const char *s, size_t old_length, size_t new_length, unsigne
>          if (x > new_length - 3)
>                  x = new_length - 3;
>  
> +        for (;s[x-1] > 127;x--)
> +                continue;
> +
>          memcpy(r, s, x);
>          r[x] = '.';
>          r[x+1] = '.';
>          r[x+2] = '.';
> +
> +        for (j=(x+3);s[j] > 127;j++)
> +                continue;
> +
>          memcpy(r + x + 3,
> -               s + old_length - (new_length - x - 3),
> -               new_length - x - 3);
> +               s + old_length - (new_length - j),
> +               new_length - j);

That's a good step, but we should also calculate new_length properly, counting
characters not bytes. Just counting unicode units, without looking for combining
characters or anything like this should be enough imho.

Zbyszek


More information about the systemd-devel mailing list