about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--wcsmbs/wcsnlen.c22
2 files changed, 6 insertions, 18 deletions
diff --git a/ChangeLog b/ChangeLog
index 97e70aa0cb..9ab66cf735 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
 2019-02-27  Adhemerval Zanella  <adhemerval.zanella@linaro.org>
 
+	* wcsmbs/wcsnlen.c (__wcsnlen): Rewrite using wmemchr.
+
 	* wcsmbs/wcsncpy.c (__wcsncpy): Rewrite using wcsnlen, wmemset, and
 	wmemcpy.
 
diff --git a/wcsmbs/wcsnlen.c b/wcsmbs/wcsnlen.c
index f3d5fc1fba..d38c7e4953 100644
--- a/wcsmbs/wcsnlen.c
+++ b/wcsmbs/wcsnlen.c
@@ -26,24 +26,10 @@
 size_t
 __wcsnlen (const wchar_t *s, size_t maxlen)
 {
-  size_t len = 0;
-
-  while (maxlen > 0 && s[len] != L'\0')
-    {
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      if (--maxlen == 0 || s[len] == L'\0')
-	return len;
-      ++len;
-      --maxlen;
-    }
-
-  return len;
+  const wchar_t *ret = __wmemchr (s, L'\0', maxlen);
+  if (ret)
+    maxlen = ret - s;
+  return maxlen;
 }
 #ifndef WCSNLEN
 weak_alias (__wcsnlen, wcsnlen)