[PATCH resend] include/wine/unicode: Avoid scanning the entire string in memrchrW

Gabriel Ivăncescu gabrielopcode at gmail.com
Mon Apr 22 06:49:03 CDT 2019


Scanning backwards can avoid a full scan of the entire string and makes
more sense since we are looking for the last char.

Signed-off-by: Gabriel Ivăncescu <gabrielopcode at gmail.com>
---
 include/wine/unicode.h | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/include/wine/unicode.h b/include/wine/unicode.h
index ff6f656..671f316 100644
--- a/include/wine/unicode.h
+++ b/include/wine/unicode.h
@@ -287,10 +287,9 @@ WINE_UNICODE_INLINE WCHAR *memchrW( const WCHAR *ptr, WCHAR ch, size_t n )
 
 WINE_UNICODE_INLINE WCHAR *memrchrW( const WCHAR *ptr, WCHAR ch, size_t n )
 {
-    const WCHAR *end;
-    WCHAR *ret = NULL;
-    for (end = ptr + n; ptr < end; ptr++) if (*ptr == ch) ret = (WCHAR *)(ULONG_PTR)ptr;
-    return ret;
+    const WCHAR *p;
+    for (p = ptr + n - 1; p >= ptr; p--) if (*p == ch) return (WCHAR *)(ULONG_PTR)p;
+    return NULL;
 }
 
 WINE_UNICODE_INLINE long int atolW( const WCHAR *str )
-- 
2.21.0




More information about the wine-devel mailing list