unistr/u8-strstr, unistr/u16-strstr: Optimize the one-character case.
authorBruno Haible <bruno@clisp.org>
Sat, 31 Jul 2010 20:10:38 +0000 (22:10 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 31 Jul 2010 20:10:38 +0000 (22:10 +0200)
ChangeLog
lib/unistr/u-strstr.h
lib/unistr/u16-strstr.c
lib/unistr/u8-strstr.c
modules/unistr/u16-strstr
modules/unistr/u8-strstr

index dee720b1dec836305bfa52ddde04232631396de3..7a0f040d4fcb038b17ea33277e6aad654101af0e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2010-07-31  Bruno Haible  <bruno@clisp.org>
+
+       unistr/u8-strstr, unistr/u16-strstr: Optimize the one-character case.
+       * lib/unistr/u-strstr.h (FUNC): When the needle contains only one
+       character, perform the search using U_STRCHR.
+       * lib/unistr/u8-strstr.c (U_STRMBTOUC): New macro.
+       * lib/unistr/u16-strstr.c (U_STRMBTOUC): Likewise.
+       * modules/unistr/u8-strstr (Depends-on): Add unistr/u8-strmbtouc.
+       * modules/unistr/u16-strstr (Depends-on): Add unistr/u16-strmbtouc.
+       Suggested by Paolo Bonzini.
+
 2010-07-31  Bruno Haible  <bruno@clisp.org>
 
        unistr/u*-strstr: Fix dependencies.
index df32be8b09d3a5a725f9e3fdf924992d8ad8a6a4..604b20cc98af73ffb48d82465dc2968a131816bf 100644 (file)
@@ -1,5 +1,5 @@
 /* Substring test for UTF-8/UTF-16/UTF-32 strings.
-   Copyright (C) 1999, 2002, 2006, 2009-2010 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2006, 2010 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2002.
 
    This program is free software: you can redistribute it and/or modify it
@@ -24,10 +24,20 @@ FUNC (const UNIT *haystack, const UNIT *needle)
   if (first == 0)
     return (UNIT *) haystack;
 
-  /* Is needle nearly empty?  */
+  /* Is needle nearly empty (only one unit)?  */
   if (needle[1] == 0)
     return U_STRCHR (haystack, first);
 
+#ifdef U_STRMBTOUC
+  /* Is needle nearly empty (only one character)?  */
+  {
+    ucs4_t first_uc;
+    int count = U_STRMBTOUC (&first_uc, needle);
+    if (count > 0 && needle[count] == 0)
+      return U_STRCHR (haystack, first_uc);
+  }
+#endif
+
   /* Search for needle's first unit.  */
   for (; *haystack != 0; haystack++)
     if (*haystack == first)
index 54a74d62fef566074fff9c5edd8ff30d58dcfcd1..804f4e44d3688fac77e59ecb790fa4f591bec594 100644 (file)
@@ -1,5 +1,5 @@
 /* Substring test for UTF-16 strings.
-   Copyright (C) 1999, 2002, 2006, 2009-2010 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2006, 2010 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2002.
 
    This program is free software: you can redistribute it and/or modify it
@@ -25,4 +25,5 @@
 #define FUNC u16_strstr
 #define UNIT uint16_t
 #define U_STRCHR u16_strchr
+#define U_STRMBTOUC u16_strmbtouc
 #include "u-strstr.h"
index cce37adcda61125fa206872a1f64aca1310fc964..7a7d6c1bbed225f015313346b2f5d55ddd0281df 100644 (file)
@@ -1,5 +1,5 @@
 /* Substring test for UTF-8 strings.
-   Copyright (C) 1999, 2002, 2006, 2009-2010 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2002, 2006, 2010 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2002.
 
    This program is free software: you can redistribute it and/or modify it
@@ -25,4 +25,5 @@
 #define FUNC u8_strstr
 #define UNIT uint8_t
 #define U_STRCHR u8_strchr
+#define U_STRMBTOUC u8_strmbtouc
 #include "u-strstr.h"
index 78980ff342fd64cbd49c46a18b8a84113f600522..014ae113b41ad7d4c47da679f8e4e6bdb7442316 100644 (file)
@@ -8,6 +8,7 @@ lib/unistr/u-strstr.h
 Depends-on:
 unistr/base
 unistr/u16-strchr
+unistr/u16-strmbtouc
 
 configure.ac:
 gl_LIBUNISTRING_MODULE([0.9], [unistr/u16-strstr])
index 7d73b2ae3cd39df5863064f155471bed8e698125..30d99a459e78c72308578b3c409e3d221047b6e3 100644 (file)
@@ -8,6 +8,7 @@ lib/unistr/u-strstr.h
 Depends-on:
 unistr/base
 unistr/u8-strchr
+unistr/u8-strmbtouc
 
 configure.ac:
 gl_LIBUNISTRING_MODULE([0.9], [unistr/u8-strstr])