Skip to content

Commit 317bfb6

Browse files
committed
UTF8 encoding support. (Unicode 16.0.0)
1 parent d895173 commit 317bfb6

11 files changed

+43568
-51
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
linenoise_example: linenoise.h linenoise.c
1+
linenoise_example: linenoise.h linenoise.c encodings/utf8.h encodings/utf8.c
22

3-
linenoise_example: linenoise.c example.c
4-
$(CC) -Wall -W -Os -g -o linenoise_example linenoise.c example.c
3+
linenoise_example: linenoise.c example.c encodings/utf8.c
4+
$(CC) -Wall -W -Os -g -o linenoise_example linenoise.c example.c encodings/utf8.c
55

66
clean:
77
rm -f linenoise_example

README.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Linenoise
1+
# Linenoise UTF-8
22

33
A minimal, zero-config, BSD licensed, readline replacement used in Redis,
44
MongoDB, Android and many other projects.

encodings/tools/EastAsianWidth.txt

+2,686
Large diffs are not rendered by default.

encodings/tools/UnicodeData.txt

+40,116
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ARGF.each do |line|
2+
if line =~ /^(.*);.*;Mn;/
3+
puts "0x#{$1},"
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ranges = []
2+
ARGF.each do |line|
3+
if line =~ /^(.*?)(?:\.\.(.*?))?\s+;\s+[FW]\s+# .*$/
4+
first = $1.to_i(16)
5+
last = $2 ? $2.to_i(16) : first
6+
if ranges.last && ranges.last[:l] + 1 == first
7+
ranges.last[:l] = last
8+
else
9+
ranges.push({ f: first, l: last })
10+
end
11+
end
12+
end
13+
ranges.each do |range|
14+
puts "{ #{'0x%X' % range[:f]}, #{'0x%X' % range[:l]} },"
15+
end

encodings/utf8.c

+473
Large diffs are not rendered by default.

encodings/utf8.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* encodings/utf8.h -- VERSION 1.0
2+
*
3+
* Guerrilla line editing library against the idea that a line editing lib
4+
* needs to be 20,000 lines of C code.
5+
*
6+
* See linenoise.c for more information.
7+
*
8+
* ------------------------------------------------------------------------
9+
*
10+
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
11+
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
12+
*
13+
* All rights reserved.
14+
*
15+
* Redistribution and use in source and binary forms, with or without
16+
* modification, are permitted provided that the following conditions are
17+
* met:
18+
*
19+
* * Redistributions of source code must retain the above copyright
20+
* notice, this list of conditions and the following disclaimer.
21+
*
22+
* * Redistributions in binary form must reproduce the above copyright
23+
* notice, this list of conditions and the following disclaimer in the
24+
* documentation and/or other materials provided with the distribution.
25+
*
26+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37+
*/
38+
39+
#ifndef __LINENOISE_ENCODINGS_UTF8_H
40+
#define __LINENOISE_ENCODINGS_UTF8_H
41+
42+
#ifdef __cplusplus
43+
extern "C" {
44+
#endif
45+
46+
size_t linenoiseUtf8PrevCharLen(const char* buf, size_t buf_len, size_t pos, size_t *col_len);
47+
size_t linenoiseUtf8NextCharLen(const char* buf, size_t buf_len, size_t pos, size_t *col_len);
48+
size_t linenoiseUtf8ReadCode(int fd, char* buf, size_t buf_len, int* cp);
49+
50+
#ifdef __cplusplus
51+
}
52+
#endif
53+
54+
#endif /* __LINENOISE_ENCODINGS_UTF8_H */
55+

example.c

100644100755
+29-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@
44
#include <sys/select.h>
55
#include "linenoise.h"
66

7+
#define UTF8
8+
9+
#ifdef UTF8
10+
#include "encodings/utf8.h"
11+
#endif
12+
713
void completion(const char *buf, linenoiseCompletions *lc) {
814
if (buf[0] == 'h') {
15+
#ifdef UTF8
16+
linenoiseAddCompletion(lc,"hello こんにちは");
17+
linenoiseAddCompletion(lc,"hello こんにちは there");
18+
linenoiseAddCompletion(lc,"hello こんにちは 👨‍💻");
19+
#else
920
linenoiseAddCompletion(lc,"hello");
1021
linenoiseAddCompletion(lc,"hello there");
22+
#endif
1123
}
1224
}
1325

@@ -17,6 +29,11 @@ char *hints(const char *buf, int *color, int *bold) {
1729
*bold = 0;
1830
return " World";
1931
}
32+
if (!strcasecmp(buf,"こんにちは")) {
33+
*color = 35;
34+
*bold = 0;
35+
return " 世界";
36+
}
2037
return NULL;
2138
}
2239

@@ -43,6 +60,13 @@ int main(int argc, char **argv) {
4360
}
4461
}
4562

63+
#ifdef UTF8
64+
linenoiseSetEncodingFunctions(
65+
linenoiseUtf8PrevCharLen,
66+
linenoiseUtf8NextCharLen,
67+
linenoiseUtf8ReadCode);
68+
#endif
69+
4670
/* Set the completion callback. This will be called every time the
4771
* user uses the <tab> key. */
4872
linenoiseSetCompletionCallback(completion);
@@ -61,7 +85,11 @@ int main(int argc, char **argv) {
6185

6286
while(1) {
6387
if (!async) {
64-
line = linenoise("hello> ");
88+
#ifdef UTF8
89+
line = linenoise("\033[32mこんにちは\x1b[0m> ");
90+
#else
91+
line = linenoise("\033[32mhello\x1b[0m> ");
92+
#endif
6593
if (line == NULL) break;
6694
} else {
6795
/* Asynchronous mode using the multiplexing API: wait for

0 commit comments

Comments
 (0)