diff --git a/libc/Android.mk b/libc/Android.mk index fd1423213..b84c1e13b 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -81,7 +81,6 @@ libc_common_src_files := \ stdio/sprintf.c \ stdio/vfprintf.c \ stdlib/atexit.c \ - stdlib/ctype_.c \ stdlib/getenv.c \ stdlib/putenv.c \ stdlib/setenv.c \ @@ -338,10 +337,12 @@ libc_upstream_openbsd_gdtoa_src_files_64 := \ libc_upstream_openbsd_src_files := \ upstream-openbsd/lib/libc/gen/alarm.c \ + upstream-openbsd/lib/libc/gen/ctype_.c \ upstream-openbsd/lib/libc/gen/exec.c \ upstream-openbsd/lib/libc/gen/fnmatch.c \ upstream-openbsd/lib/libc/gen/ftok.c \ upstream-openbsd/lib/libc/gen/getprogname.c \ + upstream-openbsd/lib/libc/gen/isctype.c \ upstream-openbsd/lib/libc/gen/setprogname.c \ upstream-openbsd/lib/libc/gen/time.c \ upstream-openbsd/lib/libc/gen/tolower_.c \ diff --git a/libc/stdlib/ctype_.c b/libc/upstream-openbsd/lib/libc/gen/ctype_.c similarity index 52% rename from libc/stdlib/ctype_.c rename to libc/upstream-openbsd/lib/libc/gen/ctype_.c index c4f294d58..89c825758 100644 --- a/libc/stdlib/ctype_.c +++ b/libc/upstream-openbsd/lib/libc/gen/ctype_.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ctype_.c,v 1.9 2005/08/08 08:05:33 espie Exp $ */ +/* $OpenBSD: ctype_.c,v 1.10 2011/09/22 09:06:10 stsp Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. * All rights reserved. @@ -36,15 +36,6 @@ #include #include "ctype_private.h" -#define _U _CTYPE_U -#define _L _CTYPE_L -#define _D _CTYPE_D -#define _S _CTYPE_S -#define _P _CTYPE_P -#define _C _CTYPE_C -#define _X _CTYPE_X -#define _B _CTYPE_B - const char _C_ctype_[1 + CTYPE_NUM_CHARS] = { 0, _C, _C, _C, _C, _C, _C, _C, _C, @@ -53,8 +44,8 @@ const char _C_ctype_[1 + CTYPE_NUM_CHARS] = { _C, _C, _C, _C, _C, _C, _C, _C, _S|(char)_B, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, _P, - _D, _D, _D, _D, _D, _D, _D, _D, - _D, _D, _P, _P, _P, _P, _P, _P, + _N, _N, _N, _N, _N, _N, _N, _N, + _N, _N, _P, _P, _P, _P, _P, _P, _P, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U|_X, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, _U, @@ -62,98 +53,24 @@ const char _C_ctype_[1 + CTYPE_NUM_CHARS] = { _P, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L|_X, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, _L, - /* determine printability based on the IS0 8859 8-bit standard */ _L, _L, _L, _P, _P, _P, _P, _C, - _C, _C, _C, _C, _C, _C, _C, _C, /* 80 */ - _C, _C, _C, _C, _C, _C, _C, _C, /* 88 */ - _C, _C, _C, _C, _C, _C, _C, _C, /* 90 */ - _C, _C, _C, _C, _C, _C, _C, _C, /* 98 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* A0 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* A8 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* B0 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* B8 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* C0 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* C8 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* D0 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* D8 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* E0 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* E8 */ - _P, _P, _P, _P, _P, _P, _P, _P, /* F0 */ - _P, _P, _P, _P, _P, _P, _P, _P /* F8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 80 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 88 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 90 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 98 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* A0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* A8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* B0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* B8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* C0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* C8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* D0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* D8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* E0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* E8 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* F0 */ + 0, 0, 0, 0, 0, 0, 0, 0 /* F8 */ }; const char *_ctype_ = _C_ctype_; - -int isalnum(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L|_D))); -} - -int isalpha(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L))); -} - -int iscntrl(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _C)); -} - -int isdigit(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _D)); -} - -int isgraph(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_P|_U|_L|_D))); -} - -int islower(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _L)); -} - -int isprint(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_P|_U|_L|_D|_B))); -} - -int ispunct(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _P)); -} - -int isspace(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _S)); -} - -int isupper(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _U)); -} - -int isxdigit(int c) -{ - return (c == -1 ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_D|_X))); -} - -int isblank(int c) -{ - return (c == ' ' || c == '\t'); -} - -int isascii(int c) -{ - return ((unsigned int)c <= 0177); -} - -int toascii(int c) -{ - return (c & 0177); -} - -__strong_alias(_toupper, toupper); -__strong_alias(_tolower, tolower); diff --git a/libc/upstream-openbsd/lib/libc/gen/isctype.c b/libc/upstream-openbsd/lib/libc/gen/isctype.c new file mode 100644 index 000000000..970b5e274 --- /dev/null +++ b/libc/upstream-openbsd/lib/libc/gen/isctype.c @@ -0,0 +1,150 @@ +/* $OpenBSD: isctype.c,v 1.11 2005/08/08 08:05:34 espie Exp $ */ +/* + * Copyright (c) 1989 The Regents of the University of California. + * All rights reserved. + * (c) UNIX System Laboratories, Inc. + * All or some portions of this file are derived from material licensed + * to the University of California by American Telephone and Telegraph + * Co. or Unix System Laboratories, Inc. and are reproduced herein with + * the permission of UNIX System Laboratories, Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. 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. + * 3. Neither the name of the University 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 THE REGENTS 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 THE REGENTS 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. + */ + +#define _ANSI_LIBRARY +#include +#include + +#undef isalnum +int +isalnum(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L|_N))); +} + +#undef isalpha +int +isalpha(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_U|_L))); +} + +#undef isblank +int +isblank(int c) +{ + return (c == ' ' || c == '\t'); +} + +#undef iscntrl +int +iscntrl(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _C)); +} + +#undef isdigit +int +isdigit(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _N)); +} + +#undef isgraph +int +isgraph(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_P|_U|_L|_N))); +} + +#undef islower +int +islower(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _L)); +} + +#undef isprint +int +isprint(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_P|_U|_L|_N|_B))); +} + +#undef ispunct +int +ispunct(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _P)); +} + +#undef isspace +int +isspace(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _S)); +} + +#undef isupper +int +isupper(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & _U)); +} + +#undef isxdigit +int +isxdigit(int c) +{ + return (c == EOF ? 0 : ((_ctype_ + 1)[(unsigned char)c] & (_N|_X))); +} + +#undef isascii +int +isascii(int c) +{ + return ((unsigned int)c <= 0177); +} + +#undef toascii +int +toascii(int c) +{ + return (c & 0177); +} + +#undef _toupper +int +_toupper(int c) +{ + return (c - 'a' + 'A'); +} + +#undef _tolower +int +_tolower(int c) +{ + return (c - 'A' + 'a'); +} diff --git a/libc/stdlib/ctype_private.h b/libc/upstream-openbsd/lib/libc/include/ctype_private.h similarity index 100% rename from libc/stdlib/ctype_private.h rename to libc/upstream-openbsd/lib/libc/include/ctype_private.h diff --git a/libc/upstream-openbsd/openbsd-compat.h b/libc/upstream-openbsd/openbsd-compat.h index 2c3fe564b..b55f39050 100644 --- a/libc/upstream-openbsd/openbsd-compat.h +++ b/libc/upstream-openbsd/openbsd-compat.h @@ -20,4 +20,16 @@ #define _GNU_SOURCE #define __USE_BSD +/* OpenBSD's uses these names, which conflicted with stlport. + * Additionally, we changed the numeric/digit type from N to D for libcxx. + */ +#define _U _CTYPE_U +#define _L _CTYPE_L +#define _N _CTYPE_D +#define _S _CTYPE_S +#define _P _CTYPE_P +#define _C _CTYPE_C +#define _X _CTYPE_X +#define _B _CTYPE_B + #endif diff --git a/tests/Android.mk b/tests/Android.mk index b32d8d40a..723d7cfee 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -40,6 +40,7 @@ test_cflags = \ libBionicStandardTests_src_files := \ buffer_tests.cpp \ + ctype_test.cpp \ dirent_test.cpp \ eventfd_test.cpp \ fcntl_test.cpp \ diff --git a/tests/ctype_test.cpp b/tests/ctype_test.cpp new file mode 100644 index 000000000..7b27d6475 --- /dev/null +++ b/tests/ctype_test.cpp @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +TEST(ctype, isalnum) { + EXPECT_TRUE(isalnum('1')); + EXPECT_TRUE(isalnum('a')); + EXPECT_TRUE(isalnum('A')); + EXPECT_FALSE(isalnum('!')); + EXPECT_FALSE(isalnum(' ')); +} + +TEST(ctype, isalpha) { + EXPECT_FALSE(isalpha('1')); + EXPECT_TRUE(isalpha('a')); + EXPECT_TRUE(isalpha('A')); + EXPECT_FALSE(isalpha('!')); + EXPECT_FALSE(isalpha(' ')); +} + +TEST(ctype, isascii) { + EXPECT_TRUE(isascii('\x7f')); + EXPECT_FALSE(isascii('\x80')); +} + +TEST(ctype, isblank) { + EXPECT_FALSE(isblank('1')); + EXPECT_TRUE(isblank(' ')); + EXPECT_TRUE(isblank('\t')); +} + +TEST(ctype, iscntrl) { + EXPECT_FALSE(iscntrl('1')); + EXPECT_TRUE(iscntrl('\b')); +} + +TEST(ctype, isdigit) { + EXPECT_TRUE(isdigit('1')); + EXPECT_FALSE(isdigit('a')); + EXPECT_FALSE(isdigit('x')); +} + +TEST(ctype, isgraph) { + EXPECT_TRUE(isgraph('a')); + EXPECT_TRUE(isgraph('A')); + EXPECT_TRUE(isgraph('1')); + EXPECT_TRUE(isgraph('!')); + EXPECT_FALSE(isgraph(' ')); +} + +TEST(ctype, islower) { + EXPECT_TRUE(islower('a')); + EXPECT_FALSE(islower('A')); + EXPECT_FALSE(islower('!')); +} + +TEST(ctype, isprint) { + EXPECT_TRUE(isprint('a')); + EXPECT_TRUE(isprint(' ')); + EXPECT_FALSE(isprint('\b')); +} + +TEST(ctype, ispunct) { + EXPECT_TRUE(ispunct('!')); + EXPECT_FALSE(ispunct('a')); + EXPECT_FALSE(ispunct(' ')); + EXPECT_FALSE(ispunct('\b')); +} + +TEST(ctype, isspace) { + EXPECT_TRUE(isspace(' ')); + EXPECT_TRUE(isspace('\f')); + EXPECT_TRUE(isspace('\n')); + EXPECT_TRUE(isspace('\r')); + EXPECT_TRUE(isspace('\t')); + EXPECT_TRUE(isspace('\v')); + EXPECT_FALSE(isspace('a')); + EXPECT_FALSE(isspace('!')); +} + +TEST(ctype, isupper) { + EXPECT_TRUE(isupper('A')); + EXPECT_FALSE(isupper('a')); + EXPECT_FALSE(isupper('!')); +} + +TEST(ctype, isxdigit) { + EXPECT_TRUE(isxdigit('0')); + EXPECT_FALSE(isxdigit('x')); + EXPECT_TRUE(isxdigit('1')); + EXPECT_TRUE(isxdigit('a')); + EXPECT_TRUE(isxdigit('A')); + EXPECT_FALSE(isxdigit('g')); + EXPECT_FALSE(isxdigit(' ')); +} + +TEST(ctype, toascii) { + EXPECT_EQ('a', toascii('a')); + EXPECT_EQ('a', toascii(0x80 | 'a')); +} + +TEST(ctype, tolower) { + EXPECT_EQ('!', tolower('!')); + EXPECT_EQ('a', tolower('a')); + EXPECT_EQ('a', tolower('A')); +} + +TEST(ctype, _tolower) { + // _tolower may mangle characters for which isupper is false. + EXPECT_EQ('a', _tolower('A')); +} + +TEST(ctype, toupper) { + EXPECT_EQ('!', toupper('!')); + EXPECT_EQ('A', toupper('a')); + EXPECT_EQ('A', toupper('A')); +} + +TEST(ctype, _toupper) { + // _toupper may mangle characters for which islower is false. + EXPECT_EQ('A', _toupper('a')); +}