chars.c 22.3 KB
Newer Older
1
2
3
/**************************************************************************
 *   chars.c                                                              *
 *                                                                        *
4
5
 *   Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,  *
 *   2010, 2011, 2013, 2014 Free Software Foundation, Inc.                *
6
7
 *   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 *
8
 *   the Free Software Foundation; either version 3, or (at your option)  *
9
10
 *   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
#include "proto.h"
24

25
#include <string.h>
26
27
#include <ctype.h>

28
#ifdef ENABLE_UTF8
29
#ifdef HAVE_WCHAR_H
30
31
#include <wchar.h>
#endif
32
#ifdef HAVE_WCTYPE_H
33
34
#include <wctype.h>
#endif
35

36
37
static bool use_utf8 = FALSE;
	/* Whether we've enabled UTF-8 support. */
38
39
static const wchar_t bad_wchar = 0xFFFD;
	/* If we get an invalid multibyte sequence, we treat it as
40
41
	 * Unicode FFFD (Replacement Character), unless we're searching
	 * for a match to it. */
42
static const char *const bad_mbchar = "\xEF\xBF\xBD";
43
static const int bad_mbchar_len = 3;
44
45
46
47
48
49
50
51
52
53
54
55

/* Enable UTF-8 support. */
void utf8_init(void)
{
    use_utf8 = TRUE;
}

/* Is UTF-8 support enabled? */
bool using_utf8(void)
{
    return use_utf8;
}
Benno Schulenberg's avatar
Benno Schulenberg committed
56
#endif /* ENABLE_UTF8 */
57

58
/* Concatenate two allocated strings, and free the second. */
59
char *addstrings(char* str1, size_t len1, char* str2, size_t len2)
60
61
62
{
    str1 = charealloc(str1, len1 + len2 + 1);
    str1[len1] = '\0';
63

64
65
66
67
68
69
    strncat(&str1[len1], str2, len2);
    free(str2);

    return str1;
}

70
71
#ifndef HAVE_ISBLANK
/* This function is equivalent to isblank(). */
72
bool nisblank(int c)
73
{
74
    return isspace(c) && (c == '\t' || !is_cntrl_char(c));
75
}
76
#endif
77

78
#if !defined(HAVE_ISWBLANK) && defined(ENABLE_UTF8)
79
/* This function is equivalent to iswblank(). */
80
bool niswblank(wchar_t wc)
81
{
82
    return iswspace(wc) && (wc == '\t' || !is_cntrl_wchar(wc));
83
}
84
#endif
85

86
/* Return TRUE if the value of c is in byte range, and FALSE otherwise. */
87
88
89
90
91
bool is_byte(int c)
{
    return ((unsigned int)c == (unsigned char)c);
}

92
93
94
95
96
97
98
99
100
101
void mbtowc_reset(void)
{
    IGNORE_CALL_RESULT(mbtowc(NULL, NULL, 0));
}

void wctomb_reset(void)
{
    IGNORE_CALL_RESULT(wctomb(NULL, 0));
}

102
103
104
105
106
/* This function is equivalent to isalnum() for multibyte characters. */
bool is_alnum_mbchar(const char *c)
{
    assert(c != NULL);

107
#ifdef ENABLE_UTF8
108
    if (use_utf8) {
109
110
	wchar_t wc;

111
112
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
113
	    wc = bad_wchar;
114
	}
115
116
117
118
119
120
121

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

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

127
#ifdef ENABLE_UTF8
128
    if (use_utf8) {
129
130
	wchar_t wc;

131
132
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
133
	    wc = bad_wchar;
134
	}
135

136
	return iswblank(wc);
137
138
    } else
#endif
139
	return isblank((unsigned char)*c);
140
141
}

142
143
144
145
146
147
148
/* This function is equivalent to iscntrl(), except in that it only
 * handles non-high-bit control characters. */
bool is_ascii_cntrl_char(int c)
{
    return (0 <= c && c < 32);
}

149
/* This function is equivalent to iscntrl(), except in that it also
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
150
 * handles high-bit control characters. */
151
bool is_cntrl_char(int c)
152
{
153
154
    return (-128 <= c && c < -96) || (0 <= c && c < 32) ||
	(127 <= c && c < 160);
155
156
}

157
#ifdef ENABLE_UTF8
158
159
160
/* This function is equivalent to iscntrl() for wide characters, except
 * in that it also handles wide control characters with their high bits
 * set. */
161
bool is_cntrl_wchar(wchar_t wc)
162
{
163
    return (0 <= wc && wc < 32) || (127 <= wc && wc < 160);
164
165
166
}
#endif

167
168
169
170
171
172
173
/* 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);

174
#ifdef ENABLE_UTF8
175
    if (use_utf8) {
176
177
	wchar_t wc;

178
179
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
180
	    wc = bad_wchar;
181
	}
182
183
184
185
186
187
188

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

189
190
/* This function is equivalent to ispunct() for multibyte characters. */
bool is_punct_mbchar(const char *c)
191
192
193
{
    assert(c != NULL);

194
#ifdef ENABLE_UTF8
195
    if (use_utf8) {
196
197
	wchar_t wc;

198
199
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
200
	    wc = bad_wchar;
201
	}
202

203
	return iswpunct(wc);
204
205
    } else
#endif
206
207
208
209
	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
210
 * an alphanumeric or punctuation character, and only the latter if
211
212
213
214
215
216
217
 * 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);
218
219
}

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
220
/* c is a control character.  It displays as ^@, ^?, or ^[ch], where ch
221
 * is (c + 64).  We return that character. */
222
char control_rep(char c)
223
{
224
225
    assert(is_cntrl_char(c));

226
227
228
229
230
231
232
233
234
    /* Treat newlines embedded in a line as encoded nulls. */
    if (c == '\n')
	return '@';
    else if (c == NANO_CONTROL_8)
	return '?';
    else
	return c + 64;
}

235
#ifdef ENABLE_UTF8
236
/* c is a wide control character.  It displays as ^@, ^?, or ^[ch],
237
 * where ch is (c + 64).  We return that wide character. */
238
239
wchar_t control_wrep(wchar_t wc)
{
240
241
    assert(is_cntrl_wchar(wc));

242
243
244
245
246
247
248
249
250
251
252
    /* 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],
253
254
 * where ch is (c + 64).  We return that multibyte character.  If crep
 * is an invalid multibyte sequence, it will be replaced with Unicode
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
255
 * 0xFFFD (Replacement Character). */
256
257
char *control_mbrep(const char *c, char *crep, int *crep_len)
{
258
    assert(c != NULL && crep != NULL && crep_len != NULL);
259

260
#ifdef ENABLE_UTF8
261
    if (use_utf8) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
262
	wchar_t wc;
263

264
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
265
	    mbtowc_reset();
266
	    *crep_len = bad_mbchar_len;
267
	    strncpy(crep, bad_mbchar, *crep_len);
268
269
270
	} else {
	    *crep_len = wctomb(crep, control_wrep(wc));

271
272
	    if (*crep_len < 0) {
		wctomb_reset();
273
		*crep_len = 0;
274
	    }
275
	}
276
    } else
277
#endif
278
    {
279
	*crep_len = 1;
280
	*crep = control_rep(*c);
281
    }
282
283

    return crep;
284
285
}

286
/* c is a multibyte non-control character.  We return that multibyte
287
 * character.  If crep is an invalid multibyte sequence, it will be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
288
 * replaced with Unicode 0xFFFD (Replacement Character). */
289
290
291
292
293
char *mbrep(const char *c, char *crep, int *crep_len)
{
    assert(c != NULL && crep != NULL && crep_len != NULL);

#ifdef ENABLE_UTF8
294
    if (use_utf8) {
295
296
	wchar_t wc;

297
298
	/* Reject invalid Unicode characters. */
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0 || !is_valid_unicode(wc)) {
299
	    mbtowc_reset();
300
	    *crep_len = bad_mbchar_len;
301
	    strncpy(crep, bad_mbchar, *crep_len);
302
303
304
	} else {
	    *crep_len = wctomb(crep, wc);

305
306
	    if (*crep_len < 0) {
		wctomb_reset();
307
		*crep_len = 0;
308
	    }
309
	}
310
    } else
311
#endif
312
    {
313
314
315
316
317
318
319
	*crep_len = 1;
	*crep = *c;
    }

    return crep;
}

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

325
#ifdef ENABLE_UTF8
326
    if (use_utf8) {
327
	wchar_t wc;
328
	int width;
329

330
331
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
332
	    wc = bad_wchar;
333
	}
334
335

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

337
338
339
340
	if (width == -1) {
	    wc = bad_wchar;
	    width = wcwidth(wc);
	}
341
342
343
344
345
346
347
348
349
350

	return width;
    } else
#endif
	return 1;
}

/* Return the maximum width in bytes of a multibyte character. */
int mb_cur_max(void)
{
351
    return
352
#ifdef ENABLE_UTF8
353
	use_utf8 ? MB_CUR_MAX :
354
#endif
355
	1;
356
357
}

358
359
/* Convert the Unicode value in chr to a multibyte character with the
 * same wide character value as chr, if possible.  If the conversion
360
361
362
 * succeeds, return the (dynamically allocated) multibyte character and
 * its length.  Otherwise, return an undefined (dynamically allocated)
 * multibyte character and a length of zero. */
363
char *make_mbchar(long chr, int *chr_mb_len)
364
{
365
366
    char *chr_mb;

367
    assert(chr_mb_len != NULL);
368

369
#ifdef ENABLE_UTF8
370
    if (use_utf8) {
371
	chr_mb = charalloc(MB_CUR_MAX);
372
	*chr_mb_len = wctomb(chr_mb, (wchar_t)chr);
373

374
	/* Reject invalid Unicode characters. */
375
376
	if (*chr_mb_len < 0 || !is_valid_unicode((wchar_t)chr)) {
	    wctomb_reset();
377
	    *chr_mb_len = 0;
378
	}
379
    } else
380
#endif
381
    {
382
	*chr_mb_len = 1;
383
	chr_mb = mallocstrncpy(NULL, (char *)&chr, 1);
384
385
386
387
388
389
390
    }

    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
391
392
393
 * 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)
394
395
396
397
398
{
    int buf_mb_len;

    assert(buf != NULL);

399
#ifdef ENABLE_UTF8
400
    if (use_utf8) {
401
402
403
	/* Get the number of bytes in the multibyte character. */
	buf_mb_len = mblen(buf, MB_CUR_MAX);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
404
405
	/* If buf contains an invalid multibyte character, only
	 * interpret buf's first byte. */
406
	if (buf_mb_len < 0) {
407
	    IGNORE_CALL_RESULT(mblen(NULL, 0));
408
	    buf_mb_len = 1;
409
410
	} else if (buf_mb_len == 0)
	    buf_mb_len++;
411
412
413
414

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

416
417
418
419
420
421
422
423
424
425
426
427
428
	    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
429
	     * returned by control_mbrep(). */
430
	    else if (is_cntrl_mbchar(buf)) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
431
		char *ctrl_buf_mb = charalloc(MB_CUR_MAX);
432
433
434
435
436
		int ctrl_buf_mb_len;

		(*col)++;

		ctrl_buf_mb = control_mbrep(buf, ctrl_buf_mb,
437
						&ctrl_buf_mb_len);
438
439
440
441
442
443
444
445
446

		*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);
	}
447
    } else
448
#endif
449
    {
450
451
452
453
454
455
456
457
458
459
460
461
	/* 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;
462
463
	    /* If we have a control character, it's two columns wide: one
	     * column for the "^", and one for the visible character. */
464
465
466
467
468
469
470
471
472
473
	    else if (is_cntrl_char((unsigned char)*buf))
		*col += 2;
	    /* If we have a normal character, it's one column wide. */
	    else
		(*col)++;
	}
    }

    return buf_mb_len;
}
474
475
476
477
478

/* 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)
{
479
    size_t before, char_len = 0;
480

481
    assert(buf != NULL && pos <= strlen(buf));
482
483

    /* There is no library function to move backward one multibyte
484
485
486
487
488
489
490
     * character.  So we just start groping for one at the farthest
     * possible point. */
    if (mb_cur_max() > pos)
	before = 0;
    else
	before = pos - mb_cur_max();

491
492
493
    while (before < pos) {
	char_len = parse_mbchar(buf + before, NULL, NULL);
	before += char_len;
494
495
    }

496
    return before - char_len;
497
498
499
500
501
502
}

/* 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)
{
503
    return pos + parse_mbchar(buf + pos, NULL, NULL);
504
}
505
506
507
508
509

#ifndef HAVE_STRCASECMP
/* This function is equivalent to strcasecmp(). */
int nstrcasecmp(const char *s1, const char *s2)
{
510
    return strncasecmp(s1, s2, (size_t)-1);
511
512
513
514
515
516
}
#endif

/* This function is equivalent to strcasecmp() for multibyte strings. */
int mbstrcasecmp(const char *s1, const char *s2)
{
517
    return mbstrncasecmp(s1, s2, (size_t)-1);
518
519
520
521
522
523
}

#ifndef HAVE_STRNCASECMP
/* This function is equivalent to strncasecmp(). */
int nstrncasecmp(const char *s1, const char *s2, size_t n)
{
524
525
526
    if (s1 == s2)
	return 0;

527
528
    assert(s1 != NULL && s2 != NULL);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
529
    for (; *s1 != '\0' && *s2 != '\0' && n > 0; s1++, s2++, n--) {
530
531
532
533
	if (tolower(*s1) != tolower(*s2))
	    break;
    }

534
    return (n > 0) ? tolower(*s1) - tolower(*s2) : 0;
535
536
537
}
#endif

538
/* This function is equivalent to strncasecmp() for multibyte strings. */
539
540
int mbstrncasecmp(const char *s1, const char *s2, size_t n)
{
541
#ifdef ENABLE_UTF8
542
    if (use_utf8) {
543
	wchar_t wc1, wc2;
544

545
546
547
	if (s1 == s2)
	    return 0;

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

550
551
	for (; *s1 != '\0' && *s2 != '\0' && n > 0;
		s1 += move_mbright(s1, 0), s2 += move_mbright(s2, 0), n--) {
552
	    bool bad1 = FALSE, bad2 = FALSE;
553

554
	    if (mbtowc(&wc1, s1, MB_CUR_MAX) < 0) {
555
		mbtowc_reset();
556
		wc1 = (unsigned char)*s1;
557
		bad1 = TRUE;
558
559
	    }

560
	    if (mbtowc(&wc2, s2, MB_CUR_MAX) < 0) {
561
		mbtowc_reset();
562
		wc2 = (unsigned char)*s2;
563
		bad2 = TRUE;
564
565
	    }

566
	    if (bad1 != bad2 || towlower(wc1) != towlower(wc2))
567
568
569
		break;
	}

570
	return (n > 0) ? towlower(wc1) - towlower(wc2) : 0;
571
572
    } else
#endif
573
	return strncasecmp(s1, s2, n);
574
575
576
}

#ifndef HAVE_STRCASESTR
577
/* This function is equivalent to strcasestr(). */
578
char *nstrcasestr(const char *haystack, const char *needle)
579
{
580
581
    size_t haystack_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
584
    if (*needle == '\0')
585
	return (char *)haystack;
586

587
588
    haystack_len = strlen(haystack);
    needle_len = strlen(needle);
589

590
591
592
    for (; *haystack != '\0' && haystack_len >= needle_len; haystack++,
	haystack_len--) {
	if (strncasecmp(haystack, needle, needle_len) == 0)
593
	    return (char *)haystack;
594
595
596
597
598
599
    }

    return NULL;
}
#endif

600
/* This function is equivalent to strcasestr() for multibyte strings. */
601
char *mbstrcasestr(const char *haystack, const char *needle)
602
{
603
#ifdef ENABLE_UTF8
604
    if (use_utf8) {
605
	size_t haystack_len, needle_len;
606
607
608

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
609
	if (*needle == '\0')
610
	    return (char *)haystack;
611

612
613
	haystack_len = mbstrlen(haystack);
	needle_len = mbstrlen(needle);
614

615
616
	for (; *haystack != '\0' && haystack_len >= needle_len;
		haystack += move_mbright(haystack, 0), haystack_len--) {
617
618
	    if (mbstrncasecmp(haystack, needle, needle_len) == 0 &&
			mblen(haystack, MB_CUR_MAX) > 0)
619
		return (char *)haystack;
620
621
	}

622
	return NULL;
623
624
    } else
#endif
625
	return (char *) strcasestr(haystack, needle);
626
627
}

628
#if !defined(NANO_TINY) || !defined(DISABLE_TABCOMP)
629
/* This function is equivalent to strstr(), except in that it scans the
630
 * string in reverse, starting at rev_start. */
631
632
char *revstrstr(const char *haystack, const char *needle, const char
	*rev_start)
633
{
634
635
    size_t rev_start_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
638
    if (*needle == '\0')
639
	return (char *)rev_start;
640

641
    needle_len = strlen(needle);
642

643
644
    if (strlen(haystack) < needle_len)
	return NULL;
645

646
647
648
649
650
    rev_start_len = strlen(rev_start);

    for (; rev_start >= haystack; rev_start--, rev_start_len++) {
	if (rev_start_len >= needle_len && strncmp(rev_start, needle,
		needle_len) == 0)
651
	    return (char *)rev_start;
652
653
654
655
    }

    return NULL;
}
656
#endif /* !NANO_TINY || !DISABLE_TABCOMP */
657

658
#ifndef NANO_TINY
659
/* This function is equivalent to strcasestr(), except in that it scans
660
 * the string in reverse, starting at rev_start. */
661
662
char *revstrcasestr(const char *haystack, const char *needle, const char
	*rev_start)
663
{
664
665
    size_t rev_start_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
668
    if (*needle == '\0')
669
	return (char *)rev_start;
670

671
672
673
674
    needle_len = strlen(needle);

    if (strlen(haystack) < needle_len)
	return NULL;
675

676
    rev_start_len = strlen(rev_start);
677

678
679
680
    for (; rev_start >= haystack; rev_start--, rev_start_len++) {
	if (rev_start_len >= needle_len && strncasecmp(rev_start,
		needle, needle_len) == 0)
681
	    return (char *)rev_start;
682
683
684
685
    }

    return NULL;
}
686
687

/* This function is equivalent to strcasestr() for multibyte strings,
688
 * except in that it scans the string in reverse, starting at rev_start. */
689
690
char *mbrevstrcasestr(const char *haystack, const char *needle, const
	char *rev_start)
691
{
692
#ifdef ENABLE_UTF8
693
    if (use_utf8) {
694
695
	bool begin_line = FALSE;
	size_t rev_start_len, needle_len;
696
697
698

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
699
	if (*needle == '\0')
700
	    return (char *)rev_start;
701

702
	needle_len = mbstrlen(needle);
703

704
705
	if (mbstrlen(haystack) < needle_len)
	    return NULL;
706

707
	rev_start_len = mbstrlen(rev_start);
708

709
	while (!begin_line) {
710
711
712
	    if (rev_start_len >= needle_len &&
			mbstrncasecmp(rev_start, needle, needle_len) == 0 &&
			mblen(rev_start, MB_CUR_MAX) > 0)
713
		return (char *)rev_start;
714
715
716

	    if (rev_start == haystack)
		begin_line = TRUE;
717
	    else {
718
719
		rev_start = haystack + move_mbleft(haystack, rev_start -
			haystack);
720
721
		rev_start_len++;
	    }
722
723
	}

724
	return NULL;
725
726
727
728
    } else
#endif
	return revstrcasestr(haystack, needle, rev_start);
}
729
#endif /* !NANO_TINY */
730

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

737
738
739
740
741
742
743
744
#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);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
745
    for (; *s != '\0' && maxlen > 0; s++, maxlen--, n++)
746
747
748
749
750
751
752
753
754
755
756
	;

    return n;
}
#endif

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

757
#ifdef ENABLE_UTF8
758
    if (use_utf8) {
759
760
	size_t n = 0;

761
762
763
	for (; *s != '\0' && maxlen > 0; s += move_mbright(s, 0),
		maxlen--, n++)
	    ;
764

765
	return n;
766
767
    } else
#endif
768
	return strnlen(s, maxlen);
769
}
770

771
#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
772
/* This function is equivalent to strchr() for multibyte strings. */
773
char *mbstrchr(const char *s, const char *c)
774
775
776
777
{
    assert(s != NULL && c != NULL);

#ifdef ENABLE_UTF8
778
    if (use_utf8) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
779
	bool bad_s_mb = FALSE, bad_c_mb = FALSE;
780
781
782
783
	char *s_mb = charalloc(MB_CUR_MAX);
	const char *q = s;
	wchar_t ws, wc;

Benno Schulenberg's avatar
Benno Schulenberg committed
784
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
785
	    mbtowc_reset();
786
787
788
789
790
	    wc = (unsigned char)*c;
	    bad_c_mb = TRUE;
	}

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

793
	    if (mbtowc(&ws, s_mb, s_mb_len) < 0) {
794
		mbtowc_reset();
795
796
797
798
799
800
801
802
803
804
805
806
807
		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);

808
	if (*s == '\0')
809
810
811
812
813
	    q = NULL;

	return (char *)q;
    } else
#endif
814
	return (char *) strchr(s, *c);
815
}
816
#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
817

818
819
820
821
822
823
824
#ifndef NANO_TINY
/* This function is equivalent to strpbrk() for multibyte strings. */
char *mbstrpbrk(const char *s, const char *accept)
{
    assert(s != NULL && accept != NULL);

#ifdef ENABLE_UTF8
825
    if (use_utf8) {
826
	for (; *s != '\0'; s += move_mbright(s, 0)) {
827
828
829
830
831
832
833
	    if (mbstrchr(accept, s) != NULL)
		return (char *)s;
	}

	return NULL;
    } else
#endif
834
	return (char *) strpbrk(s, accept);
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
}

/* This function is equivalent to strpbrk(), except in that it scans the
 * string in reverse, starting at rev_start. */
char *revstrpbrk(const char *s, const char *accept, const char
	*rev_start)
{
    assert(s != NULL && accept != NULL && rev_start != NULL);

    for (; rev_start >= s; rev_start--) {
	const char *q = (*rev_start == '\0') ? NULL : strchr(accept,
		*rev_start);

	if (q != NULL)
	    return (char *)rev_start;
    }

    return NULL;
}

/* This function is equivalent to strpbrk() for multibyte strings,
856
 * except in that it scans the string in reverse, starting at rev_start. */
857
858
859
860
861
862
char *mbrevstrpbrk(const char *s, const char *accept, const char
	*rev_start)
{
    assert(s != NULL && accept != NULL && rev_start != NULL);

#ifdef ENABLE_UTF8
863
    if (use_utf8) {
864
865
866
	bool begin_line = FALSE;

	while (!begin_line) {
867
868
869
870
871
872
	    const char *q = (*rev_start == '\0') ? NULL :
		mbstrchr(accept, rev_start);

	    if (q != NULL)
		return (char *)rev_start;

873
874
875
876
	    if (rev_start == s)
		begin_line = TRUE;
	    else
		rev_start = s + move_mbleft(s, rev_start - s);
877
878
879
880
881
882
883
884
885
	}

	return NULL;
    } else
#endif
	return revstrpbrk(s, accept, rev_start);
}
#endif /* !NANO_TINY */

886
#if !defined(DISABLE_NANORC) && (!defined(NANO_TINY) || !defined(DISABLE_JUSTIFY))
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/* 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)
{
905
    assert(s != NULL);
906

907
#ifdef ENABLE_UTF8
908
    if (use_utf8) {
909
	bool retval = FALSE;
910
	char *chr_mb = charalloc(MB_CUR_MAX);
911

912
913
	for (; *s != '\0'; s += move_mbright(s, 0)) {
	    parse_mbchar(s, chr_mb, NULL);
914
915
916
917
918
919
920
921
922
923
924
925
926
927

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

	free(chr_mb);

	return retval;
    } else
#endif
	return has_blank_chars(s);
}
928
#endif /* !DISABLE_NANORC && (!NANO_TINY || !DISABLE_JUSTIFY) */
929

930
#ifdef ENABLE_UTF8
931
/* Return TRUE if wc is valid Unicode, and FALSE otherwise. */
932
933
bool is_valid_unicode(wchar_t wc)
{
934
    return ((0 <= wc && wc <= 0xD7FF) ||
935
936
937
		(0xE000 <= wc && wc <= 0xFDCF) ||
		(0xFDF0 <= wc && wc <= 0xFFFD) ||
		(0xFFFF < wc && wc <= 0x10FFFF && (wc & 0xFFFF) <= 0xFFFD));
938
939
940
}
#endif

941
#ifndef DISABLE_NANORC
942
943
944
945
946
947
/* 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);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
948
    return
949
#ifdef ENABLE_UTF8
950
	use_utf8 ? (mbstowcs(NULL, s, 0) != (size_t)-1) :
951
952
953
#endif
	TRUE;
}
954
#endif /* !DISABLE_NANORC */