Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
common/include: Add ASCII-only ctype header and ascii_is* functions.
Our existing uses of <ctype.h> were not necessarily safe if the locale was changed. Also I removed the unnecessary use of isascii, deprecated by POSIX.1-2008.
- Loading branch information
Showing
9 changed files
with
146 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* nbdkit | ||
* Copyright (C) 2013-2020 Red Hat Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of Red Hat nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
/* Normal ctype functions are affected by the current locale. For | ||
* example isupper() might recognize Ä in some but not all locales. | ||
* These functions match only 7 bit ASCII characters. | ||
*/ | ||
|
||
#ifndef NBDKIT_ASCII_CTYPE_H | ||
#define NBDKIT_ASCII_CTYPE_H | ||
|
||
#define ascii_isalnum(c) (ascii_isalpha (c) || ascii_isdigit (c)) | ||
|
||
#define ascii_isalpha(c) \ | ||
(((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) | ||
|
||
#define ascii_isdigit(c) \ | ||
((c) >= '0' && (c) <= '9') | ||
|
||
#define ascii_isspace(c) \ | ||
((c) == '\t' || (c) == '\n' || (c) == '\f' || (c) == '\r' || (c) == ' ') | ||
|
||
#define ascii_isxdigit(c) \ | ||
((c) == '0' || (c) == '1' || (c) == '2' || (c) == '3' || (c) == '4' || \ | ||
(c) == '5' || (c) == '6' || (c) == '7' || (c) == '8' || (c) == '9' || \ | ||
(c) == 'a' || (c) == 'b' || (c) == 'c' || \ | ||
(c) == 'd' || (c) == 'e' || (c) == 'f' || \ | ||
(c) == 'A' || (c) == 'B' || (c) == 'C' || \ | ||
(c) == 'D' || (c) == 'E' || (c) == 'F') | ||
|
||
#endif /* NBDKIT_ASCII_CTYPE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* nbdkit | ||
* Copyright (C) 2020 Red Hat Inc. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are | ||
* met: | ||
* | ||
* * Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of Red Hat nor the names of its contributors may be | ||
* used to endorse or promote products derived from this software without | ||
* specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | ||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
* SUCH DAMAGE. | ||
*/ | ||
|
||
#include <config.h> | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <assert.h> | ||
|
||
#include "ascii-ctype.h" | ||
|
||
int | ||
main (void) | ||
{ | ||
assert (ascii_isspace (' ')); | ||
assert (ascii_isspace ('\t')); | ||
assert (ascii_isspace ('\n')); | ||
assert (! ascii_isspace ('a')); | ||
|
||
assert (ascii_isalpha ('a')); | ||
assert (ascii_isalpha ('Z')); | ||
assert (ascii_isalpha ('z')); | ||
assert (! ascii_isalpha (' ')); | ||
assert (! ascii_isalpha ('0')); | ||
{ const char *s = "Ä"; assert (! ascii_isalpha (s[0])); } | ||
{ const char *s = "®"; assert (! ascii_isalpha (s[0])); } | ||
|
||
assert (ascii_isdigit ('0')); | ||
assert (ascii_isdigit ('9')); | ||
{ const char *s = "Ø"; assert (! ascii_isdigit (s[0])); } /* U+00D8 */ | ||
{ const char *s = "9"; assert (! ascii_isdigit (s[0])); } /* U+FF19 */ | ||
|
||
exit (EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters