chars.c 18.8 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
27
 *                                                                        *
 **************************************************************************/

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

#include <stdlib.h>
28
#include <string.h>
29
30
31
32
#include <ctype.h>
#include <assert.h>
#include "proto.h"

33
34
#ifdef NANO_WIDE
#ifdef HAVE_WCHAR_H
35
36
#include <wchar.h>
#endif
37
#ifdef HAVE_WCTYPE_H
38
39
#include <wctype.h>
#endif
40
#endif
41

42
43
/* Return TRUE if the value of c is in byte range, and FALSE
 * otherwise. */
44
bool is_byte(int c)
45
{
46
    return ((unsigned int)c == (unsigned char)c);
47
48
}

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

57
58
59
#if defined(NANO_WIDE) && !defined(HAVE_ISWBLANK)
/* This function is equivalent to iswblank(). */
bool niswblank(wint_t wc)
60
{
61
    return iswspace(wc) && (wc == '\t' || !is_cntrl_wchar(wc));
62
}
63
#endif
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

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

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	wchar_t wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	}

80
	return iswblank(wc);
81
82
    } else
#endif
83
	return isblank((unsigned char)*c);
84
85
86
87
}

/* This function is equivalent to iscntrl(), except in that it also
 * handles control characters with their high bits set. */
88
bool is_cntrl_char(int c)
89
{
90
91
    return (-128 <= c && c < -96) || (0 <= c && c < 32) ||
	(127 <= c && c < 160);
92
93
}

94
95
96
97
98
99
100
101
102
103
104
#ifdef NANO_WIDE
/* This function is equivalent to iscntrl() for wide characters, except
 * in that it also handles wide control characters with their high bits
 * set. */
bool is_cntrl_wchar(wint_t wc)
{
    return (-128 <= wc && wc < -96) || (0 <= wc && wc < 32) ||
	(127 <= wc && wc < 160);
}
#endif

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* 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);

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	wchar_t wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	}

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

128
129
130
/* Return TRUE for a multibyte character found in a word (currently only
 * an alphanumeric or punctuation character) and FALSE otherwise. */
bool is_word_mbchar(const char *c)
131
132
133
134
135
136
137
138
139
140
141
142
143
{
    assert(c != NULL);

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	wchar_t wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	}

144
	return iswalnum(wc) || iswpunct(wc);
145
146
    } else
#endif
147
	return isalnum((unsigned char)*c) || ispunct((unsigned char)*c);
148
149
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
150
/* c is a control character.  It displays as ^@, ^?, or ^[ch], where ch
151
 * is c + 64.  We return that character. */
152
char control_rep(char c)
153
154
155
156
157
158
159
160
161
162
{
    /* Treat newlines embedded in a line as encoded nulls. */
    if (c == '\n')
	return '@';
    else if (c == NANO_CONTROL_8)
	return '?';
    else
	return c + 64;
}

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifdef NANO_WIDE
/* c is a wide control character.  It displays as ^@, ^?, or ^[ch],
 * where ch is c + 64.  We return that wide character. */
wchar_t control_wrep(wchar_t wc)
{
    /* 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],
179
180
181
182
183
184
185
186
187
188
189
190
 * where ch is c + 64.  We return that multibyte character. */
char *control_mbrep(const char *c, char *crep, int *crep_len)
{
    assert(c != NULL);

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	wchar_t wc, wcrep;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX), crep_mb_len;

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
191
	    wc = (unsigned char)*c;
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
	}

	wcrep = control_wrep(wc);

	crep_mb_len = wctomb(crep, wcrep);

	if (crep_mb_len <= 0) {
	    wctomb(NULL, 0);
	    crep_mb_len = 0;
	}

	*crep_len = crep_mb_len;
    } else {
#endif
	*crep_len = 1;
207
	*crep = control_rep(*c);
208
209
210
#ifdef NANO_WIDE
    }
#endif
211
212

    return crep;
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}

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

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	wchar_t wc;
	int c_mb_len = mbtowc(&wc, c, MB_CUR_MAX), width;

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	}

	width = wcwidth(wc);
	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)
{
243
    return
244
#ifdef NANO_WIDE
245
	(!ISSET(NO_UTF8)) ? MB_CUR_MAX :
246
#endif
247
	1;
248
249
250
}

/* Convert the value in chr to a multibyte character with the same
251
252
253
 * wide character value as chr.  Return the (dynamically allocated)
 * multibyte character and its length. */
char *make_mbchar(int chr, int *chr_mb_len)
254
{
255
256
    char *chr_mb;

257
    assert(chr_mb_len != NULL);
258

259
260
#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
261
	chr_mb = charalloc(MB_CUR_MAX);
262
263
264
	*chr_mb_len = wctomb(chr_mb, chr);

	if (*chr_mb_len <= 0) {
265
266
	    wctomb(NULL, 0);
	    *chr_mb_len = 0;
267
	}
268
269

	align(&chr_mb);
270
271
272
    } else {
#endif
	*chr_mb_len = 1;
273
	chr_mb = mallocstrncpy(NULL, (char *)&chr, 1);
274
275
276
277
278
279
280
#ifdef NANO_WIDE
    }
#endif

    return chr_mb;
}

281
#if defined(ENABLE_NANORC) || defined(NANO_EXTRA)
282
/* Convert the string str to a valid multibyte string with the same wide
283
284
 * character values as str.  Return the (dynamically allocated)
 * multibyte string. */
285
char *make_mbstring(const char *str)
286
{
287
288
    assert(str != NULL);

289
290
291
292
#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	char *chr_mb = charalloc(MB_CUR_MAX);
	int chr_mb_len;
293
	char *str_mb = charalloc((MB_CUR_MAX * strlen(str)) + 1);
294
	size_t str_mb_len = 0;
295
296

	while (*str != '\0') {
297
	    bool bad_chr;
298
299
	    int i;

300
	    chr_mb_len = parse_mbchar(str, chr_mb, &bad_chr, NULL);
301

302
	    if (bad_chr) {
303
		char *bad_chr_mb;
304
305
		int bad_chr_mb_len;

306
		bad_chr_mb = make_mbchar((unsigned char)*chr_mb,
307
		    &bad_chr_mb_len);
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

		for (i = 0; i < bad_chr_mb_len; i++)
		    str_mb[str_mb_len + i] = bad_chr_mb[i];
		str_mb_len += bad_chr_mb_len;

		free(bad_chr_mb);
	    } else {
		for (i = 0; i < chr_mb_len; i++)
		    str_mb[str_mb_len + i] = chr_mb[i];
		str_mb_len += chr_mb_len;
	    }

	    str += chr_mb_len;
	}

	free(chr_mb);
	null_at(&str_mb, str_mb_len);

	return str_mb;
     } else
#endif
329
	return mallocstrcpy(NULL, str);
330
}
331
#endif /* ENABLE_NANORC || NANO_EXTRA */
332

333
334
/* Parse a multibyte character from buf.  Return the number of bytes
 * used.  If chr isn't NULL, store the multibyte character in it.  If
335
336
337
338
339
 * bad_chr isn't NULL, set it to TRUE if we have a bad multibyte
 * character.  If col isn't NULL, store the new display width in it.  If
 * *str is '\t', we expect col to have the current display width. */
int parse_mbchar(const char *buf, char *chr, bool *bad_chr, size_t
	*col)
340
341
342
343
344
345
346
347
{
    int buf_mb_len;

    assert(buf != NULL);

    if (bad_chr != NULL)
	*bad_chr = FALSE;

348
#ifdef NANO_WIDE
349
350
351
352
353
    if (!ISSET(NO_UTF8)) {
	/* Get the number of bytes in the multibyte character. */
	buf_mb_len = mblen(buf, MB_CUR_MAX);

	/* If buf contains a null byte or an invalid multibyte
354
355
	 * character, set bad_chr to TRUE (if it contains the latter)
	 * and interpret buf's first byte. */
356
357
	if (buf_mb_len <= 0) {
	    mblen(NULL, 0);
358
	    if (buf_mb_len < 0 && bad_chr != NULL)
359
		*bad_chr = TRUE;
360
	    buf_mb_len = 1;
361
362
363
364
365
	}

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

367
368
369
370
371
372
373
374
375
376
377
378
379
	    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
380
	     * returned by control_mbrep(). */
381
	    else if (is_cntrl_mbchar(buf)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
382
		char *ctrl_buf_mb = charalloc(MB_CUR_MAX);
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
		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
415
	     * by control_mbrep(). */
416
417
418
419
420
421
422
423
424
425
426
427
	    else if (is_cntrl_char((unsigned char)*buf))
		*col += 2;
	    /* If we have a normal character, it's one column wide. */
	    else
		(*col)++;
	}
#ifdef NANO_WIDE
    }
#endif

    return buf_mb_len;
}
428
429
430
431
432
433
434

/* 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;

435
    assert(buf != NULL && pos <= strlen(buf));
436
437
438
439

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

443
	if (pos_prev <= (size_t)buf_mb_len)
444
445
446
447
448
449
450
451
452
453
454
455
	    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)
{
456
    return pos + parse_mbchar(buf + pos, NULL, NULL, NULL);
457
}
458
459
460
461
462

#ifndef HAVE_STRCASECMP
/* This function is equivalent to strcasecmp(). */
int nstrcasecmp(const char *s1, const char *s2)
{
463
    return strncasecmp(s1, s2, (size_t)-1);
464
465
466
467
468
469
}
#endif

/* This function is equivalent to strcasecmp() for multibyte strings. */
int mbstrcasecmp(const char *s1, const char *s2)
{
470
    return mbstrncasecmp(s1, s2, (size_t)-1);
471
472
473
474
475
476
477
478
479
480
481
482
483
484
}

#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)
485
	return tolower(*s1) - tolower(*s2);
486
487
488
489
490
491
492
493
494
495
496
    else
	return 0;
}
#endif

/* This function is equivalent to strncasecmp() for multibyte
 * strings. */
int mbstrncasecmp(const char *s1, const char *s2, size_t n)
{
#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
497
498
	char *s1_mb = charalloc(MB_CUR_MAX);
	char *s2_mb = charalloc(MB_CUR_MAX);
499
500
	wchar_t ws1, ws2;

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

503
	while (n > 0 && *s1 != '\0' && *s2 != '\0') {
504
	    bool bad_s1_mb = FALSE, bad_s2_mb = FALSE;
505
506
507
	    int s1_mb_len, s2_mb_len;

	    s1_mb_len = parse_mbchar(s1, s1_mb, NULL, NULL);
508
509
510
511

	    if (mbtowc(&ws1, s1_mb, s1_mb_len) <= 0) {
		mbtowc(NULL, NULL, 0);
		ws1 = (unsigned char)*s1_mb;
512
		bad_s1_mb = TRUE;
513
514
	    }

515
	    s2_mb_len = parse_mbchar(s2, s2_mb, NULL, NULL);
516
517
518
519

	    if (mbtowc(&ws2, s2_mb, s2_mb_len) <= 0) {
		mbtowc(NULL, NULL, 0);
		ws2 = (unsigned char)*s2_mb;
520
		bad_s2_mb = TRUE;
521
522
	    }

523
524
	    if (n == 0 || bad_s1_mb != bad_s2_mb ||
		towlower(ws1) != towlower(ws2))
525
526
527
528
		break;

	    s1 += s1_mb_len;
	    s2 += s2_mb_len;
529
	    n--;
530
531
532
533
534
	}

	free(s1_mb);
	free(s2_mb);

535
	return towlower(ws1) - towlower(ws2);
536
537
    } else
#endif
538
	return strncasecmp(s1, s2, n);
539
540
541
542
543
544
545
546
547
548
}

#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++) {
549
	const char *r = haystack, *q = needle;
550

551
	for (; tolower(*r) == tolower(*q) && *q != '\0'; r++, q++)
552
553
554
555
556
557
558
559
560
561
	    ;

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

    return NULL;
}
#endif

562
563
564
565
566
/* This function is equivalent to strcasestr() for multibyte strings. */
const char *mbstrcasestr(const char *haystack, const char *needle)
{
#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
567
	char *r_mb = charalloc(MB_CUR_MAX);
568
	char *q_mb = charalloc(MB_CUR_MAX);
569
	wchar_t wr, wq;
570
571
572
573
574
	bool found_needle = FALSE;

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

	while (*haystack != '\0') {
575
576
	    const char *r = haystack, *q = needle;
	    int r_mb_len, q_mb_len;
577
578

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

581
		r_mb_len = parse_mbchar(r, r_mb, NULL, NULL);
582

583
		if (mbtowc(&wr, r_mb, r_mb_len) <= 0) {
584
		    mbtowc(NULL, NULL, 0);
585
		    wr = (unsigned char)*r;
586
		    bad_r_mb = TRUE;
587
588
589
590
591
592
593
		}

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

		if (mbtowc(&wq, q_mb, q_mb_len) <= 0) {
		    mbtowc(NULL, NULL, 0);
		    wq = (unsigned char)*q;
594
		    bad_q_mb = TRUE;
595
596
		}

597
598
		if (bad_r_mb != bad_q_mb ||
			towlower(wr) != towlower(wq))
599
600
		    break;

601
		r += r_mb_len;
602
603
604
605
606
607
608
609
		q += q_mb_len;
	    }

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

610
	    haystack += move_mbright(haystack, 0);
611
612
	}

613
	free(r_mb);
614
615
	free(q_mb);

616
	return (found_needle) ? haystack : NULL;
617
618
    } else
#endif
619
	return strcasestr(haystack, needle);
620
621
}

622
623
#ifndef NANO_SMALL
#ifndef DISABLE_TABCOMP
624
/* This function is equivalent to strstr(), except in that it scans the
625
 * string in reverse, starting at rev_start. */
626
627
628
629
630
631
632
633
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;

634
	for (r = rev_start, q = needle; *r == *q && *q != '\0'; r++, q++)
635
636
637
638
639
640
641
642
	    ;

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

    return NULL;
}
643
#endif /* !DISABLE_TABCOMP */
644
645

/* This function is equivalent to strcasestr(), except in that it scans
646
 * the string in reverse, starting at rev_start. */
647
648
649
650
651
652
653
654
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;

655
	for (; tolower(*r) == tolower(*q) && *q != '\0'; r++, q++)
656
657
658
659
660
661
662
663
	    ;

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

    return NULL;
}
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684

/* 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)
{
#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	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') {
685
686
		bool bad_r_mb = FALSE, bad_q_mb = FALSE;

687
688
689
690
691
		r_mb_len = parse_mbchar(r, r_mb, NULL, NULL);

		if (mbtowc(&wr, r_mb, r_mb_len) <= 0) {
		    mbtowc(NULL, NULL, 0);
		    wr = (unsigned char)*r;
692
		    bad_r_mb = TRUE;
693
694
695
696
697
698
699
		}

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

		if (mbtowc(&wq, q_mb, q_mb_len) <= 0) {
		    mbtowc(NULL, NULL, 0);
		    wq = (unsigned char)*q;
700
		    bad_q_mb = TRUE;
701
702
		}

703
704
		if (bad_r_mb != bad_q_mb ||
			towlower(wr) != towlower(wq))
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
		    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);

726
	return (found_needle) ? rev_start : NULL;
727
728
729
730
    } else
#endif
	return revstrcasestr(haystack, needle, rev_start);
}
731
#endif /* !NANO_SMALL */
732

733
734
735
736
737
738
/* This function is equivalent to strlen() for multibyte strings. */
size_t mbstrlen(const char *s)
{
    return mbstrnlen(s, (size_t)-1);
}

739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#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);

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	size_t n = 0;
	int s_mb_len;

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

767
	    if (maxlen == 0)
768
769
		break;

770
	    maxlen--;
771
772
	    s += s_mb_len;
	    n++;
773
774
	}

775
	return n;
776
777
    } else
#endif
778
	return strnlen(s, maxlen);
779
}
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823

#ifndef DISABLE_JUSTIFY
/* This function is equivalent to strchr() for multibyte strings. */
char *mbstrchr(const char *s, char *c)
{
    assert(s != NULL && c != NULL);

#ifdef NANO_WIDE
    if (!ISSET(NO_UTF8)) {
	char *s_mb = charalloc(MB_CUR_MAX);
	const char *q = s;
	wchar_t ws, wc;
	int s_mb_len, c_mb_len = mbtowc(&wc, c, MB_CUR_MAX);

	if (c_mb_len <= 0) {
	    mbtowc(NULL, NULL, 0);
	    wc = (unsigned char)*c;
	}

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

	    if (mbtowc(&ws, s_mb, s_mb_len) <= 0) {
		mbtowc(NULL, NULL, 0);
		ws = (unsigned char)*s;
	    }

	    if (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);
}
824
#endif /* !DISABLE_JUSTIFY */