allascii.c revision 1.3
1/*	$NetBSD: allascii.c,v 1.3 2020/03/18 19:05:21 christos Exp $	*/
2
3/*++
4/* NAME
5/*	allascii 3
6/* SUMMARY
7/*	predicate if string is all ASCII
8/* SYNOPSIS
9/*	#include <stringops.h>
10/*
11/*	int	allascii(buffer)
12/*	const char *buffer;
13/*
14/*	int	allascii_len(buffer, len)
15/*	const char *buffer;
16/*	ssize_t	len;
17/* DESCRIPTION
18/*	allascii() determines if its argument is an all-ASCII string.
19/*
20/*	Arguments:
21/* .IP buffer
22/*	The null-terminated input string.
23/* .IP len
24/*	The string length, -1 to determine the length dynamically.
25/* LICENSE
26/* .ad
27/* .fi
28/*	The Secure Mailer license must be distributed with this software.
29/* AUTHOR(S)
30/*	Wietse Venema
31/*	IBM T.J. Watson Research
32/*	P.O. Box 704
33/*	Yorktown Heights, NY 10598, USA
34/*
35/*	Wietse Venema
36/*	Google, Inc.
37/*	111 8th Avenue
38/*	New York, NY 10011, USA
39/*--*/
40
41/* System library. */
42
43#include <sys_defs.h>
44#include <ctype.h>
45#include <string.h>
46
47/* Utility library. */
48
49#include "stringops.h"
50
51/* allascii_len - return true if string is all ASCII */
52
53int     allascii_len(const char *string, ssize_t len)
54{
55    const char *cp;
56    int     ch;
57
58    if (len < 0)
59	len = strlen(string);
60    if (len == 0)
61	return (0);
62    for (cp = string; cp < string + len
63	 && (ch = *(unsigned char *) cp) != 0; cp++)
64	if (!ISASCII(ch))
65	    return (0);
66    return (1);
67}
68