Skip to content

Commit

Permalink
utf8: pick_one_utf8_char()
Browse files Browse the repository at this point in the history
utf8_width() function was doing two different things.  To pick a
valid character from UTF-8 stream, and compute the display width of
that character.  This splits the former to a separate function
pick_one_utf8_char().

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Junio C Hamano committed Jan 7, 2008
1 parent 40f162b commit 396ccf1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
27 changes: 21 additions & 6 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */

typedef unsigned int ucs_char_t; /* assuming 32bit int */

struct interval {
int first;
int last;
Expand Down Expand Up @@ -153,11 +151,14 @@ static int git_wcwidth(ucs_char_t ch)
}

/*
* This function returns the number of columns occupied by the character
* pointed to by the variable start. The pointer is updated to point at
* the next character. If it was not valid UTF-8, the pointer is set to NULL.
* Pick one ucs character starting from the location *start points at,
* and return it, while updating the *start pointer to point at the
* end of that character.
*
* If the string was not a valid UTF-8, *start pointer is set to NULL
* and the return value is undefined.
*/
int utf8_width(const char **start)
ucs_char_t pick_one_utf8_char(const char **start)
{
unsigned char *s = (unsigned char *)*start;
ucs_char_t ch;
Expand Down Expand Up @@ -208,6 +209,20 @@ int utf8_width(const char **start)
return 0;
}

return ch;
}

/*
* This function returns the number of columns occupied by the character
* pointed to by the variable start. The pointer is updated to point at
* the next character. If it was not valid UTF-8, the pointer is set to
* NULL.
*/
int utf8_width(const char **start)
{
ucs_char_t ch = pick_one_utf8_char(start);
if (!*start)
return 0;
return git_wcwidth(ch);
}

Expand Down
3 changes: 3 additions & 0 deletions utf8.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef GIT_UTF8_H
#define GIT_UTF8_H

typedef unsigned int ucs_char_t; /* assuming 32bit int */

ucs_char_t pick_one_utf8_char(const char **start);
int utf8_width(const char **start);
int is_utf8(const char *text);
int is_encoding_utf8(const char *name);
Expand Down

0 comments on commit 396ccf1

Please sign in to comment.