36 lines · cpp
1//===-- Implementation of mblen -------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/wchar/mblen.h"10 11#include "hdr/types/size_t.h"12#include "src/__support/common.h"13#include "src/__support/libc_errno.h"14#include "src/__support/macros/config.h"15#include "src/__support/wchar/mbrtowc.h"16#include "src/__support/wchar/mbstate.h"17 18namespace LIBC_NAMESPACE_DECL {19 20LLVM_LIBC_FUNCTION(int, mblen, (const char *s, size_t n)) {21 // returns 0 since UTF-8 encoding is not state-dependent22 if (s == nullptr)23 return 0;24 internal::mbstate internal_mbstate;25 auto ret = internal::mbrtowc(nullptr, s, n, &internal_mbstate);26 if (!ret.has_value() || static_cast<int>(ret.value()) == -2) {27 // Encoding failure28 if (!ret.has_value())29 libc_errno = EILSEQ;30 return -1;31 }32 return static_cast<int>(ret.value());33}34 35} // namespace LIBC_NAMESPACE_DECL36