utf8: pick_one_utf8_char()
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>
Junio C Hamano committed
Jan 6, 2008 at 19:02 UTC
396ccf1fcb91f31d5060555cbffc35bf24172a35
2 files changed
+24
-6
utf8.c
+21
-6
@@ -3,8 +3,6 @@
3
4
/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
5
6
-typedef unsigned int ucs_char_t; /* assuming 32bit int */
7
-
6
struct interval {
7
int first;
8
int last;
@@ -153,11 +151,14 @@ static int git_wcwidth(ucs_char_t ch)
151
}
152
153
/*
156
- * This function returns the number of columns occupied by the character
157
- * pointed to by the variable start. The pointer is updated to point at
158
- * the next character. If it was not valid UTF-8, the pointer is set to NULL.
154
+ * Pick one ucs character starting from the location *start points at,
155
+ * and return it, while updating the *start pointer to point at the
156
+ * end of that character.
157
+ *
158
+ * If the string was not a valid UTF-8, *start pointer is set to NULL
159
+ * and the return value is undefined.
160
*/
160
-int utf8_width(const char **start)
161
+ucs_char_t pick_one_utf8_char(const char **start)
162
{
163
unsigned char *s = (unsigned char *)*start;
164
ucs_char_t ch;
@@ -208,6 +209,20 @@ invalid:
209
return 0;
210
}
211
212
+ return ch;
213
+}
214
+
215
+/*
216
+ * This function returns the number of columns occupied by the character
217
+ * pointed to by the variable start. The pointer is updated to point at
218
+ * the next character. If it was not valid UTF-8, the pointer is set to
219
+ * NULL.
220
+ */
221
+int utf8_width(const char **start)
222
+{
223
+ ucs_char_t ch = pick_one_utf8_char(start);
224
+ if (!*start)
225
+ return 0;
226
return git_wcwidth(ch);
227
}
228
utf8.h
+3
@@ -1,6 +1,9 @@
1
#ifndef GIT_UTF8_H
2
#define GIT_UTF8_H
3
4
+typedef unsigned int ucs_char_t; /* assuming 32bit int */
5
+
6
+ucs_char_t pick_one_utf8_char(const char **start);
7
int utf8_width(const char **start);
8
int is_utf8(const char *text);
9
int is_encoding_utf8(const char *name);