[libc-commits] [libc] [libc] add wmemchr, wcslen, wcschr, wcsrchr, wcspbrk, wcsstr (PR #121183)

Nick Desaulniers via libc-commits libc-commits at lists.llvm.org
Thu Jan 23 08:58:58 PST 2025


================
@@ -0,0 +1,22 @@
+//===-- Implementation of wcslen ------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/wchar/wcslen.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(size_t, wcslen, (const wchar_t *s)) {
+  size_t length = 0;
+  while (s[length++])
+    ;
----------------
nickdesaulniers wrote:

wchar_t has conically been 2B on Windows and 4B on Unixes, so we can read more than one byte at time all by having the vanilla implementation as written here.

---
This impl has an off by one bug in it; `length` will get post incremented regardless of whether `s[length]` is truthy or not. So my repost will look slightly different.

https://github.com/llvm/llvm-project/pull/121183


More information about the libc-commits mailing list