Skip to content

Commit 8139f7a

Browse files
committed
UTF8 encoding support.
1 parent 97d2850 commit 8139f7a

11 files changed

+37978
-60
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, and Android.

encodings/tools/EastAsianWidth.txt

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

encodings/tools/UnicodeData.txt

+34,626
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 =~ /^(.*?)(?:\.\.(.*?))?;[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

+464
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
+28-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
#include <string.h>
44
#include "linenoise.h"
55

6+
#define UTF8
7+
8+
#ifdef UTF8
9+
#include "encodings/utf8.h"
10+
#endif
611

712
void completion(const char *buf, linenoiseCompletions *lc) {
813
if (buf[0] == 'h') {
14+
#ifdef UTF8
15+
linenoiseAddCompletion(lc,"hello こんにちは");
16+
linenoiseAddCompletion(lc,"hello こんにちは there");
17+
linenoiseAddCompletion(lc,"hello こんにちは 👨‍💻");
18+
#else
919
linenoiseAddCompletion(lc,"hello");
1020
linenoiseAddCompletion(lc,"hello there");
21+
#endif
1122
}
1223
}
1324

@@ -17,6 +28,11 @@ char *hints(const char *buf, int *color, int *bold) {
1728
*bold = 0;
1829
return " World";
1930
}
31+
if (!strcasecmp(buf,"こんにちは")) {
32+
*color = 35;
33+
*bold = 0;
34+
return " 世界";
35+
}
2036
return NULL;
2137
}
2238

@@ -40,6 +56,13 @@ int main(int argc, char **argv) {
4056
}
4157
}
4258

59+
#ifdef UTF8
60+
linenoiseSetEncodingFunctions(
61+
linenoiseUtf8PrevCharLen,
62+
linenoiseUtf8NextCharLen,
63+
linenoiseUtf8ReadCode);
64+
#endif
65+
4366
/* Set the completion callback. This will be called every time the
4467
* user uses the <tab> key. */
4568
linenoiseSetCompletionCallback(completion);
@@ -55,8 +78,11 @@ int main(int argc, char **argv) {
5578
*
5679
* The typed string is returned as a malloc() allocated string by
5780
* linenoise, so the user needs to free() it. */
58-
59-
while((line = linenoise("hello> ")) != NULL) {
81+
#ifdef UTF8
82+
while((line = linenoise("\033[32mこんにちは\x1b[0m> ")) != NULL) {
83+
#else
84+
while((line = linenoise("\033[32mhello\x1b[0m> ")) != NULL) {
85+
#endif
6086
/* Do something with the string. */
6187
if (line[0] != '\0' && line[0] != '/') {
6288
printf("echo: '%s'\n", line);

0 commit comments

Comments
 (0)