chars.c 21.1 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
    return ((c & 0x60) == 0 || c == 127);
154
155
156
157
158
159
160
161
162
}

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

163
#ifdef ENABLE_UTF8
164
    if (use_utf8) {
165
166
	return ((c[0] & 0xE0) == 0 || c[0] == 127 ||
		((signed char)c[0] == -62 && (signed char)c[1] < -96));
167
168
169
170
171
    } else
#endif
	return is_cntrl_char((unsigned char)*c);
}

172
173
/* This function is equivalent to ispunct() for multibyte characters. */
bool is_punct_mbchar(const char *c)
174
175
176
{
    assert(c != NULL);

177
#ifdef ENABLE_UTF8
178
    if (use_utf8) {
179
180
	wchar_t wc;

181
182
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
183
	    wc = bad_wchar;
184
	}
185

186
	return iswpunct(wc);
187
188
    } else
#endif
189
190
191
192
	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
193
 * an alphanumeric or punctuation character, and only the latter if
194
195
196
197
198
199
200
 * 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);
201
202
}

203
/* Return the visible representation of control character c. */
204
char control_rep(const signed char c)
205
{
206
207
    assert(is_cntrl_char(c));

208
    /* An embedded newline is an encoded null. */
209
210
211
212
    if (c == '\n')
	return '@';
    else if (c == NANO_CONTROL_8)
	return '?';
213
214
215
216
    else if (c == -97)
	return '=';
    else if (c < 0)
	return c + 224;
217
218
219
220
    else
	return c + 64;
}

221
222
/* Return the visible representation of multibyte control character c. */
char control_mbrep(const char *c)
223
{
224
    assert(c != NULL);
225

226
#ifdef ENABLE_UTF8
227
    if (use_utf8) {
228
	if (0 <= c[0] && c[0] <= 127)
229
	    return control_rep(c[0]);
230
	else
231
	    return control_rep(c[1]);
232
    } else
233
#endif
234
	return control_rep(*c);
235
236
}

237
/* c is a multibyte non-control character.  We return that multibyte
238
 * character.  If crep is an invalid multibyte sequence, it will be
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
239
 * replaced with Unicode 0xFFFD (Replacement Character). */
240
241
242
243
244
char *mbrep(const char *c, char *crep, int *crep_len)
{
    assert(c != NULL && crep != NULL && crep_len != NULL);

#ifdef ENABLE_UTF8
245
    if (use_utf8) {
246
247
	wchar_t wc;

248
249
	/* Reject invalid Unicode characters. */
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0 || !is_valid_unicode(wc)) {
250
	    mbtowc_reset();
251
	    *crep_len = bad_mbchar_len;
252
	    strncpy(crep, bad_mbchar, *crep_len);
253
254
255
	} else {
	    *crep_len = wctomb(crep, wc);

256
257
	    if (*crep_len < 0) {
		wctomb_reset();
258
		*crep_len = 0;
259
	    }
260
	}
261
    } else
262
#endif
263
    {
264
265
266
267
268
269
270
	*crep_len = 1;
	*crep = *c;
    }

    return crep;
}

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

276
#ifdef ENABLE_UTF8
277
    if (use_utf8) {
278
	wchar_t wc;
279
	int width;
280

281
282
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
	    mbtowc_reset();
283
	    wc = bad_wchar;
284
	}
285
286

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

288
289
290
291
	if (width == -1) {
	    wc = bad_wchar;
	    width = wcwidth(wc);
	}
292
293
294
295
296
297
298
299
300
301

	return width;
    } else
#endif
	return 1;
}

/* Return the maximum width in bytes of a multibyte character. */
int mb_cur_max(void)
{
302
    return
303
#ifdef ENABLE_UTF8
304
	use_utf8 ? MB_CUR_MAX :
305
#endif
306
	1;
307
308
}

309
310
/* Convert the Unicode value in chr to a multibyte character with the
 * same wide character value as chr, if possible.  If the conversion
311
312
313
 * succeeds, return the (dynamically allocated) multibyte character and
 * its length.  Otherwise, return an undefined (dynamically allocated)
 * multibyte character and a length of zero. */
314
char *make_mbchar(long chr, int *chr_mb_len)
315
{
316
317
    char *chr_mb;

318
    assert(chr_mb_len != NULL);
319

320
#ifdef ENABLE_UTF8
321
    if (use_utf8) {
322
	chr_mb = charalloc(MB_CUR_MAX);
323
	*chr_mb_len = wctomb(chr_mb, (wchar_t)chr);
324

325
	/* Reject invalid Unicode characters. */
326
327
	if (*chr_mb_len < 0 || !is_valid_unicode((wchar_t)chr)) {
	    wctomb_reset();
328
	    *chr_mb_len = 0;
329
	}
330
    } else
331
#endif
332
    {
333
	*chr_mb_len = 1;
334
	chr_mb = mallocstrncpy(NULL, (char *)&chr, 1);
335
336
337
338
339
340
341
    }

    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
342
343
344
 * 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)
345
346
347
348
349
{
    int buf_mb_len;

    assert(buf != NULL);

350
#ifdef ENABLE_UTF8
351
    if (use_utf8) {
352
353
354
	/* Get the number of bytes in the multibyte character. */
	buf_mb_len = mblen(buf, MB_CUR_MAX);

355
	/* When the multibyte sequence is invalid, only take the first byte. */
356
	if (buf_mb_len < 0) {
357
	    IGNORE_CALL_RESULT(mblen(NULL, 0));
358
	    buf_mb_len = 1;
359
360
	} else if (buf_mb_len == 0)
	    buf_mb_len++;
361

362
	/* When requested, store the multibyte character in chr. */
363
364
	if (chr != NULL) {
	    int i;
365

366
367
368
369
	    for (i = 0; i < buf_mb_len; i++)
		chr[i] = buf[i];
	}

370
	/* When requested, store the width of the wide character in col. */
371
372
373
374
375
	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;
376
377
	    /* If we have a control character, it's two columns wide: one
	     * column for the "^", and one for the visible character. */
378
	    else if (is_cntrl_mbchar(buf)) {
379
		*col += 2;
380
381
382
383
384
	    /* If we have a normal character, get its width in columns
	     * normally. */
	    } else
		*col += mbwidth(buf);
	}
385
    } else
386
#endif
387
    {
388
	/* A byte character is one byte long. */
389
390
	buf_mb_len = 1;

391
	/* When requested, store the byte character in chr. */
392
393
394
	if (chr != NULL)
	    *chr = *buf;

395
	/* When requested, store the width of the wide character in col. */
396
397
398
399
400
	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;
401
402
	    /* If we have a control character, it's two columns wide: one
	     * column for the "^", and one for the visible character. */
403
404
405
406
407
408
409
410
411
412
	    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;
}
413
414
415
416
417

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

420
    assert(buf != NULL && pos <= strlen(buf));
421
422

    /* There is no library function to move backward one multibyte
423
424
425
426
427
428
429
     * 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();

430
431
432
    while (before < pos) {
	char_len = parse_mbchar(buf + before, NULL, NULL);
	before += char_len;
433
434
    }

435
    return before - char_len;
436
437
438
439
440
441
}

/* 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)
{
442
    return pos + parse_mbchar(buf + pos, NULL, NULL);
443
}
444
445
446
447
448

#ifndef HAVE_STRCASECMP
/* This function is equivalent to strcasecmp(). */
int nstrcasecmp(const char *s1, const char *s2)
{
449
    return strncasecmp(s1, s2, HIGHEST_POSITIVE);
450
451
452
453
454
455
}
#endif

/* This function is equivalent to strcasecmp() for multibyte strings. */
int mbstrcasecmp(const char *s1, const char *s2)
{
456
    return mbstrncasecmp(s1, s2, HIGHEST_POSITIVE);
457
458
459
460
461
462
}

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

466
467
    assert(s1 != NULL && s2 != NULL);

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
468
    for (; *s1 != '\0' && *s2 != '\0' && n > 0; s1++, s2++, n--) {
469
470
471
472
	if (tolower(*s1) != tolower(*s2))
	    break;
    }

473
    return (n > 0) ? tolower(*s1) - tolower(*s2) : 0;
474
475
476
}
#endif

477
/* This function is equivalent to strncasecmp() for multibyte strings. */
478
479
int mbstrncasecmp(const char *s1, const char *s2, size_t n)
{
480
#ifdef ENABLE_UTF8
481
    if (use_utf8) {
482
	wchar_t wc1, wc2;
483

484
485
486
	if (s1 == s2)
	    return 0;

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

489
490
	for (; *s1 != '\0' && *s2 != '\0' && n > 0;
		s1 += move_mbright(s1, 0), s2 += move_mbright(s2, 0), n--) {
491
	    bool bad1 = FALSE, bad2 = FALSE;
492

493
	    if (mbtowc(&wc1, s1, MB_CUR_MAX) < 0) {
494
		mbtowc_reset();
495
		wc1 = (unsigned char)*s1;
496
		bad1 = TRUE;
497
498
	    }

499
	    if (mbtowc(&wc2, s2, MB_CUR_MAX) < 0) {
500
		mbtowc_reset();
501
		wc2 = (unsigned char)*s2;
502
		bad2 = TRUE;
503
504
	    }

505
	    if (bad1 != bad2 || towlower(wc1) != towlower(wc2))
506
507
508
		break;
	}

509
	return (n > 0) ? towlower(wc1) - towlower(wc2) : 0;
510
511
    } else
#endif
512
	return strncasecmp(s1, s2, n);
513
514
515
}

#ifndef HAVE_STRCASESTR
516
/* This function is equivalent to strcasestr(). */
517
char *nstrcasestr(const char *haystack, const char *needle)
518
{
519
520
    size_t haystack_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
523
    if (*needle == '\0')
524
	return (char *)haystack;
525

526
527
    haystack_len = strlen(haystack);
    needle_len = strlen(needle);
528

529
530
531
    for (; *haystack != '\0' && haystack_len >= needle_len; haystack++,
	haystack_len--) {
	if (strncasecmp(haystack, needle, needle_len) == 0)
532
	    return (char *)haystack;
533
534
535
536
537
538
    }

    return NULL;
}
#endif

539
/* This function is equivalent to strcasestr() for multibyte strings. */
540
char *mbstrcasestr(const char *haystack, const char *needle)
541
{
542
#ifdef ENABLE_UTF8
543
    if (use_utf8) {
544
	size_t haystack_len, needle_len;
545
546
547

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
548
	if (*needle == '\0')
549
	    return (char *)haystack;
550

551
552
	haystack_len = mbstrlen(haystack);
	needle_len = mbstrlen(needle);
553

554
555
	for (; *haystack != '\0' && haystack_len >= needle_len;
		haystack += move_mbright(haystack, 0), haystack_len--) {
556
557
	    if (mbstrncasecmp(haystack, needle, needle_len) == 0 &&
			mblen(haystack, MB_CUR_MAX) > 0)
558
		return (char *)haystack;
559
560
	}

561
	return NULL;
562
563
    } else
#endif
564
	return (char *) strcasestr(haystack, needle);
565
566
}

567
#if !defined(NANO_TINY) || !defined(DISABLE_TABCOMP)
568
/* This function is equivalent to strstr(), except in that it scans the
569
 * string in reverse, starting at rev_start. */
570
571
char *revstrstr(const char *haystack, const char *needle, const char
	*rev_start)
572
{
573
574
    size_t rev_start_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
577
    if (*needle == '\0')
578
	return (char *)rev_start;
579

580
    needle_len = strlen(needle);
581

582
583
    if (strlen(haystack) < needle_len)
	return NULL;
584

585
586
587
588
589
    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)
590
	    return (char *)rev_start;
591
592
593
594
    }

    return NULL;
}
595
#endif /* !NANO_TINY || !DISABLE_TABCOMP */
596

597
#ifndef NANO_TINY
598
/* This function is equivalent to strcasestr(), except in that it scans
599
 * the string in reverse, starting at rev_start. */
600
601
char *revstrcasestr(const char *haystack, const char *needle, const char
	*rev_start)
602
{
603
604
    size_t rev_start_len, needle_len;

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

David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
607
    if (*needle == '\0')
608
	return (char *)rev_start;
609

610
611
612
613
    needle_len = strlen(needle);

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

615
    rev_start_len = strlen(rev_start);
616

617
618
619
    for (; rev_start >= haystack; rev_start--, rev_start_len++) {
	if (rev_start_len >= needle_len && strncasecmp(rev_start,
		needle, needle_len) == 0)
620
	    return (char *)rev_start;
621
622
623
624
    }

    return NULL;
}
625
626

/* This function is equivalent to strcasestr() for multibyte strings,
627
 * except in that it scans the string in reverse, starting at rev_start. */
628
629
char *mbrevstrcasestr(const char *haystack, const char *needle, const
	char *rev_start)
630
{
631
#ifdef ENABLE_UTF8
632
    if (use_utf8) {
633
	size_t rev_start_len, needle_len;
634
635
636

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

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

640
	needle_len = mbstrlen(needle);
641

642
643
	if (mbstrlen(haystack) < needle_len)
	    return NULL;
644

645
	rev_start_len = mbstrlen(rev_start);
646

647
	while (TRUE) {
648
649
650
	    if (rev_start_len >= needle_len &&
			mbstrncasecmp(rev_start, needle, needle_len) == 0 &&
			mblen(rev_start, MB_CUR_MAX) > 0)
651
		return (char *)rev_start;
652

653
	    /* If we've reached the head of the haystack, we found nothing. */
654
	    if (rev_start == haystack)
655
		return NULL;
656

657
658
659
	    rev_start = haystack + move_mbleft(haystack, rev_start - haystack);
	    rev_start_len++;
	}
660
661
662
663
    } else
#endif
	return revstrcasestr(haystack, needle, rev_start);
}
664
#endif /* !NANO_TINY */
665

666
667
668
669
670
671
/* This function is equivalent to strlen() for multibyte strings. */
size_t mbstrlen(const char *s)
{
    return mbstrnlen(s, (size_t)-1);
}

672
673
674
675
676
677
678
679
#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
680
    for (; *s != '\0' && maxlen > 0; s++, maxlen--, n++)
681
682
683
684
685
686
687
688
689
690
691
	;

    return n;
}
#endif

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

692
#ifdef ENABLE_UTF8
693
    if (use_utf8) {
694
695
	size_t n = 0;

696
697
698
	for (; *s != '\0' && maxlen > 0; s += move_mbright(s, 0),
		maxlen--, n++)
	    ;
699

700
	return n;
701
702
    } else
#endif
703
	return strnlen(s, maxlen);
704
}
705

706
#if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY)
707
/* This function is equivalent to strchr() for multibyte strings. */
708
char *mbstrchr(const char *s, const char *c)
709
710
711
712
{
    assert(s != NULL && c != NULL);

#ifdef ENABLE_UTF8
713
    if (use_utf8) {
David Lawrence Ramsey's avatar
David Lawrence Ramsey committed
714
	bool bad_s_mb = FALSE, bad_c_mb = FALSE;
715
716
717
718
	char *s_mb = charalloc(MB_CUR_MAX);
	const char *q = s;
	wchar_t ws, wc;

Benno Schulenberg's avatar
Benno Schulenberg committed
719
	if (mbtowc(&wc, c, MB_CUR_MAX) < 0) {
720
	    mbtowc_reset();
721
722
723
724
725
	    wc = (unsigned char)*c;
	    bad_c_mb = TRUE;
	}

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

728
	    if (mbtowc(&ws, s_mb, s_mb_len) < 0) {
729
		mbtowc_reset();
730
731
732
733
734
735
736
737
738
739
740
741
742
		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);

743
	if (*s == '\0')
744
745
746
747
748
	    q = NULL;

	return (char *)q;
    } else
#endif
749
	return (char *) strchr(s, *c);
750
}
751
#endif /* !NANO_TINY || !DISABLE_JUSTIFY */
752

753
754
755
756
757
758
759
#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
760
    if (use_utf8) {
761
	for (; *s != '\0'; s += move_mbright(s, 0)) {
762
763
764
765
766
767
768
	    if (mbstrchr(accept, s) != NULL)
		return (char *)s;
	}

	return NULL;
    } else
#endif
769
	return (char *) strpbrk(s, accept);
770
771
772
773
774
775
776
777
778
}

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

779
780
781
782
783
    if (*rev_start == '\0') {
	if (rev_start == s)
	   return NULL;
	rev_start--;
    }
784

785
786
    for (; rev_start >= s; rev_start--) {
	if (strchr(accept, *rev_start) != NULL)
787
788
789
790
791
792
793
	    return (char *)rev_start;
    }

    return NULL;
}

/* This function is equivalent to strpbrk() for multibyte strings,
794
 * except in that it scans the string in reverse, starting at rev_start. */
795
796
797
798
799
800
char *mbrevstrpbrk(const char *s, const char *accept, const char
	*rev_start)
{
    assert(s != NULL && accept != NULL && rev_start != NULL);

#ifdef ENABLE_UTF8
801
    if (use_utf8) {
802
803
804
805
806
	if (*rev_start == '\0') {
	    if (rev_start == s)
		return NULL;
	    rev_start = s + move_mbleft(s, rev_start - s);
	}
807

808
809
	while (TRUE) {
	    if (mbstrchr(accept, rev_start) != NULL)
810
811
		return (char *)rev_start;

812
	    /* If we've reached the head of the string, we found nothing. */
813
	    if (rev_start == s)
814
		return NULL;
815

816
817
	    rev_start = s + move_mbleft(s, rev_start - s);
	}
818
819
820
821
822
823
    } else
#endif
	return revstrpbrk(s, accept, rev_start);
}
#endif /* !NANO_TINY */

824
#if !defined(DISABLE_NANORC) && (!defined(NANO_TINY) || !defined(DISABLE_JUSTIFY))
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/* 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)
{
843
    assert(s != NULL);
844

845
#ifdef ENABLE_UTF8
846
    if (use_utf8) {
847
	bool retval = FALSE;
848
	char *chr_mb = charalloc(MB_CUR_MAX);
849

850
851
	for (; *s != '\0'; s += move_mbright(s, 0)) {
	    parse_mbchar(s, chr_mb, NULL);
852
853
854
855
856
857
858
859
860
861
862
863
864
865

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

	free(chr_mb);

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

868
#ifdef ENABLE_UTF8
869
/* Return TRUE if wc is valid Unicode, and FALSE otherwise. */
870
871
bool is_valid_unicode(wchar_t wc)
{
872
    return ((0 <= wc && wc <= 0xD7FF) ||
873
874
875
		(0xE000 <= wc && wc <= 0xFDCF) ||
		(0xFDF0 <= wc && wc <= 0xFFFD) ||
		(0xFFFF < wc && wc <= 0x10FFFF && (wc & 0xFFFF) <= 0xFFFD));
876
877
878
}
#endif

879
#ifndef DISABLE_NANORC
880
881
882
883
884
885
/* 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
886
    return
887
#ifdef ENABLE_UTF8
888
	use_utf8 ? (mbstowcs(NULL, s, 0) != (size_t)-1) :
889
890
891
#endif
	TRUE;
}
892
#endif /* !DISABLE_NANORC */