chars.c 21.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* $Id$ */
/**************************************************************************
 *   chars.c                                                              *
 *                                                                        *
 *   Copyright (C) 2005 Chris Allegretta                                  *
 *   This program 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 2, or (at your option)  *
 *   any later version.                                                   *
 *                                                                        *
11
12
13
14
 *   This program 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.                             *
15
16
17
 *                                                                        *
 *   You should have received a copy of the GNU General Public License    *
 *   along with this program; if not, write to the Free Software          *
18
19
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA            *
 *   02110-1301, USA.                                                     *
20
21
22
23
24
25
26
 *                                                                        *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

27
#include <string.h>
28
29
30
#include <ctype.h>
#include "proto.h"

31
#ifdef ENABLE_UTF8
32
#ifdef HAVE_WCHAR_H
33
34
#include <wchar.h>
#endif
35
#ifdef HAVE_WCTYPE_H
36
37
#include <wctype.h>
#endif
38
39
40
41
42
43

static const wchar_t bad_wchar = 0xFFFD;
	/* If we get an invalid multibyte sequence, we treat it as
	 * Unicode FFFD (Replacement Character), unless we're
	 * determining if it's a control character or searching for a
	 * match to it. */
44
45
static const char *bad_mbchar = "\xEF\xBF\xBD";
static const int bad_mbchar_len = 3;
46
#endif
47

48
49
#ifndef HAVE_ISBLANK
/* This function is equivalent to isblank(). */
50
bool nisblank(int c)
51
{
52
    return isspace(c) && (c == '\t' || !is_cntrl_char(c));
53
}
54
#endif
55

56
#if !defined(HAVE_ISWBLANK) && defined(ENABLE_UTF8)
57
/* This function is equivalent to iswblank(). */
58
bool niswblank(wchar_t wc)
59
{
60
    return iswspace(wc) && (wc == '\t' || !is_cntrl_wchar(wc));
61
}
62
#endif
63

64
65
66
67
68
69
70
/* Return TRUE if the value of c is in byte range, and FALSE
 * otherwise. */
bool is_byte(int c)
{
    return ((unsigned int)c == (unsigned char)c);
}

71
72
73
74
75
/* This function is equivalent to isalnum() for multibyte characters. */
bool is_alnum_mbchar(const char *c)
{
    assert(c != NULL);

76
#ifdef ENABLE_UTF8
77
    if (ISSET(USE_UTF8)) {
78
79
	wchar_t wc;

80
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
81
	    mbtowc(NULL, NULL, 0);
82
	    wc = bad_wchar;
83
84
85
86
87
88
89
90
	}

	return iswalnum(wc);
    } else
#endif
	return isalnum((unsigned char)*c);
}

91
92
93
94
95
/* This function is equivalent to isblank() for multibyte characters. */
bool is_blank_mbchar(const char *c)
{
    assert(c != NULL);

96
#ifdef ENABLE_UTF8
97
    if (ISSET(USE_UTF8)) {
98
99
	wchar_t wc;

100
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
101
	    mbtowc(NULL, NULL, 0);
102
	    wc = bad_wchar;
103
104
	}

105
	return iswblank(wc);
106
107
    } else
#endif
108
	return isblank((unsigned char)*c);
109
110
111
}

/* This function is equivalent to iscntrl(), except in that it also
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
112
 * handles high-bit control characters. */
113
bool is_cntrl_char(int c)
114
{
115
116
    return (-128 <= c && c < -96) || (0 <= c && c < 32) ||
	(127 <= c && c < 160);
117
118
}

119
#ifdef ENABLE_UTF8
120
121
122
/* This function is equivalent to iscntrl() for wide characters, except
 * in that it also handles wide control characters with their high bits
 * set. */
123
bool is_cntrl_wchar(wchar_t wc)
124
{
125
    return (0 <= wc && wc < 32) || (127 <= wc && wc < 160);
126
127
128
}
#endif

129
130
131
132
133
134
135
/* This function is equivalent to iscntrl() for multibyte characters,
 * except in that it also handles multibyte control characters with
 * their high bits set. */
bool is_cntrl_mbchar(const char *c)
{
    assert(c != NULL);

136
#ifdef ENABLE_UTF8
137
    if (ISSET(USE_UTF8)) {
138
139
	wchar_t wc;

140
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
141
	    mbtowc(NULL, NULL, 0);
142
	    wc = bad_wchar;
143
144
145
146
147
148
149
150
	}

	return is_cntrl_wchar(wc);
    } else
#endif
	return is_cntrl_char((unsigned char)*c);
}

151
152
/* This function is equivalent to ispunct() for multibyte characters. */
bool is_punct_mbchar(const char *c)
153
154
155
{
    assert(c != NULL);

156
#ifdef ENABLE_UTF8
157
    if (ISSET(USE_UTF8)) {
158
159
160
	wchar_t wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

161
	if (c_mb_len < 0) {
162
	    mbtowc(NULL, NULL, 0);
163
	    wc = bad_wchar;
164
165
	}

166
	return iswpunct(wc);
167
168
    } else
#endif
169
170
171
172
	return ispunct((unsigned char)*c);
}

/* Return TRUE for a multibyte character found in a word (currently only
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
173
 * an alphanumeric or punctuation character, and only the latter if
174
175
176
177
178
179
180
 * allow_punct is TRUE) and FALSE otherwise. */
bool is_word_mbchar(const char *c, bool allow_punct)
{
    assert(c != NULL);

    return is_alnum_mbchar(c) || (allow_punct ? is_punct_mbchar(c) :
	FALSE);
181
182
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
183
/* c is a control character.  It displays as ^@, ^?, or ^[ch], where ch
184
 * is (c + 64).  We return that character. */
185
char control_rep(char c)
186
{
187
188
    assert(is_cntrl_char(c));

189
190
191
192
193
194
195
196
197
    /* Treat newlines embedded in a line as encoded nulls. */
    if (c == '\n')
	return '@';
    else if (c == NANO_CONTROL_8)
	return '?';
    else
	return c + 64;
}

198
#ifdef ENABLE_UTF8
199
/* c is a wide control character.  It displays as ^@, ^?, or ^[ch],
200
 * where ch is (c + 64).  We return that wide character. */
201
202
wchar_t control_wrep(wchar_t wc)
{
203
204
    assert(is_cntrl_wchar(wc));

205
206
207
208
209
210
211
212
213
214
215
    /* Treat newlines embedded in a line as encoded nulls. */
    if (wc == '\n')
	return '@';
    else if (wc == NANO_CONTROL_8)
	return '?';
    else
	return wc + 64;
}
#endif

/* c is a multibyte control character.  It displays as ^@, ^?, or ^[ch],
216
217
218
219
 * where ch is (c + 64).  We return that multibyte character.  If crep
 * is an invalid multibyte sequence, it will be replaced with Unicode
 * 0xFFFD (Replacement Character), so it should be dynamically allocated
 * and able to hold MB_CUR_MAX single-byte characters. */
220
221
char *control_mbrep(const char *c, char *crep, int *crep_len)
{
222
    assert(c != NULL && crep != NULL && crep_len != NULL);
223

224
#ifdef ENABLE_UTF8
225
    if (ISSET(USE_UTF8)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
226
	wchar_t wc;
227

228
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
229
	    mbtowc(NULL, NULL, 0);
230
	    *crep_len = bad_mbchar_len;
231
	    strncpy(crep, bad_mbchar, *crep_len);
232
233
234
235
236
237
238
	} else {
	    *crep_len = wctomb(crep, control_wrep(wc));

	    if (*crep_len < 0) {
		wctomb(NULL, 0);
		*crep_len = 0;
	    }
239
240
241
242
	}
    } else {
#endif
	*crep_len = 1;
243
	*crep = control_rep(*c);
244
#ifdef ENABLE_UTF8
245
246
    }
#endif
247
248

    return crep;
249
250
}

251
/* c is a multibyte non-control character.  We return that multibyte
252
253
254
255
 * character.  If crep is an invalid multibyte sequence, it will be
 * replaced with Unicode 0xFFFD (Replacement Character), so it should be
 * dynamically allocated and able to hold MB_CUR_MAX single-byte
 * characters. */
256
257
258
259
260
261
262
263
char *mbrep(const char *c, char *crep, int *crep_len)
{
    assert(c != NULL && crep != NULL && crep_len != NULL);

#ifdef ENABLE_UTF8
    if (ISSET(USE_UTF8)) {
	wchar_t wc;

264
265
	/* Reject invalid Unicode characters. */
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0 || !is_valid_unicode(wc)) {
266
267
	    mbtowc(NULL, NULL, 0);
	    *crep_len = bad_mbchar_len;
268
	    strncpy(crep, bad_mbchar, *crep_len);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
	} else {
	    *crep_len = wctomb(crep, wc);

	    if (*crep_len < 0) {
		wctomb(NULL, 0);
		*crep_len = 0;
	    }
	}
    } else {
#endif
	*crep_len = 1;
	*crep = *c;
#ifdef ENABLE_UTF8
    }
#endif

    return crep;
}

288
289
290
291
292
/* This function is equivalent to wcwidth() for multibyte characters. */
int mbwidth(const char *c)
{
    assert(c != NULL);

293
#ifdef ENABLE_UTF8
294
    if (ISSET(USE_UTF8)) {
295
	wchar_t wc;
296
	int width;
297

298
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
299
	    mbtowc(NULL, NULL, 0);
300
	    wc = bad_wchar;
301
302
303
	}

	width = wcwidth(wc);
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
304

305
306
307
308
309
310
311
312
313
314
315
316
	if (width == -1)
	    width++;

	return width;
    } else
#endif
	return 1;
}

/* Return the maximum width in bytes of a multibyte character. */
int mb_cur_max(void)
{
317
    return
318
#ifdef ENABLE_UTF8
319
	ISSET(USE_UTF8) ? MB_CUR_MAX :
320
#endif
321
	1;
322
323
}

324
325
/* Convert the Unicode value in chr to a multibyte character with the
 * same wide character value as chr, if possible.  If the conversion
326
327
328
 * succeeds, return the (dynamically allocated) multibyte character and
 * its length.  Otherwise, return an undefined (dynamically allocated)
 * multibyte character and a length of zero. */
329
char *make_mbchar(long chr, int *chr_mb_len)
330
{
331
332
    char *chr_mb;

333
    assert(chr_mb_len != NULL);
334

335
#ifdef ENABLE_UTF8
336
    if (ISSET(USE_UTF8)) {
337
	chr_mb = charalloc(MB_CUR_MAX);
338
	*chr_mb_len = wctomb(chr_mb, (wchar_t)chr);
339

340
341
	/* Reject invalid Unicode characters. */
	if (*chr_mb_len < 0 || !is_valid_unicode((wchar_t)chr)) {
342
343
	    wctomb(NULL, 0);
	    *chr_mb_len = 0;
344
345
346
347
	}
    } else {
#endif
	*chr_mb_len = 1;
348
	chr_mb = mallocstrncpy(NULL, (char *)&chr, 1);
349
#ifdef ENABLE_UTF8
350
351
352
353
354
355
356
357
    }
#endif

    return chr_mb;
}

/* Parse a multibyte character from buf.  Return the number of bytes
 * used.  If chr isn't NULL, store the multibyte character in it.  If
358
359
360
 * col isn't NULL, store the new display width in it.  If *buf is '\t',
 * we expect col to have the current display width. */
int parse_mbchar(const char *buf, char *chr, size_t *col)
361
362
363
364
365
{
    int buf_mb_len;

    assert(buf != NULL);

366
#ifdef ENABLE_UTF8
367
    if (ISSET(USE_UTF8)) {
368
369
370
	/* Get the number of bytes in the multibyte character. */
	buf_mb_len = mblen(buf, MB_CUR_MAX);

371
372
373
	/* If buf contains an invalid multibyte character, set bad_chr
	 * to TRUE and interpret buf's first byte. */
	if (buf_mb_len < 0) {
374
	    mblen(NULL, 0);
375
	    buf_mb_len = 1;
376
377
	} else if (buf_mb_len == 0)
	    buf_mb_len++;
378
379
380
381

	/* Save the multibyte character in chr. */
	if (chr != NULL) {
	    int i;
382

383
384
385
386
387
388
389
390
391
392
393
394
395
	    for (i = 0; i < buf_mb_len; i++)
		chr[i] = buf[i];
	}

	/* Save the column width of the wide character in col. */
	if (col != NULL) {
	    /* If we have a tab, get its width in columns using the
	     * current value of col. */
	    if (*buf == '\t')
		*col += tabsize - *col % tabsize;
	    /* If we have a control character, get its width using one
	     * column for the "^" that will be displayed in front of it,
	     * and the width in columns of its visible equivalent as
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
396
	     * returned by control_mbrep(). */
397
	    else if (is_cntrl_mbchar(buf)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
398
		char *ctrl_buf_mb = charalloc(MB_CUR_MAX);
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
		int ctrl_buf_mb_len;

		(*col)++;

		ctrl_buf_mb = control_mbrep(buf, ctrl_buf_mb,
			&ctrl_buf_mb_len);

		*col += mbwidth(ctrl_buf_mb);

		free(ctrl_buf_mb);
	    /* If we have a normal character, get its width in columns
	     * normally. */
	    } else
		*col += mbwidth(buf);
	}
    } else {
#endif
	/* Get the number of bytes in the byte character. */
	buf_mb_len = 1;

	/* Save the byte character in chr. */
	if (chr != NULL)
	    *chr = *buf;

	if (col != NULL) {
	    /* If we have a tab, get its width in columns using the
	     * current value of col. */
	    if (*buf == '\t')
		*col += tabsize - *col % tabsize;
	    /* If we have a control character, it's two columns wide:
	     * one column for the "^" that will be displayed in front of
	     * it, and one column for its visible equivalent as returned
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
431
	     * by control_mbrep(). */
432
433
434
435
436
437
	    else if (is_cntrl_char((unsigned char)*buf))
		*col += 2;
	    /* If we have a normal character, it's one column wide. */
	    else
		(*col)++;
	}
438
#ifdef ENABLE_UTF8
439
440
441
442
443
    }
#endif

    return buf_mb_len;
}
444
445
446
447
448
449
450

/* Return the index in buf of the beginning of the multibyte character
 * before the one at pos. */
size_t move_mbleft(const char *buf, size_t pos)
{
    size_t pos_prev = pos;

451
    assert(buf != NULL && pos <= strlen(buf));
452
453
454
455

    /* There is no library function to move backward one multibyte
     * character.  Here is the naive, O(pos) way to do it. */
    while (TRUE) {
456
	int buf_mb_len = parse_mbchar(buf + pos - pos_prev, NULL, NULL);
457

458
	if (pos_prev <= (size_t)buf_mb_len)
459
460
461
462
463
464
465
466
467
468
469
470
	    break;

	pos_prev -= buf_mb_len;
    }

    return pos - pos_prev;
}

/* Return the index in buf of the beginning of the multibyte character
 * after the one at pos. */
size_t move_mbright(const char *buf, size_t pos)
{
471
    return pos + parse_mbchar(buf + pos, NULL, NULL);
472
}
473
474
475
476
477

#ifndef HAVE_STRCASECMP
/* This function is equivalent to strcasecmp(). */
int nstrcasecmp(const char *s1, const char *s2)
{
478
    return strncasecmp(s1, s2, (size_t)-1);
479
480
481
482
483
484
}
#endif

/* This function is equivalent to strcasecmp() for multibyte strings. */
int mbstrcasecmp(const char *s1, const char *s2)
{
485
    return mbstrncasecmp(s1, s2, (size_t)-1);
486
487
488
489
490
491
492
493
494
495
496
497
498
499
}

#ifndef HAVE_STRNCASECMP
/* This function is equivalent to strncasecmp(). */
int nstrncasecmp(const char *s1, const char *s2, size_t n)
{
    assert(s1 != NULL && s2 != NULL);

    for (; n > 0 && *s1 != '\0' && *s2 != '\0'; n--, s1++, s2++) {
	if (tolower(*s1) != tolower(*s2))
	    break;
    }

    if (n > 0)
500
	return tolower(*s1) - tolower(*s2);
501
502
503
504
505
506
507
508
509
    else
	return 0;
}
#endif

/* This function is equivalent to strncasecmp() for multibyte
 * strings. */
int mbstrncasecmp(const char *s1, const char *s2, size_t n)
{
510
#ifdef ENABLE_UTF8
511
    if (ISSET(USE_UTF8)) {
512
513
	char *s1_mb = charalloc(MB_CUR_MAX);
	char *s2_mb = charalloc(MB_CUR_MAX);
514
515
	wchar_t ws1, ws2;

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
516
517
	assert(s1 != NULL && s2 != NULL);

518
	while (n > 0 && *s1 != '\0' && *s2 != '\0') {
519
	    bool bad_s1_mb = FALSE, bad_s2_mb = FALSE;
520
521
	    int s1_mb_len, s2_mb_len;

522
	    s1_mb_len = parse_mbchar(s1, s1_mb, NULL);
523

524
	    if (mbtowc(&ws1, s1_mb, s1_mb_len) < 0) {
525
526
		mbtowc(NULL, NULL, 0);
		ws1 = (unsigned char)*s1_mb;
527
		bad_s1_mb = TRUE;
528
529
	    }

530
	    s2_mb_len = parse_mbchar(s2, s2_mb, NULL);
531

532
	    if (mbtowc(&ws2, s2_mb, s2_mb_len) < 0) {
533
534
		mbtowc(NULL, NULL, 0);
		ws2 = (unsigned char)*s2_mb;
535
		bad_s2_mb = TRUE;
536
537
	    }

538
539
	    if (n == 0 || bad_s1_mb != bad_s2_mb ||
		towlower(ws1) != towlower(ws2))
540
541
542
543
		break;

	    s1 += s1_mb_len;
	    s2 += s2_mb_len;
544
	    n--;
545
546
547
548
549
	}

	free(s1_mb);
	free(s2_mb);

550
	return towlower(ws1) - towlower(ws2);
551
552
    } else
#endif
553
	return strncasecmp(s1, s2, n);
554
555
556
557
558
559
560
561
562
563
}

#ifndef HAVE_STRCASESTR
/* This function is equivalent to strcasestr().  It was adapted from
 * mutt's mutt_stristr() function. */
const char *nstrcasestr(const char *haystack, const char *needle)
{
    assert(haystack != NULL && needle != NULL);

    for (; *haystack != '\0'; haystack++) {
564
	const char *r = haystack, *q = needle;
565

566
	for (; tolower(*r) == tolower(*q) && *q != '\0'; r++, q++)
567
568
569
570
571
572
573
574
575
576
	    ;

	if (*q == '\0')
	    return haystack;
    }

    return NULL;
}
#endif

577
578
579
/* This function is equivalent to strcasestr() for multibyte strings. */
const char *mbstrcasestr(const char *haystack, const char *needle)
{
580
#ifdef ENABLE_UTF8
581
    if (ISSET(USE_UTF8)) {
582
	char *r_mb = charalloc(MB_CUR_MAX);
583
	char *q_mb = charalloc(MB_CUR_MAX);
584
	wchar_t wr, wq;
585
586
587
588
589
	bool found_needle = FALSE;

	assert(haystack != NULL && needle != NULL);

	while (*haystack != '\0') {
590
591
	    const char *r = haystack, *q = needle;
	    int r_mb_len, q_mb_len;
592
593

	    while (*q != '\0') {
594
595
		bool bad_r_mb = FALSE, bad_q_mb = FALSE;

596
		r_mb_len = parse_mbchar(r, r_mb, NULL);
597

598
		if (mbtowc(&wr, r_mb, r_mb_len) < 0) {
599
		    mbtowc(NULL, NULL, 0);
600
		    wr = (unsigned char)*r;
601
		    bad_r_mb = TRUE;
602
603
		}

604
		q_mb_len = parse_mbchar(q, q_mb, NULL);
605

606
		if (mbtowc(&wq, q_mb, q_mb_len) < 0) {
607
608
		    mbtowc(NULL, NULL, 0);
		    wq = (unsigned char)*q;
609
		    bad_q_mb = TRUE;
610
611
		}

612
613
		if (bad_r_mb != bad_q_mb ||
			towlower(wr) != towlower(wq))
614
615
		    break;

616
		r += r_mb_len;
617
618
619
620
621
622
623
624
		q += q_mb_len;
	    }

	    if (*q == '\0') {
		found_needle = TRUE;
		break;
	    }

625
	    haystack += move_mbright(haystack, 0);
626
627
	}

628
	free(r_mb);
629
630
	free(q_mb);

631
	return found_needle ? haystack : NULL;
632
633
    } else
#endif
634
	return strcasestr(haystack, needle);
635
636
}

637
#if !defined(NANO_SMALL) || !defined(DISABLE_TABCOMP)
638
/* This function is equivalent to strstr(), except in that it scans the
639
 * string in reverse, starting at rev_start. */
640
641
642
643
644
645
646
647
const char *revstrstr(const char *haystack, const char *needle, const
	char *rev_start)
{
    assert(haystack != NULL && needle != NULL && rev_start != NULL);

    for (; rev_start >= haystack; rev_start--) {
	const char *r, *q;

648
	for (r = rev_start, q = needle; *r == *q && *q != '\0'; r++, q++)
649
650
651
652
653
654
655
656
	    ;

	if (*q == '\0')
	    return rev_start;
    }

    return NULL;
}
657
#endif /* !NANO_SMALL || !DISABLE_TABCOMP */
658

659
#ifndef NANO_SMALL
660
/* This function is equivalent to strcasestr(), except in that it scans
661
 * the string in reverse, starting at rev_start. */
662
663
664
665
666
667
668
669
const char *revstrcasestr(const char *haystack, const char *needle,
	const char *rev_start)
{
    assert(haystack != NULL && needle != NULL && rev_start != NULL);

    for (; rev_start >= haystack; rev_start--) {
	const char *r = rev_start, *q = needle;

670
	for (; tolower(*r) == tolower(*q) && *q != '\0'; r++, q++)
671
672
673
674
675
676
677
678
	    ;

	if (*q == '\0')
	    return rev_start;
    }

    return NULL;
}
679
680
681
682
683
684
685

/* This function is equivalent to strcasestr() for multibyte strings,
 * except in that it scans the string in reverse, starting at
 * rev_start. */
const char *mbrevstrcasestr(const char *haystack, const char *needle,
	const char *rev_start)
{
686
#ifdef ENABLE_UTF8
687
    if (ISSET(USE_UTF8)) {
688
689
690
691
692
693
694
695
696
697
698
699
	char *r_mb = charalloc(MB_CUR_MAX);
	char *q_mb = charalloc(MB_CUR_MAX);
	wchar_t wr, wq;
	bool begin_line = FALSE, found_needle = FALSE;

	assert(haystack != NULL && needle != NULL && rev_start != NULL);

	while (!begin_line) {
	    const char *r = rev_start, *q = needle;
	    int r_mb_len, q_mb_len;

	    while (*q != '\0') {
700
701
		bool bad_r_mb = FALSE, bad_q_mb = FALSE;

702
		r_mb_len = parse_mbchar(r, r_mb, NULL);
703

704
		if (mbtowc(&wr, r_mb, r_mb_len) < 0) {
705
706
		    mbtowc(NULL, NULL, 0);
		    wr = (unsigned char)*r;
707
		    bad_r_mb = TRUE;
708
709
		}

710
		q_mb_len = parse_mbchar(q, q_mb, NULL);
711

712
		if (mbtowc(&wq, q_mb, q_mb_len) < 0) {
713
714
		    mbtowc(NULL, NULL, 0);
		    wq = (unsigned char)*q;
715
		    bad_q_mb = TRUE;
716
717
		}

718
719
		if (bad_r_mb != bad_q_mb ||
			towlower(wr) != towlower(wq))
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
		    break;

		r += r_mb_len;
		q += q_mb_len;
	    }

	    if (*q == '\0') {
		found_needle = TRUE;
		break;
	    }

	    if (rev_start == haystack)
		begin_line = TRUE;
	    else
		rev_start = haystack + move_mbleft(haystack, rev_start -
			haystack);
	}

	free(r_mb);
	free(q_mb);

741
	return found_needle ? rev_start : NULL;
742
743
744
745
    } else
#endif
	return revstrcasestr(haystack, needle, rev_start);
}
746
#endif /* !NANO_SMALL */
747

748
749
750
751
752
753
/* This function is equivalent to strlen() for multibyte strings. */
size_t mbstrlen(const char *s)
{
    return mbstrnlen(s, (size_t)-1);
}

754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
#ifndef HAVE_STRNLEN
/* This function is equivalent to strnlen(). */
size_t nstrnlen(const char *s, size_t maxlen)
{
    size_t n = 0;

    assert(s != NULL);

    for (; maxlen > 0 && *s != '\0'; maxlen--, n++, s++)
	;

    return n;
}
#endif

/* This function is equivalent to strnlen() for multibyte strings. */
size_t mbstrnlen(const char *s, size_t maxlen)
{
    assert(s != NULL);

774
#ifdef ENABLE_UTF8
775
    if (ISSET(USE_UTF8)) {
776
777
778
779
	size_t n = 0;
	int s_mb_len;

	while (*s != '\0') {
780
	    s_mb_len = parse_mbchar(s, NULL, NULL);
781

782
	    if (maxlen == 0)
783
784
		break;

785
	    maxlen--;
786
787
	    s += s_mb_len;
	    n++;
788
789
	}

790
	return n;
791
792
    } else
#endif
793
	return strnlen(s, maxlen);
794
}
795
796

#ifndef DISABLE_JUSTIFY
797
798
799
800
801
802
803
804
805
806
807
808
809
/* This function is equivalent to strchr() for multibyte strings. */
char *mbstrchr(const char *s, char *c)
{
    assert(s != NULL && c != NULL);

#ifdef ENABLE_UTF8
    if (ISSET(USE_UTF8)) {
	bool bad_c_mb = FALSE, bad_s_mb = FALSE;
	char *s_mb = charalloc(MB_CUR_MAX);
	const char *q = s;
	wchar_t ws, wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

810
	if (c_mb_len < 0) {
811
812
813
814
815
816
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	    bad_c_mb = TRUE;
	}

	while (*s != '\0') {
817
	    int s_mb_len = parse_mbchar(s, s_mb, NULL);
818

819
	    if (mbtowc(&ws, s_mb, s_mb_len) < 0) {
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
		mbtowc(NULL, NULL, 0);
		ws = (unsigned char)*s;
		bad_s_mb = TRUE;
	    }

	    if (bad_s_mb == bad_c_mb && ws == wc)
		break;

	    s += s_mb_len;
	    q += s_mb_len;
	}

	free(s_mb);

	if (ws != wc)
	    q = NULL;

	return (char *)q;
    } else
#endif
	return strchr(s, *c);
}

843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#ifdef ENABLE_NANORC
/* Return TRUE if the string s contains one or more blank characters,
 * and FALSE otherwise. */
bool has_blank_chars(const char *s)
{
    assert(s != NULL);

    for (; *s != '\0'; s++) {
	if (isblank(*s))
	    return TRUE;
    }

    return FALSE;
}

/* Return TRUE if the multibyte string s contains one or more blank
 * multibyte characters, and FALSE otherwise. */
bool has_blank_mbchars(const char *s)
{
862
    assert(s != NULL);
863

864
#ifdef ENABLE_UTF8
865
    if (ISSET(USE_UTF8)) {
866
867
868
869
870
871
	char *chr_mb = charalloc(MB_CUR_MAX);
	bool retval = FALSE;

	while (*s != '\0') {
	    int chr_mb_len;

872
	    chr_mb_len = parse_mbchar(s, chr_mb, NULL);
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888

	    if (is_blank_mbchar(chr_mb)) {
		retval = TRUE;
		break;
	    }

	    s += chr_mb_len;
	}

	free(chr_mb);

	return retval;
    } else
#endif
	return has_blank_chars(s);
}
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
889
#endif /* ENABLE_NANORC */
890
#endif /* !DISABLE_JUSTIFY */
891

892
#ifdef ENABLE_UTF8
893
/* Return TRUE if wc is valid Unicode, and FALSE otherwise. */
894
895
bool is_valid_unicode(wchar_t wc)
{
896
897
898
    return ((0 <= wc && wc <= 0x10FFFF) && (wc <= 0xD7FF || 0xE000 <=
	wc) && (wc <= 0xFDCF || 0xFDF0 <= wc) && ((wc & 0xFFFF) <=
	0xFFFD));
899
900
901
}
#endif

902
903
904
905
906
907
908
909
#ifdef ENABLE_NANORC
/* Check if the string s is a valid multibyte string.  Return TRUE if it
 * is, and FALSE otherwise. */
bool is_valid_mbstring(const char *s)
{
    assert(s != NULL);

    return 
910
#ifdef ENABLE_UTF8
911
	ISSET(USE_UTF8) ?
912
	(mbstowcs(NULL, s, 0) != (size_t)-1) :
913
914
915
916
917
#endif

	TRUE;
}
#endif /* ENABLE_NANORC */