gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] 01/03: MHD_str_quote(): optimized for typical scenario


From: gnunet
Subject: [libmicrohttpd] 01/03: MHD_str_quote(): optimized for typical scenario
Date: Tue, 07 Jun 2022 18:34:04 +0200

This is an automated email from the git hooks/post-receive script.

karlson2k pushed a commit to branch master
in repository libmicrohttpd.

commit bde583e6474553c8e86d1286b82ac3bbb04c0fee
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Tue Jun 7 13:29:02 2022 +0300

    MHD_str_quote(): optimized for typical scenario
---
 src/microhttpd/mhd_str.c | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

diff --git a/src/microhttpd/mhd_str.c b/src/microhttpd/mhd_str.c
index f8e1965f..d3d8c594 100644
--- a/src/microhttpd/mhd_str.c
+++ b/src/microhttpd/mhd_str.c
@@ -1489,25 +1489,45 @@ MHD_str_quote (const char *unquoted,
   r = 0;
   w = 0;
 
-  if (unquoted_len > buf_size)
-    return 0; /* The output buffer is too small */
-
-  while (unquoted_len > r)
+#ifndef MHD_FAVOR_SMALL_CODE
+  if (unquoted_len * 2 <= buf_size)
   {
-    if (buf_size <= w)
-      return 0; /* The output buffer is too small */
-    else
+    /* Fast loop: the output will fit the buffer with any input string content 
*/
+    while (unquoted_len > r)
     {
       const char chr = unquoted[r++];
       if (('\\' == chr) || ('\"' == chr))
-      {
         result[w++] = '\\'; /* Escape current char */
-        if (buf_size <= w)
-          return 0; /* The output buffer is too small */
-      }
       result[w++] = chr;
     }
   }
+  else
+  {
+    if (unquoted_len > buf_size)
+      return 0; /* Quick fail: the output buffer is too small */
+#else  /* MHD_FAVOR_SMALL_CODE */
+  if (1)
+  {
+#endif /* MHD_FAVOR_SMALL_CODE */
+
+    while (unquoted_len > r)
+    {
+      if (buf_size <= w)
+        return 0; /* The output buffer is too small */
+      else
+      {
+        const char chr = unquoted[r++];
+        if (('\\' == chr) || ('\"' == chr))
+        {
+          result[w++] = '\\'; /* Escape current char */
+          if (buf_size <= w)
+            return 0; /* The output buffer is too small */
+        }
+        result[w++] = chr;
+      }
+    }
+  }
+
   mhd_assert (w >= r);
   mhd_assert (w <= r * 2);
   return w;

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]