1 /* mbutil.c -- readline multibyte character utility functions */
3 /* Copyright (C) 2001-2015 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
22 #define READLINE_LIBRARY
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
32 #if defined (HAVE_UNISTD_H)
33 # include <unistd.h> /* for _POSIX_VERSION */
34 #endif /* HAVE_UNISTD_H */
36 #if defined (HAVE_STDLIB_H)
39 # include "ansi_stdlib.h"
40 #endif /* HAVE_STDLIB_H */
45 /* System-specific feature definitions and include files. */
49 #if defined (TIOCSTAT_IN_SYS_IOCTL)
50 # include <sys/ioctl.h>
51 #endif /* TIOCSTAT_IN_SYS_IOCTL */
53 /* Some standard library routines. */
56 #include "rlprivate.h"
59 /* Declared here so it can be shared between the readline and history
61 #if defined (HANDLE_MULTIBYTE)
62 int rl_byte_oriented
= 0;
64 int rl_byte_oriented
= 1;
68 int _rl_utf8locale
= 0;
70 /* **************************************************************** */
72 /* Multibyte Character Utility Functions */
74 /* **************************************************************** */
76 #if defined(HANDLE_MULTIBYTE)
79 _rl_find_next_mbchar_internal (string
, seed
, count
, find_non_zero
)
81 int seed
, count
, find_non_zero
;
90 memset(&ps
, 0, sizeof (mbstate_t));
96 point
= seed
+ _rl_adjust_point (string
, seed
, &ps
);
97 /* if this is true, means that seed was not pointing to a byte indicating
98 the beginning of a multibyte character. Correct the point and consume
105 len
= strlen (string
+ point
);
108 tmp
= mbrtowc (&wc
, string
+point
, len
, &ps
);
109 if (MB_INVALIDCH ((size_t)tmp
))
111 /* invalid bytes. assume a byte represents a character */
115 memset(&ps
, 0, sizeof(mbstate_t));
117 else if (MB_NULLWCH (tmp
))
118 break; /* found wide '\0' */
125 if (WCWIDTH (wc
) == 0)
137 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
138 while (MB_NULLWCH (tmp
) == 0 && MB_INVALIDCH (tmp
) == 0 && WCWIDTH (wc
) == 0)
141 tmp
= mbrtowc (&wc
, string
+ point
, strlen (string
+ point
), &ps
);
149 _rl_find_prev_mbchar_internal (string
, seed
, find_non_zero
)
151 int seed
, find_non_zero
;
154 int prev
, non_zero_prev
, point
, length
;
158 memset(&ps
, 0, sizeof(mbstate_t));
159 length
= strlen(string
);
163 else if (length
< seed
)
166 prev
= non_zero_prev
= point
= 0;
169 tmp
= mbrtowc (&wc
, string
+ point
, length
- point
, &ps
);
170 if (MB_INVALIDCH ((size_t)tmp
))
172 /* in this case, bytes are invalid or shorted to compose
173 multibyte char, so assume that the first byte represents
174 a single character anyway. */
176 /* clear the state of the byte sequence, because
177 in this case effect of mbstate is undefined */
178 memset(&ps
, 0, sizeof (mbstate_t));
180 /* Since we're assuming that this byte represents a single
181 non-zero-width character, don't forget about it. */
184 else if (MB_NULLWCH (tmp
))
185 break; /* Found '\0' char. Can this happen? */
190 if (WCWIDTH (wc
) != 0)
203 /* return the number of bytes parsed from the multibyte sequence starting
204 at src, if a non-L'\0' wide character was recognized. It returns 0,
205 if a L'\0' wide character was recognized. It returns (size_t)(-1),
206 if an invalid multibyte sequence was encountered. It returns (size_t)(-2)
207 if it couldn't parse a complete multibyte character. */
209 _rl_get_char_len (src
, ps
)
215 tmp
= mbrlen((const char *)src
, (size_t)strlen (src
), ps
);
216 if (tmp
== (size_t)(-2))
218 /* shorted to compose multibyte char */
220 memset (ps
, 0, sizeof(mbstate_t));
223 else if (tmp
== (size_t)(-1))
225 /* invalid to compose multibyte char */
226 /* initialize the conversion state */
228 memset (ps
, 0, sizeof(mbstate_t));
231 else if (tmp
== (size_t)0)
237 /* compare the specified two characters. If the characters matched,
238 return 1. Otherwise return 0. */
240 _rl_compare_chars (buf1
, pos1
, ps1
, buf2
, pos2
, ps2
)
250 if ((w1
= _rl_get_char_len (&buf1
[pos1
], ps1
)) <= 0 ||
251 (w2
= _rl_get_char_len (&buf2
[pos2
], ps2
)) <= 0 ||
253 (buf1
[pos1
] != buf2
[pos2
]))
256 for (i
= 1; i
< w1
; i
++)
257 if (buf1
[pos1
+i
] != buf2
[pos2
+i
])
263 /* adjust pointed byte and find mbstate of the point of string.
264 adjusted point will be point <= adjusted_point, and returns
265 differences of the byte(adjusted_point - point).
266 if point is invalied (point < 0 || more than string length),
269 _rl_adjust_point (string
, point
, ps
)
278 length
= strlen(string
);
286 tmp
= mbrlen (string
+ pos
, length
- pos
, ps
);
287 if (MB_INVALIDCH ((size_t)tmp
))
289 /* in this case, bytes are invalid or shorted to compose
290 multibyte char, so assume that the first byte represents
291 a single character anyway. */
293 /* clear the state of the byte sequence, because
294 in this case effect of mbstate is undefined */
296 memset (ps
, 0, sizeof (mbstate_t));
298 else if (MB_NULLWCH (tmp
))
304 return (pos
- point
);
308 _rl_is_mbchar_matched (string
, seed
, end
, mbchar
, length
)
316 if ((end
- seed
) < length
)
319 for (i
= 0; i
< length
; i
++)
320 if (string
[seed
+ i
] != mbchar
[i
])
326 _rl_char_value (buf
, ind
)
335 if (MB_LEN_MAX
== 1 || rl_byte_oriented
)
336 return ((wchar_t) buf
[ind
]);
339 return ((wchar_t) buf
[ind
]);
340 memset (&ps
, 0, sizeof (mbstate_t));
341 tmp
= mbrtowc (&wc
, buf
+ ind
, l
- ind
, &ps
);
342 if (MB_INVALIDCH (tmp
) || MB_NULLWCH (tmp
))
343 return ((wchar_t) buf
[ind
]);
346 #endif /* HANDLE_MULTIBYTE */
348 /* Find next `count' characters started byte point of the specified seed.
349 If flags is MB_FIND_NONZERO, we look for non-zero-width multibyte
351 #undef _rl_find_next_mbchar
353 _rl_find_next_mbchar (string
, seed
, count
, flags
)
355 int seed
, count
, flags
;
357 #if defined (HANDLE_MULTIBYTE)
358 return _rl_find_next_mbchar_internal (string
, seed
, count
, flags
);
360 return (seed
+ count
);
364 /* Find previous character started byte point of the specified seed.
365 Returned point will be point <= seed. If flags is MB_FIND_NONZERO,
366 we look for non-zero-width multibyte characters. */
367 #undef _rl_find_prev_mbchar
369 _rl_find_prev_mbchar (string
, seed
, flags
)
373 #if defined (HANDLE_MULTIBYTE)
374 return _rl_find_prev_mbchar_internal (string
, seed
, flags
);
376 return ((seed
== 0) ? seed
: seed
- 1);
This page took 0.037928 seconds and 5 git commands to generate.