diff options
author | Elliott Hughes <enh@google.com> | 2016-04-28 14:54:52 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2016-04-28 16:41:29 -0700 |
commit | 6f6f9058fa7ba3cc9d53bd93e46c254ef8e8cd96 (patch) | |
tree | 572c806c787d2c3a1626a9d97b37de78072f3485 | |
parent | 671e6b20eabd0a1ab973f77dff272a00a4096ee7 (diff) | |
download | android_bionic-6f6f9058fa7ba3cc9d53bd93e46c254ef8e8cd96.tar.gz android_bionic-6f6f9058fa7ba3cc9d53bd93e46c254ef8e8cd96.tar.bz2 android_bionic-6f6f9058fa7ba3cc9d53bd93e46c254ef8e8cd96.zip |
Implement mblen(3).
Change-Id: I65948ea5b9ecd63f966ba767ad6db4a2effc4700
-rw-r--r-- | libc/Android.mk | 1 | ||||
-rw-r--r-- | libc/bionic/mblen.cpp | 35 | ||||
-rw-r--r-- | libc/include/stdlib.h | 13 | ||||
-rw-r--r-- | libc/libc.arm.brillo.map | 1 | ||||
-rw-r--r-- | libc/libc.arm.map | 1 | ||||
-rw-r--r-- | libc/libc.arm64.map | 1 | ||||
-rw-r--r-- | libc/libc.map.txt | 1 | ||||
-rw-r--r-- | libc/libc.mips.brillo.map | 1 | ||||
-rw-r--r-- | libc/libc.mips.map | 1 | ||||
-rw-r--r-- | libc/libc.mips64.map | 1 | ||||
-rw-r--r-- | libc/libc.x86.brillo.map | 1 | ||||
-rw-r--r-- | libc/libc.x86.map | 1 | ||||
-rw-r--r-- | libc/libc.x86_64.map | 1 | ||||
-rw-r--r-- | tests/stdlib_test.cpp | 23 |
14 files changed, 74 insertions, 8 deletions
diff --git a/libc/Android.mk b/libc/Android.mk index 374ffa65a..ac7cd7535 100644 --- a/libc/Android.mk +++ b/libc/Android.mk @@ -133,6 +133,7 @@ libc_bionic_ndk_src_files := \ bionic/lockf.cpp \ bionic/lstat.cpp \ bionic/malloc_info.cpp \ + bionic/mblen.cpp \ bionic/mbrtoc16.cpp \ bionic/mbrtoc32.cpp \ bionic/mbstate.cpp \ diff --git a/libc/bionic/mblen.cpp b/libc/bionic/mblen.cpp new file mode 100644 index 000000000..7def6f138 --- /dev/null +++ b/libc/bionic/mblen.cpp @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * All rights reserved. + * + * 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 + * COPYRIGHT OWNER 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 <stdlib.h> +#include <wchar.h> + +int mblen(const char* s, size_t n) { + mbstate_t state = {}; + return mbrlen(s, n, &state); +} diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h index 2abaec396..ba4b14986 100644 --- a/libc/include/stdlib.h +++ b/libc/include/stdlib.h @@ -162,14 +162,11 @@ extern lldiv_t lldiv(long long, long long) __pure2; extern const char* getprogname(void); extern void setprogname(const char*); -/* make STLPort happy */ -extern int mblen(const char *, size_t) __UNAVAILABLE; -extern size_t mbstowcs(wchar_t *, const char *, size_t); -extern int mbtowc(wchar_t *, const char *, size_t); - -/* Likewise, make libstdc++-v3 happy. */ -extern int wctomb(char *, wchar_t); -extern size_t wcstombs(char *, const wchar_t *, size_t); +int mblen(const char*, size_t); +size_t mbstowcs(wchar_t*, const char*, size_t); +int mbtowc(wchar_t*, const char*, size_t); +int wctomb(char*, wchar_t); +size_t wcstombs(char*, const wchar_t*, size_t); extern size_t __ctype_get_mb_cur_max(void); #define MB_CUR_MAX __ctype_get_mb_cur_max() diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map index 3e78e307c..a1373d899 100644 --- a/libc/libc.arm.brillo.map +++ b/libc/libc.arm.brillo.map @@ -1279,6 +1279,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.arm.map b/libc/libc.arm.map index 420cd7e30..4e69bf112 100644 --- a/libc/libc.arm.map +++ b/libc/libc.arm.map @@ -1279,6 +1279,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map index bdde35eee..7ea74a672 100644 --- a/libc/libc.arm64.map +++ b/libc/libc.arm64.map @@ -1201,6 +1201,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.map.txt b/libc/libc.map.txt index be37ffbd3..8d1a1f9d0 100644 --- a/libc/libc.map.txt +++ b/libc/libc.map.txt @@ -1304,6 +1304,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map index 6b5744cab..aac1d8ced 100644 --- a/libc/libc.mips.brillo.map +++ b/libc/libc.mips.brillo.map @@ -1263,6 +1263,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.mips.map b/libc/libc.mips.map index 6ee9cf586..e514aa135 100644 --- a/libc/libc.mips.map +++ b/libc/libc.mips.map @@ -1263,6 +1263,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map index bdde35eee..7ea74a672 100644 --- a/libc/libc.mips64.map +++ b/libc/libc.mips64.map @@ -1201,6 +1201,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map index d483d3e55..f14861d30 100644 --- a/libc/libc.x86.brillo.map +++ b/libc/libc.x86.brillo.map @@ -1261,6 +1261,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.x86.map b/libc/libc.x86.map index f0679c052..f6b6ea1ed 100644 --- a/libc/libc.x86.map +++ b/libc/libc.x86.map @@ -1261,6 +1261,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map index bdde35eee..7ea74a672 100644 --- a/libc/libc.x86_64.map +++ b/libc/libc.x86_64.map @@ -1201,6 +1201,7 @@ LIBC_O { getpwent; getsubopt; hasmntopt; + mblen; pthread_getname_np; quotactl; setdomainname; diff --git a/tests/stdlib_test.cpp b/tests/stdlib_test.cpp index 05438eb17..773230f2f 100644 --- a/tests/stdlib_test.cpp +++ b/tests/stdlib_test.cpp @@ -537,3 +537,26 @@ TEST(stdlib, getsubopt) { ASSERT_EQ(-1, getsubopt(&subopts, tokens, &value)); } + +TEST(stdlib, mblen) { + // "If s is a null pointer, mblen() shall return a non-zero or 0 value, if character encodings, + // respectively, do or do not have state-dependent encodings." We're always UTF-8. + EXPECT_EQ(0, mblen(nullptr, 1)); + + ASSERT_STREQ("C.UTF-8", setlocale(LC_ALL, "C.UTF-8")); + + // 1-byte UTF-8. + EXPECT_EQ(1, mblen("abcdef", 6)); + // 2-byte UTF-8. + EXPECT_EQ(2, mblen("\xc2\xa2" "cdef", 6)); + // 3-byte UTF-8. + EXPECT_EQ(3, mblen("\xe2\x82\xac" "def", 6)); + // 4-byte UTF-8. + EXPECT_EQ(4, mblen("\xf0\xa4\xad\xa2" "ef", 6)); + + // Illegal over-long sequence. + ASSERT_EQ(-1, mblen("\xf0\x82\x82\xac" "ef", 6)); + + // "mblen() shall ... return 0 (if s points to the null byte)". + EXPECT_EQ(0, mblen("", 1)); +} |