[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] libbb: shrink is_suffixed_with() function
From:       Tito <farmatito () tiscali ! it>
Date:       2015-08-27 12:04:10
Message-ID: 55DEFCBA.6070805 () tiscali ! it
[Download RAW message or body]

Hi,
attached you will find a patch that shrinks the is_suffixed_with function
and adds one more test case as suggested by Bartosz Golaszewski.
Bloat-o-meter says:

function                                             old     new   delta
is_suffixed_with                                      83      47    -36
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-36)            Total: -36 bytes

Ciao,
Tito

["is_suffixed_with.patch" (text/x-diff)]

Shrink is_suffixed_with function.

Signed-off-by: Tito Ragusa <farmatito@tiscali.it>

--- libbb/compare_string_array.c.orig	2015-08-27 00:39:27.694416966 +0200
+++ libbb/compare_string_array.c	2015-08-27 13:55:44.997158181 +0200
@@ -35,16 +35,9 @@ char* FAST_FUNC is_prefixed_with(const c
  */
 char* FAST_FUNC is_suffixed_with(const char *string, const char *key)
 {
-	size_t key_len = strlen(key);
-	ssize_t len_diff = strlen(string) - key_len;
-
-	if (len_diff >= 0) {
-		string += len_diff;
-		if (strcmp(string, key) == 0) {
-			return (char*)string;
-		}
-	}
-
+	char *s = strrstr(string, key);
+	if (s && strcmp(s, key) == 0)
+		return s;
 	return NULL;
 }
 
@@ -159,6 +152,7 @@ BBUNIT_DEFINE_TEST(is_suffixed_with)
 	BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("foo", "foo"));
 	BBUNIT_ASSERT_STREQ("", is_suffixed_with("foo", ""));
 	BBUNIT_ASSERT_STREQ("", is_suffixed_with("", ""));
+	BBUNIT_ASSERT_STREQ("foo", is_suffixed_with("barfoofoo", "foo"));
 
 	BBUNIT_ASSERT_NULL(is_suffixed_with("foo", "bar foo"));
 	BBUNIT_ASSERT_NULL(is_suffixed_with("foo foo", "bar"));


_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic