NetBSD Problem Report #23460

Received: (qmail 25431 invoked by uid 605); 16 Nov 2003 18:15:54 -0000
Message-Id: <20031116181549.3E71A5693@localhost>
Date: Sun, 16 Nov 2003 19:15:49 +0100 (CET)
From: christianbiere@gmx.de
Sender: gnats-bugs-owner@NetBSD.org
Reply-To: christianbiere@gmx.de
To: gnats-bugs@gnats.netbsd.org
Subject: Small improvements for a few wchar functions
X-Send-Pr-Version: 3.95

>Number:         23460
>Category:       lib
>Synopsis:       Small improvements for a few wchar functions
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Sun Nov 16 18:16:00 +0000 2003
>Closed-Date:    
>Last-Modified:  
>Originator:     Christian Biere <christianbiere@gmx.de>
>Release:        NetBSD 1.6ZE
>Organization:
>Environment:
System: NetBSD cyclonus 1.6ZE NetBSD 1.6ZE (STARSCREAM) #5: Sat Nov 15 01:35:55 CET 2003 bin@cyclonus:/usr/build/sys/arch/i386/compile/STARSCREAM i386
Architecture: i386
Machine: i386
>Description:

wcsrchr.c:

The string doesn't have to be scanned backward. I've used the same
solution as strrchr.c. FreeBSD has fixed to the same strategy a
year ago. I've used a more readable (IMHO) implementation.

wcsstr.c:

"q = little" was absolutely unnecessary. I've also eliminated the
length comparison which caused some overhead. FreeBSD has changed
it's implementation in a different/better way - at least they
claim it's more efficient. Though, this one has no further function
call overhead [See FreeBSD's Implementation for details.].

Rest:

Instead of counting i up to n, just count n down to zero. Less code
and maybe a little faster.

[I hope the patch survives this.]

>How-To-Repeat:
>Fix:

Index: wcsrchr.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/wcsrchr.c,v
retrieving revision 1.2
diff -u -r1.2 wcsrchr.c
--- wcsrchr.c	2001/01/03 14:29:37	1.2
+++ wcsrchr.c	2003/11/16 07:47:30
@@ -41,19 +41,15 @@
 	const wchar_t *s;
 	wchar_t c;
 {
-	const wchar_t *p;
+	const wchar_t *save = NULL;

 	_DIAGASSERT(s != NULL);

-	p = s;
-	while (*p)
-		p++;
-	while (s <= p) {
-		if (*p == c) {
-			/* LINTED interface specification */
-			return (wchar_t *)p;
-		}
-		p--;
-	}
-	return NULL;
+	do {
+		if (*s == c)
+			save = s;
+	} while (*s++);
+
+	/* LINTED interface specification */
+	return (wchar_t *)save;
 }
Index: wcsstr.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/wcsstr.c,v
retrieving revision 1.3
diff -u -r1.3 wcsstr.c
--- wcsstr.c	2003/03/05 20:18:17	1.3
+++ wcsstr.c	2003/11/16 07:47:30
@@ -56,17 +56,18 @@
 		/* LINTED interface specification */
 		return (wchar_t *)big;
 	}
-	if (wcslen(big) < wcslen(little))
-		return NULL;

 	p = big;
-	q = little;
 	while (*p) {
 		q = little;
 		r = p;
 		while (*q) {
-			if (*r != *q)
+			if (*r != *q) {
+				if (!*r)
+					return NULL;
 				break;
+			}
+				
 			q++;
 			r++;
 		}
Index: wmemchr.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/wmemchr.c,v
retrieving revision 1.2
diff -u -r1.2 wmemchr.c
--- wmemchr.c	2001/01/03 14:29:37	1.2
+++ wmemchr.c	2003/11/16 07:47:30
@@ -42,11 +42,10 @@
 	wchar_t c;
 	size_t n;
 {
-	size_t i;

 	_DIAGASSERT(s != NULL);

-	for (i = 0; i < n; i++) {
+	while (n--) {
 		if (*s == c) {
 			/* LINTED const castaway */
 			return (wchar_t *)s;
Index: wmemcmp.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/wmemcmp.c,v
retrieving revision 1.3
diff -u -r1.3 wmemcmp.c
--- wmemcmp.c	2003/04/06 18:33:23	1.3
+++ wmemcmp.c	2003/11/16 07:47:30
@@ -43,12 +43,11 @@
 	const wchar_t *s2;
 	size_t n;
 {
-	size_t i;

 	_DIAGASSERT(s1 != NULL);
 	_DIAGASSERT(s2 != NULL);

-	for (i = 0; i < n; i++) {
+	while (n--) {
 		if (*s1 != *s2) {
 			/* wchar might be unsigned */
 			return *(const __nbrune_t *)s1 >
Index: wmemset.c
===================================================================
RCS file: /cvsroot/src/lib/libc/string/wmemset.c,v
retrieving revision 1.2
diff -u -r1.2 wmemset.c
--- wmemset.c	2001/01/03 14:29:37	1.2
+++ wmemset.c	2003/11/16 07:47:31
@@ -42,15 +42,12 @@
 	wchar_t c;
 	size_t n;
 {
-	size_t i;
 	wchar_t *p;

 	_DIAGASSERT(s != NULL);

-	p = (wchar_t *)s;
-	for (i = 0; i < n; i++) {
+	for (p = (wchar_t *)s; n--; p++)
 		*p = c;
-		p++;
-	}
+
 	return s;
 }
>Release-Note:
>Audit-Trail:
>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.39 2013/11/01 18:47:49 spz Exp $
$NetBSD: gnats_config.sh,v 1.8 2006/05/07 09:23:38 tsutsui Exp $
Copyright © 1994-2007 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.