Merge "Implement mblen(3)."

This commit is contained in:
Treehugger Robot 2016-04-29 00:43:26 +00:00 committed by Gerrit Code Review
commit bd8773d19d
14 changed files with 74 additions and 8 deletions

View file

@ -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 \

35
libc/bionic/mblen.cpp Normal file
View file

@ -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);
}

View file

@ -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()

View file

@ -1279,6 +1279,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1279,6 +1279,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1201,6 +1201,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1304,6 +1304,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1263,6 +1263,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1263,6 +1263,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1201,6 +1201,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1261,6 +1261,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1261,6 +1261,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -1201,6 +1201,7 @@ LIBC_O {
getpwent;
getsubopt;
hasmntopt;
mblen;
pthread_getname_np;
quotactl;
setdomainname;

View file

@ -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));
}