author | Mikael Berthe <mikael@lilotux.net> |
Fri, 29 Feb 2008 14:55:55 +0100 | |
changeset 1443 | 0623d694a77f |
parent 930 | a75f7a13df7b |
permissions | -rw-r--r-- |
930
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
1 |
/* |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
2 |
* utf8.c -- UTF-8 routines |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
3 |
* |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
4 |
* Copyright (C) 2006 Reimar Döffinger <Reimar.Doeffinger@stud.uni-karlsruhe.de> |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
5 |
* |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
6 |
* This program is free software; you can redistribute it and/or modify |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
7 |
* it under the terms of the GNU General Public License as published by |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
* your option) any later version. |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
* |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
* This program is distributed in the hope that it will be useful, but |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
* General Public License for more details. |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
* |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
16 |
* You should have received a copy of the GNU General Public License |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
* along with this program; if not, write to the Free Software |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
* USA |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
*/ |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
#include "utf8.h" |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
char *prev_char(char *str, const char *limit) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
25 |
{ |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
if (str <= limit) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
str--; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
if (utf8_mode) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
while ((str > limit) && ((*str & 0xc0) == 0x80)) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
str--; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
34 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
char *next_char(char *str) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
{ |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
if (!*str) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
str++; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
if (utf8_mode) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
while ((*str & 0xc0) == 0x80) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
str++; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
43 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
unsigned get_char(const char *str) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
{ |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
unsigned char *strp = (unsigned char *)str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
unsigned c = *strp++; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
unsigned mask = 0x80; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
int len = -1; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
52 |
if (!utf8_mode) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
return c; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
while (c & mask) { |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 |
mask >>= 1; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
len++; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
if (len <= 0 || len > 4) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
goto no_utf8; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
c &= mask - 1; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
while ((*strp & 0xc0) == 0x80) { |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
62 |
if (len-- <= 0) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
goto no_utf8; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
c = (c << 6) | (*strp++ & 0x3f); |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
65 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
if (len) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
goto no_utf8; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
return c; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
no_utf8: |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
return *str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
char *put_char(char *str, unsigned c) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
{ |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 |
int mask = 0xffffffc0; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
int i = 4; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
char code[5]; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
if (!utf8_mode || c < 128) { |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
80 |
*str++ = c; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
81 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
82 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
83 |
while (c & mask) { |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
code[i--] = 0x80 | (c & 0x3f); |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
c >>= 6; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 |
mask >>= 1; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
if (i < 0) { |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
*str++ = '?'; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
code[i] = (mask << 1) | c; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
for (; i < 5; i++) |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
*str++ = code[i]; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
return str; |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
} |
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
|
a75f7a13df7b
UTF-8 terminal support (Reimar Döffinger)
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |