2005-01-12 03:25:57 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* chars.c -- This file is part of GNU nano. *
|
2005-01-12 03:25:57 +00:00
|
|
|
* *
|
2020-01-15 10:42:38 +00:00
|
|
|
* Copyright (C) 2001-2011, 2013-2020 Free Software Foundation, Inc. *
|
2020-01-15 11:11:56 +00:00
|
|
|
* Copyright (C) 2016-2019 Benno Schulenberg *
|
2016-08-29 13:14:18 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2005-01-12 03:25:57 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty *
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
|
|
|
|
* See the GNU General Public License for more details. *
|
2005-01-12 03:25:57 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2005-01-12 03:25:57 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2020-06-20 10:09:31 +00:00
|
|
|
#include "prototypes.h"
|
2005-01-12 03:25:57 +00:00
|
|
|
|
|
|
|
#include <ctype.h>
|
2017-08-06 11:32:44 +00:00
|
|
|
#include <string.h>
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2005-01-12 03:25:57 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
2005-07-21 22:12:03 +00:00
|
|
|
|
2006-04-12 15:27:40 +00:00
|
|
|
static bool use_utf8 = FALSE;
|
2017-12-29 18:27:33 +00:00
|
|
|
/* Whether we've enabled UTF-8 support. */
|
2006-04-12 15:27:40 +00:00
|
|
|
|
|
|
|
/* Enable UTF-8 support. */
|
|
|
|
void utf8_init(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
use_utf8 = TRUE;
|
2006-04-12 15:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is UTF-8 support enabled? */
|
|
|
|
bool using_utf8(void)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
return use_utf8;
|
2006-04-12 15:27:40 +00:00
|
|
|
}
|
2014-04-05 20:28:29 +00:00
|
|
|
#endif /* ENABLE_UTF8 */
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2020-03-13 10:59:08 +00:00
|
|
|
#ifdef ENABLE_SPELLER
|
2020-03-12 14:35:02 +00:00
|
|
|
/* Return TRUE when the given character is some kind of letter. */
|
|
|
|
bool is_alpha_char(const char *c)
|
2016-08-02 20:09:22 +00:00
|
|
|
{
|
2020-03-13 10:43:31 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2020-03-12 14:52:13 +00:00
|
|
|
wchar_t wc;
|
2016-08-02 20:09:22 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
if (mbtowc(&wc, c, MAXCHARLEN) < 0)
|
|
|
|
return FALSE;
|
2016-08-02 20:09:22 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
return iswalpha(wc);
|
2020-03-13 10:43:31 +00:00
|
|
|
#else
|
|
|
|
return isalpha((unsigned char)*c);
|
|
|
|
#endif
|
2016-08-02 20:09:22 +00:00
|
|
|
}
|
2020-03-13 10:59:08 +00:00
|
|
|
#endif /* ENABLE_SPELLER */
|
2016-08-02 20:09:22 +00:00
|
|
|
|
2020-03-12 14:35:02 +00:00
|
|
|
/* Return TRUE when the given character is some kind of letter or a digit. */
|
|
|
|
bool is_alnum_char(const char *c)
|
2005-06-15 06:04:08 +00:00
|
|
|
{
|
2020-03-13 10:43:31 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2020-03-12 14:52:13 +00:00
|
|
|
wchar_t wc;
|
2005-06-15 06:04:08 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
if (mbtowc(&wc, c, MAXCHARLEN) < 0)
|
|
|
|
return FALSE;
|
2005-06-15 06:04:08 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
return iswalnum(wc);
|
2020-03-13 10:43:31 +00:00
|
|
|
#else
|
|
|
|
return isalnum((unsigned char)*c);
|
|
|
|
#endif
|
2005-06-15 06:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 11:29:44 +00:00
|
|
|
/* Return TRUE when the given character is space or tab or other whitespace. */
|
|
|
|
bool is_blank_char(const char *c)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2020-03-13 10:43:31 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2020-03-12 14:52:13 +00:00
|
|
|
wchar_t wc;
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2020-03-12 14:42:52 +00:00
|
|
|
if ((signed char)*c >= 0)
|
2020-03-19 13:27:57 +00:00
|
|
|
return (*c == ' ' || *c == '\t');
|
2020-03-12 14:42:52 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
if (mbtowc(&wc, c, MAXCHARLEN) < 0)
|
|
|
|
return FALSE;
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
return iswblank(wc);
|
2020-03-13 10:43:31 +00:00
|
|
|
#else
|
|
|
|
return isblank((unsigned char)*c);
|
|
|
|
#endif
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 14:35:02 +00:00
|
|
|
/* Return TRUE when the given character is a control character. */
|
|
|
|
bool is_cntrl_char(const char *c)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
2020-03-22 13:29:10 +00:00
|
|
|
return ((c[0] & 0xE0) == 0 || c[0] == DEL_CODE ||
|
2017-12-29 18:27:33 +00:00
|
|
|
((signed char)c[0] == -62 && (signed char)c[1] < -96));
|
|
|
|
} else
|
2005-01-12 03:25:57 +00:00
|
|
|
#endif
|
2020-03-22 13:29:10 +00:00
|
|
|
return ((*c & 0x60) == 0 || *c == DEL_CODE);
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 14:35:02 +00:00
|
|
|
/* Return TRUE when the given character is a punctuation character. */
|
|
|
|
bool is_punct_char(const char *c)
|
2005-06-13 02:40:04 +00:00
|
|
|
{
|
2020-03-13 10:43:31 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2020-03-12 14:52:13 +00:00
|
|
|
wchar_t wc;
|
2005-06-13 02:40:04 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
if (mbtowc(&wc, c, MAXCHARLEN) < 0)
|
|
|
|
return FALSE;
|
2005-06-13 02:40:04 +00:00
|
|
|
|
2020-03-12 14:52:13 +00:00
|
|
|
return iswpunct(wc);
|
2020-03-13 10:43:31 +00:00
|
|
|
#else
|
|
|
|
return ispunct((unsigned char)*c);
|
|
|
|
#endif
|
2005-06-15 06:04:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-12 11:38:01 +00:00
|
|
|
/* Return TRUE when the given character is word-forming (it is alphanumeric or
|
|
|
|
* specified in 'wordchars', or it is punctuation when allow_punct is TRUE). */
|
|
|
|
bool is_word_char(const char *c, bool allow_punct)
|
2005-06-15 06:04:08 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (*c == '\0')
|
|
|
|
return FALSE;
|
2016-07-21 07:46:47 +00:00
|
|
|
|
2020-03-12 14:35:02 +00:00
|
|
|
if (is_alnum_char(c))
|
2017-12-29 18:27:33 +00:00
|
|
|
return TRUE;
|
2016-06-30 16:02:45 +00:00
|
|
|
|
2020-04-06 09:04:21 +00:00
|
|
|
if (allow_punct && is_punct_char(c))
|
|
|
|
return TRUE;
|
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (word_chars != NULL && *word_chars != '\0') {
|
|
|
|
char symbol[MAXCHARLEN + 1];
|
2019-10-21 10:35:14 +00:00
|
|
|
int symlen = collect_char(c, symbol);
|
2016-06-30 16:02:45 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
symbol[symlen] = '\0';
|
|
|
|
return (strstr(word_chars, symbol) != NULL);
|
2020-04-06 09:04:21 +00:00
|
|
|
} else
|
|
|
|
return FALSE;
|
2005-06-13 02:40:04 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 18:40:22 +00:00
|
|
|
/* Return the visible representation of control character c. */
|
2016-05-30 09:28:16 +00:00
|
|
|
char control_rep(const signed char c)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
if (c == DEL_CODE)
|
|
|
|
return '?';
|
|
|
|
else if (c == -97)
|
|
|
|
return '=';
|
|
|
|
else if (c < 0)
|
|
|
|
return c + 224;
|
|
|
|
else
|
|
|
|
return c + 64;
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 18:37:28 +00:00
|
|
|
/* Return the visible representation of multibyte control character c. */
|
2016-12-18 08:40:09 +00:00
|
|
|
char control_mbrep(const char *c, bool isdata)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
/* An embedded newline is an encoded NUL if it is data. */
|
|
|
|
if (*c == '\n' && (isdata || as_an_at))
|
|
|
|
return '@';
|
2016-12-18 08:40:09 +00:00
|
|
|
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
|
|
|
if ((unsigned char)c[0] < 128)
|
|
|
|
return control_rep(c[0]);
|
|
|
|
else
|
|
|
|
return control_rep(c[1]);
|
|
|
|
} else
|
2005-01-12 03:25:57 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
return control_rep(*c);
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 08:25:58 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2019-10-03 09:09:21 +00:00
|
|
|
/* Return the width in columns of the given (multibyte) character. */
|
2005-01-12 03:25:57 +00:00
|
|
|
int mbwidth(const char *c)
|
|
|
|
{
|
2019-10-03 09:09:21 +00:00
|
|
|
/* Ask for the width only when the character isn't plain ASCII. */
|
|
|
|
if ((signed char)*c <= 0) {
|
2017-12-29 18:27:33 +00:00
|
|
|
wchar_t wc;
|
|
|
|
int width;
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2018-06-03 12:13:33 +00:00
|
|
|
if (mbtowc(&wc, c, MAXCHARLEN) < 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return 1;
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
width = wcwidth(wc);
|
2005-06-14 02:08:25 +00:00
|
|
|
|
2019-10-03 08:25:58 +00:00
|
|
|
if (width < 0)
|
2017-12-29 18:27:33 +00:00
|
|
|
return 1;
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return width;
|
2019-10-03 09:09:21 +00:00
|
|
|
} else
|
|
|
|
return 1;
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
2019-10-03 08:25:58 +00:00
|
|
|
#endif
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2019-10-03 08:12:30 +00:00
|
|
|
/* Convert the Unicode value in code to a multibyte character, if possible.
|
2016-12-15 18:46:16 +00:00
|
|
|
* If the conversion succeeds, return the (dynamically allocated) multibyte
|
|
|
|
* character and its length. Otherwise, return an undefined (dynamically
|
|
|
|
* allocated) multibyte character and a length of zero. */
|
2019-10-03 08:12:30 +00:00
|
|
|
char *make_mbchar(long code, int *length)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2020-02-20 15:38:14 +00:00
|
|
|
char *mb_char = charalloc(MAXCHARLEN);
|
2005-03-14 18:47:21 +00:00
|
|
|
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
2019-10-03 08:12:30 +00:00
|
|
|
*length = wctomb(mb_char, (wchar_t)code);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
/* Reject invalid Unicode characters. */
|
2019-10-03 08:12:30 +00:00
|
|
|
if (*length < 0 || !is_valid_unicode((wchar_t)code)) {
|
2017-12-29 18:27:33 +00:00
|
|
|
IGNORE_CALL_RESULT(wctomb(NULL, 0));
|
2019-10-03 08:12:30 +00:00
|
|
|
*length = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
} else
|
2005-01-12 03:25:57 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
{
|
2020-02-20 15:38:14 +00:00
|
|
|
*mb_char = (char)code;
|
2019-10-03 08:12:30 +00:00
|
|
|
*length = 1;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-01-12 03:25:57 +00:00
|
|
|
|
2019-10-03 08:12:30 +00:00
|
|
|
return mb_char;
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:36:29 +00:00
|
|
|
/* Return the length (in bytes) of the character located at *pointer. */
|
|
|
|
int char_length(const char *pointer)
|
|
|
|
{
|
2019-06-11 17:21:47 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2019-06-09 15:36:29 +00:00
|
|
|
/* If possibly a multibyte character, get its length; otherwise, it's 1. */
|
|
|
|
if ((signed char)*pointer < 0) {
|
|
|
|
int length = mblen(pointer, MAXCHARLEN);
|
|
|
|
|
2019-06-11 17:21:47 +00:00
|
|
|
return (length < 0 ? 1 : length);
|
2019-06-09 15:36:29 +00:00
|
|
|
} else
|
2019-06-11 17:21:47 +00:00
|
|
|
#endif
|
2019-06-09 15:36:29 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-10-03 09:20:48 +00:00
|
|
|
/* Return the number of (multibyte) characters in the given string. */
|
|
|
|
size_t mbstrlen(const char *pointer)
|
|
|
|
{
|
|
|
|
size_t count = 0;
|
|
|
|
|
|
|
|
while (*pointer != '\0') {
|
|
|
|
#ifdef ENABLE_UTF8
|
|
|
|
if ((signed char)*pointer < 0) {
|
|
|
|
int length = mblen(pointer, MAXCHARLEN);
|
|
|
|
|
|
|
|
pointer += (length < 0 ? 1 : length);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
pointer++;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-10-21 11:00:57 +00:00
|
|
|
/* Return the length (in bytes) of the character at the start of the
|
|
|
|
* given string, and return a copy of this character in *thechar. */
|
|
|
|
int collect_char(const char *string, char *thechar)
|
2005-01-12 03:25:57 +00:00
|
|
|
{
|
2019-10-21 11:00:57 +00:00
|
|
|
int charlen;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-07-08 18:11:32 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2018-07-10 10:27:17 +00:00
|
|
|
/* If this is a UTF-8 starter byte, get the number of bytes of the character. */
|
2019-10-21 11:00:57 +00:00
|
|
|
if ((signed char)*string < 0) {
|
|
|
|
charlen = mblen(string, MAXCHARLEN);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2018-07-10 10:27:17 +00:00
|
|
|
/* When the multibyte sequence is invalid, only take the first byte. */
|
2019-10-21 11:00:57 +00:00
|
|
|
if (charlen <= 0)
|
|
|
|
charlen = 1;
|
2018-07-10 10:27:17 +00:00
|
|
|
} else
|
|
|
|
#endif
|
2019-10-21 11:00:57 +00:00
|
|
|
charlen = 1;
|
2018-07-10 10:27:17 +00:00
|
|
|
|
2019-10-21 11:00:57 +00:00
|
|
|
for (int i = 0; i < charlen; i++)
|
|
|
|
thechar[i] = string[i];
|
2018-07-10 10:27:17 +00:00
|
|
|
|
2019-10-21 11:00:57 +00:00
|
|
|
return charlen;
|
2005-01-12 03:25:57 +00:00
|
|
|
}
|
2005-01-14 21:50:32 +00:00
|
|
|
|
2019-10-21 10:21:46 +00:00
|
|
|
/* Return the length (in bytes) of the character at the start of
|
|
|
|
* the given string, and add this character's width to *column. */
|
|
|
|
int advance_over(const char *string, size_t *column)
|
|
|
|
{
|
|
|
|
#ifdef ENABLE_UTF8
|
|
|
|
if ((signed char)*string < 0) {
|
2019-10-21 13:37:43 +00:00
|
|
|
int charlen = mblen(string, MAXCHARLEN);
|
|
|
|
|
|
|
|
if (charlen > 0) {
|
2020-03-12 14:35:02 +00:00
|
|
|
if (is_cntrl_char(string))
|
2019-10-21 13:37:43 +00:00
|
|
|
*column += 2;
|
|
|
|
else
|
|
|
|
*column += mbwidth(string);
|
|
|
|
} else {
|
2019-10-21 10:21:46 +00:00
|
|
|
charlen = 1;
|
2019-10-21 13:37:43 +00:00
|
|
|
*column += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return charlen;
|
|
|
|
}
|
2019-10-21 10:21:46 +00:00
|
|
|
#endif
|
|
|
|
|
2019-10-21 13:37:43 +00:00
|
|
|
if ((unsigned char)*string < 0x20) {
|
|
|
|
if (*string == '\t')
|
|
|
|
*column += tabsize - *column % tabsize;
|
|
|
|
else
|
|
|
|
*column += 2;
|
|
|
|
} else if (*string == 0x7F)
|
|
|
|
*column += 2;
|
|
|
|
#ifndef ENABLE_UTF8
|
|
|
|
else if (0x7F < (unsigned char)*string && (unsigned char)*string < 0xA0)
|
2019-10-21 10:21:46 +00:00
|
|
|
*column += 2;
|
|
|
|
#endif
|
2019-10-21 13:37:43 +00:00
|
|
|
else
|
|
|
|
*column += 1;
|
2019-10-21 10:21:46 +00:00
|
|
|
|
2019-10-21 13:37:43 +00:00
|
|
|
return 1;
|
2019-10-21 10:21:46 +00:00
|
|
|
}
|
|
|
|
|
2005-01-14 21:50:32 +00:00
|
|
|
/* Return the index in buf of the beginning of the multibyte character
|
|
|
|
* before the one at pos. */
|
2019-06-09 17:37:56 +00:00
|
|
|
size_t step_left(const char *buf, size_t pos)
|
2005-01-14 21:50:32 +00:00
|
|
|
{
|
2017-05-05 19:40:00 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
2019-06-09 15:07:02 +00:00
|
|
|
size_t before, charlen = 0;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (pos < 4)
|
|
|
|
before = 0;
|
|
|
|
else {
|
|
|
|
const char *ptr = buf + pos;
|
|
|
|
|
|
|
|
/* Probe for a valid starter byte in the preceding four bytes. */
|
|
|
|
if ((signed char)*(--ptr) > -65)
|
|
|
|
before = pos - 1;
|
|
|
|
else if ((signed char)*(--ptr) > -65)
|
|
|
|
before = pos - 2;
|
|
|
|
else if ((signed char)*(--ptr) > -65)
|
|
|
|
before = pos - 3;
|
|
|
|
else if ((signed char)*(--ptr) > -65)
|
|
|
|
before = pos - 4;
|
|
|
|
else
|
|
|
|
before = pos - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move forward again until we reach the original character,
|
|
|
|
* so we know the length of its preceding character. */
|
|
|
|
while (before < pos) {
|
2019-06-09 15:36:29 +00:00
|
|
|
charlen = char_length(buf + before);
|
2019-06-09 15:07:02 +00:00
|
|
|
before += charlen;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 15:07:02 +00:00
|
|
|
return before - charlen;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else
|
2017-05-05 19:40:00 +00:00
|
|
|
#endif
|
2019-10-03 09:20:48 +00:00
|
|
|
return (pos == 0 ? 0 : pos - 1);
|
2005-01-14 21:50:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the index in buf of the beginning of the multibyte character
|
|
|
|
* after the one at pos. */
|
2019-06-09 17:37:56 +00:00
|
|
|
size_t step_right(const char *buf, size_t pos)
|
2005-01-14 21:50:32 +00:00
|
|
|
{
|
2019-06-09 15:36:29 +00:00
|
|
|
return pos + char_length(buf + pos);
|
2005-01-14 21:50:32 +00:00
|
|
|
}
|
2005-01-16 18:49:19 +00:00
|
|
|
|
|
|
|
/* This function is equivalent to strcasecmp() for multibyte strings. */
|
|
|
|
int mbstrcasecmp(const char *s1, const char *s2)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
return mbstrncasecmp(s1, s2, HIGHEST_POSITIVE);
|
2005-01-16 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-24 08:34:40 +00:00
|
|
|
/* This function is equivalent to strncasecmp() for multibyte strings. */
|
2005-01-16 18:49:19 +00:00
|
|
|
int mbstrncasecmp(const char *s1, const char *s2, size_t n)
|
|
|
|
{
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
|
|
|
wchar_t wc1, wc2;
|
|
|
|
|
|
|
|
while (*s1 != '\0' && *s2 != '\0' && n > 0) {
|
2018-06-03 12:13:33 +00:00
|
|
|
bool bad1 = (mbtowc(&wc1, s1, MAXCHARLEN) < 0);
|
|
|
|
bool bad2 = (mbtowc(&wc2, s2, MAXCHARLEN) < 0);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
|
|
|
if (bad1 || bad2) {
|
|
|
|
if (*s1 != *s2)
|
|
|
|
return (unsigned char)*s1 - (unsigned char)*s2;
|
|
|
|
|
|
|
|
if (bad1 != bad2)
|
|
|
|
return (bad1 ? 1 : -1);
|
|
|
|
} else {
|
|
|
|
int difference = towlower(wc1) - towlower(wc2);
|
|
|
|
|
|
|
|
if (difference != 0)
|
|
|
|
return difference;
|
|
|
|
}
|
|
|
|
|
2019-06-09 17:11:14 +00:00
|
|
|
s1 += char_length(s1);
|
|
|
|
s2 += char_length(s2);
|
2017-12-29 18:27:33 +00:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (n > 0) ? ((unsigned char)*s1 - (unsigned char)*s2) : 0;
|
|
|
|
} else
|
2005-01-16 18:49:19 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
return strncasecmp(s1, s2, n);
|
2005-01-16 18:49:19 +00:00
|
|
|
}
|
|
|
|
|
2005-01-22 18:24:16 +00:00
|
|
|
/* This function is equivalent to strcasestr() for multibyte strings. */
|
2007-07-06 13:44:13 +00:00
|
|
|
char *mbstrcasestr(const char *haystack, const char *needle)
|
2005-01-22 18:24:16 +00:00
|
|
|
{
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
|
|
|
size_t needle_len = mbstrlen(needle);
|
2005-01-22 18:24:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (*haystack != '\0') {
|
|
|
|
if (mbstrncasecmp(haystack, needle, needle_len) == 0)
|
|
|
|
return (char *)haystack;
|
2016-08-05 15:16:37 +00:00
|
|
|
|
2019-06-09 17:11:14 +00:00
|
|
|
haystack += char_length(haystack);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
2005-01-22 18:24:16 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return NULL;
|
|
|
|
} else
|
2005-01-22 18:24:16 +00:00
|
|
|
#endif
|
2019-06-09 18:02:58 +00:00
|
|
|
return (char *)strcasestr(haystack, needle);
|
2005-01-22 18:24:16 +00:00
|
|
|
}
|
|
|
|
|
2005-01-18 17:00:00 +00:00
|
|
|
/* This function is equivalent to strstr(), except in that it scans the
|
2017-04-30 15:50:12 +00:00
|
|
|
* string in reverse, starting at pointer. */
|
2016-12-18 20:45:47 +00:00
|
|
|
char *revstrstr(const char *haystack, const char *needle,
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *pointer)
|
2005-01-18 17:00:00 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t needle_len = strlen(needle);
|
|
|
|
size_t tail_len = strlen(pointer);
|
2007-07-10 22:54:58 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (tail_len < needle_len)
|
|
|
|
pointer += tail_len - needle_len;
|
2007-07-10 22:54:58 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (pointer >= haystack) {
|
|
|
|
if (strncmp(pointer, needle, needle_len) == 0)
|
|
|
|
return (char *)pointer;
|
|
|
|
pointer--;
|
|
|
|
}
|
2005-01-18 17:00:00 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return NULL;
|
2005-01-18 17:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function is equivalent to strcasestr(), except in that it scans
|
2017-04-30 15:50:12 +00:00
|
|
|
* the string in reverse, starting at pointer. */
|
2017-04-28 19:50:36 +00:00
|
|
|
char *revstrcasestr(const char *haystack, const char *needle,
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *pointer)
|
2005-01-16 18:49:19 +00:00
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
size_t needle_len = strlen(needle);
|
|
|
|
size_t tail_len = strlen(pointer);
|
2007-07-10 22:54:58 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (tail_len < needle_len)
|
|
|
|
pointer += tail_len - needle_len;
|
2005-01-16 18:49:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (pointer >= haystack) {
|
|
|
|
if (strncasecmp(pointer, needle, needle_len) == 0)
|
|
|
|
return (char *)pointer;
|
|
|
|
pointer--;
|
|
|
|
}
|
2005-01-16 18:49:19 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
return NULL;
|
2005-01-16 18:49:19 +00:00
|
|
|
}
|
2005-01-24 01:14:17 +00:00
|
|
|
|
|
|
|
/* This function is equivalent to strcasestr() for multibyte strings,
|
2017-04-30 15:50:12 +00:00
|
|
|
* except in that it scans the string in reverse, starting at pointer. */
|
2017-04-28 19:50:36 +00:00
|
|
|
char *mbrevstrcasestr(const char *haystack, const char *needle,
|
2017-12-29 18:27:33 +00:00
|
|
|
const char *pointer)
|
2005-01-24 01:14:17 +00:00
|
|
|
{
|
2005-07-17 02:40:07 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
|
|
|
size_t needle_len = mbstrlen(needle);
|
|
|
|
size_t tail_len = mbstrlen(pointer);
|
2005-01-24 01:14:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (tail_len < needle_len)
|
|
|
|
pointer += tail_len - needle_len;
|
2017-04-30 16:07:04 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (pointer < haystack)
|
|
|
|
return NULL;
|
2005-01-24 01:14:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
while (TRUE) {
|
|
|
|
if (mbstrncasecmp(pointer, needle, needle_len) == 0)
|
|
|
|
return (char *)pointer;
|
2005-01-24 01:14:17 +00:00
|
|
|
|
2017-12-29 18:27:33 +00:00
|
|
|
if (pointer == haystack)
|
|
|
|
return NULL;
|
2005-01-24 01:14:17 +00:00
|
|
|
|
2019-06-09 17:37:56 +00:00
|
|
|
pointer = haystack + step_left(haystack, pointer - haystack);
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
} else
|
2005-01-24 01:14:17 +00:00
|
|
|
#endif
|
2017-12-29 18:27:33 +00:00
|
|
|
return revstrcasestr(haystack, needle, pointer);
|
2005-01-24 01:14:17 +00:00
|
|
|
}
|
2005-01-16 18:49:19 +00:00
|
|
|
|
2017-10-31 16:34:07 +00:00
|
|
|
#if !defined(NANO_TINY) || defined(ENABLE_JUSTIFY)
|
2005-07-21 18:05:27 +00:00
|
|
|
/* This function is equivalent to strchr() for multibyte strings. */
|
2019-06-10 10:36:16 +00:00
|
|
|
char *mbstrchr(const char *string, const char *chr)
|
2005-07-21 18:05:27 +00:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_UTF8
|
2017-12-29 18:27:33 +00:00
|
|
|
if (use_utf8) {
|
2019-06-10 10:29:49 +00:00
|
|
|
bool bad_s = FALSE, bad_c = FALSE;
|
2017-12-29 18:27:33 +00:00
|
|
|
wchar_t ws, wc;
|
|
|
|
|
2019-06-10 10:36:16 +00:00
|
|
|
if (mbtowc(&wc, chr, MAXCHARLEN) < 0) {
|
|
|
|
wc = (unsigned char)*chr;
|
2019-06-10 10:29:49 +00:00
|
|
|
bad_c = TRUE;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:36:16 +00:00
|
|
|
while (*string != '\0') {
|
|
|
|
int symlen = mbtowc(&ws, string, MAXCHARLEN);
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-06-10 10:29:49 +00:00
|
|
|
if (symlen < 0) {
|
2019-06-10 10:36:16 +00:00
|
|
|
ws = (unsigned char)*string;
|
2019-06-10 10:29:49 +00:00
|
|
|
bad_s = TRUE;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:29:49 +00:00
|
|
|
if (ws == wc && bad_s == bad_c)
|
2017-12-29 18:27:33 +00:00
|
|
|
break;
|
|
|
|
|
2019-06-10 10:36:16 +00:00
|
|
|
string += symlen;
|
2017-12-29 18:27:33 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 10:36:16 +00:00
|
|
|
if (*string == '\0')
|
2019-06-10 10:06:12 +00:00
|
|
|
return NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-06-10 10:36:16 +00:00
|
|
|
return (char *)string;
|
2017-12-29 18:27:33 +00:00
|
|
|
} else
|
2005-07-21 18:05:27 +00:00
|
|
|
#endif
|
2019-06-10 10:36:16 +00:00
|
|
|
return strchr(string, *chr);
|
2005-07-21 18:05:27 +00:00
|
|
|
}
|
2017-10-31 16:34:07 +00:00
|
|
|
#endif /* !NANO_TINY || ENABLE_JUSTIFY */
|
2005-07-21 18:05:27 +00:00
|
|
|
|
2006-01-06 07:10:30 +00:00
|
|
|
#ifndef NANO_TINY
|
2019-04-06 17:45:45 +00:00
|
|
|
/* Locate, in the given string, the first occurrence of any of
|
|
|
|
* the characters in accept, searching forward. */
|
|
|
|
char *mbstrpbrk(const char *string, const char *accept)
|
2006-01-06 07:10:30 +00:00
|
|
|
{
|
2019-04-06 17:45:45 +00:00
|
|
|
while (*string != '\0') {
|
|
|
|
if (mbstrchr(accept, string) != NULL)
|
|
|
|
return (char *)string;
|
2006-01-06 07:10:30 +00:00
|
|
|
|
2019-06-09 17:11:14 +00:00
|
|
|
string += char_length(string);
|
2019-04-06 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2006-01-06 07:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-30 15:50:12 +00:00
|
|
|
/* Locate, in the string that starts at head, the first occurrence of any of
|
2019-04-06 17:45:45 +00:00
|
|
|
* the characters in accept, starting from pointer and searching backwards. */
|
2017-04-30 15:50:12 +00:00
|
|
|
char *mbrevstrpbrk(const char *head, const char *accept, const char *pointer)
|
2006-01-06 07:10:30 +00:00
|
|
|
{
|
2019-04-06 17:45:45 +00:00
|
|
|
if (*pointer == '\0') {
|
|
|
|
if (pointer == head)
|
|
|
|
return NULL;
|
2019-06-09 17:37:56 +00:00
|
|
|
pointer = head + step_left(head, pointer - head);
|
2019-04-06 17:45:45 +00:00
|
|
|
}
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-06 17:45:45 +00:00
|
|
|
while (TRUE) {
|
|
|
|
if (mbstrchr(accept, pointer) != NULL)
|
|
|
|
return (char *)pointer;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-04-06 17:45:45 +00:00
|
|
|
/* If we've reached the head of the string, we found nothing. */
|
|
|
|
if (pointer == head)
|
|
|
|
return NULL;
|
2017-12-29 18:27:33 +00:00
|
|
|
|
2019-06-09 17:37:56 +00:00
|
|
|
pointer = head + step_left(head, pointer - head);
|
2019-04-06 17:45:45 +00:00
|
|
|
}
|
2006-01-06 07:10:30 +00:00
|
|
|
}
|
|
|
|
#endif /* !NANO_TINY */
|
|
|
|
|
2017-10-31 16:34:07 +00:00
|
|
|
#if defined(ENABLE_NANORC) && (!defined(NANO_TINY) || defined(ENABLE_JUSTIFY))
|
2019-03-21 16:36:46 +00:00
|
|
|
/* Return TRUE if the given string contains at least one blank character,
|
2005-06-14 01:55:56 +00:00
|
|
|
* and FALSE otherwise. */
|
2019-03-24 08:42:57 +00:00
|
|
|
bool has_blank_char(const char *string)
|
2005-06-14 01:55:56 +00:00
|
|
|
{
|
2019-03-24 08:42:57 +00:00
|
|
|
char symbol[MAXCHARLEN];
|
2005-06-14 01:55:56 +00:00
|
|
|
|
2019-03-24 08:42:57 +00:00
|
|
|
while (*string != '\0') {
|
2019-10-21 10:35:14 +00:00
|
|
|
string += collect_char(string, symbol);
|
2005-06-14 01:55:56 +00:00
|
|
|
|
2020-03-12 11:29:44 +00:00
|
|
|
if (is_blank_char(symbol))
|
2019-03-24 08:42:57 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2019-03-21 16:36:46 +00:00
|
|
|
|
|
|
|
return FALSE;
|
2005-06-14 01:55:56 +00:00
|
|
|
}
|
2017-10-31 16:34:07 +00:00
|
|
|
#endif /* ENABLE_NANORC && (!NANO_TINY || ENABLE_JUSTIFY) */
|
2005-06-14 23:36:13 +00:00
|
|
|
|
2019-08-09 17:24:30 +00:00
|
|
|
/* Return TRUE when the given string is empty or consists of only blanks. */
|
|
|
|
bool white_string(const char *string)
|
|
|
|
{
|
2020-03-12 11:29:44 +00:00
|
|
|
while (*string != '\0' && (is_blank_char(string) || *string == '\r'))
|
2019-08-09 17:24:30 +00:00
|
|
|
string += char_length(string);
|
|
|
|
|
|
|
|
return !*string;
|
|
|
|
}
|
|
|
|
|
2005-08-05 03:14:29 +00:00
|
|
|
#ifdef ENABLE_UTF8
|
2005-08-28 03:07:13 +00:00
|
|
|
/* Return TRUE if wc is valid Unicode, and FALSE otherwise. */
|
2005-08-05 03:14:29 +00:00
|
|
|
bool is_valid_unicode(wchar_t wc)
|
|
|
|
{
|
2017-12-29 18:27:33 +00:00
|
|
|
return ((0 <= wc && wc <= 0xD7FF) ||
|
|
|
|
(0xE000 <= wc && wc <= 0xFDCF) ||
|
|
|
|
(0xFDF0 <= wc && wc <= 0xFFFD) ||
|
|
|
|
(0xFFFF < wc && wc <= 0x10FFFF && (wc & 0xFFFF) <= 0xFFFD));
|
2005-08-05 03:14:29 +00:00
|
|
|
}
|
|
|
|
#endif
|