summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAli Alzyod <ali198724@gmail.com>2020-06-22 17:31:54 +0900
committerWooHyun Jung <wh0705.jung@samsung.com>2020-06-22 17:31:54 +0900
commitc501d09c3aa4c3e7f1ab5a27171b897c3cb4a11c (patch)
tree3de35b8b209289a16a85a56d2e323e0fb8b921ca
parentfd24fb568eb600357c4a84b6fc4aa2ba8e36c86d (diff)
eina_strbuf: introduce change last occurrence function
Reviewers: cedric, woohyun, bowonryu Reviewed By: cedric Subscribers: cedric, #reviewers, #committers Tags: #efl Maniphest Tasks: T8757 Differential Revision: https://phab.enlightenment.org/D11990
-rw-r--r--src/lib/eina/eina_strbuf.h13
-rw-r--r--src/lib/eina/eina_strbuf_common.c55
-rw-r--r--src/tests/eina/eina_test_strbuf.c6
3 files changed, 73 insertions, 1 deletions
diff --git a/src/lib/eina/eina_strbuf.h b/src/lib/eina/eina_strbuf.h
index da94435c2d..15d6294349 100644
--- a/src/lib/eina/eina_strbuf.h
+++ b/src/lib/eina/eina_strbuf.h
@@ -609,6 +609,19 @@ EAPI Eina_Bool eina_strbuf_replace(Eina_Strbuf *buf, const char *str, const char
609#define eina_strbuf_replace_first(buf, str, with) eina_strbuf_replace(buf, str, with, 1) 609#define eina_strbuf_replace_first(buf, str, with) eina_strbuf_replace(buf, str, with, 1)
610 610
611/** 611/**
612 * @brief Replaces the last occurrence of a substring with another string.
613 *
614 * @param[in,out] buf The string buffer.
615 * @param[in] str The text to match.
616 * @param[in] with The replacement string.
617 * @return #EINA_TRUE on success, #EINA_FALSE on failure.
618 *
619 * This function replaces the last occurrence of @p str in @p buf with
620 * @p with.
621 */
622EAPI Eina_Bool eina_strbuf_replace_last(Eina_Strbuf *buf, const char *str, const char *with) EINA_ARG_NONNULL(1, 2, 3);
623
624/**
612 * @brief Replaces all matching substrings with another string. 625 * @brief Replaces all matching substrings with another string.
613 * 626 *
614 * @param[in,out] buf The string buffer. 627 * @param[in,out] buf The string buffer.
diff --git a/src/lib/eina/eina_strbuf_common.c b/src/lib/eina/eina_strbuf_common.c
index b4d3427a39..9a1d27d8ce 100644
--- a/src/lib/eina/eina_strbuf_common.c
+++ b/src/lib/eina/eina_strbuf_common.c
@@ -980,6 +980,61 @@ eina_strbuf_replace(Eina_Strbuf *buf,
980 return EINA_TRUE; 980 return EINA_TRUE;
981} 981}
982 982
983EAPI Eina_Bool
984eina_strbuf_replace_last(Eina_Strbuf *buf,
985 const char *str,
986 const char *with)
987{
988 size_t len1, len2;
989 char *spos, *spos_next;
990 size_t pos;
991
992 EINA_SAFETY_ON_NULL_RETURN_VAL( str, EINA_FALSE);
993 EINA_SAFETY_ON_NULL_RETURN_VAL(with, EINA_FALSE);
994 EINA_MAGIC_CHECK_STRBUF(buf, 0);
995
996 spos = NULL;
997 spos_next = strstr(buf->buf, str);
998 while (spos_next && *spos_next)
999 {
1000 spos = spos_next;
1001 spos_next = strstr(spos + 1, str);
1002 }
1003
1004 if (!spos) return EINA_FALSE;
1005
1006 pos = spos - (const char *)buf->buf;
1007 len1 = strlen(str);
1008 len2 = strlen(with);
1009
1010 /* This is a read only buffer which need change to be made */
1011 if (buf->ro)
1012 {
1013 char *dest;
1014
1015 dest = malloc(buf->size);
1016 if (!dest) return 0;
1017 memcpy(dest, buf->buf, buf->len);
1018 buf->buf = dest;
1019 }
1020
1021 if (len1 != len2)
1022 {
1023 /* resize the buffer if necessary */
1024 if (EINA_UNLIKELY(!_eina_strbuf_common_grow(_STRBUF_CSIZE, buf,
1025 buf->len - len1 + len2)))
1026 return EINA_FALSE; /* move the existing text */
1027 memmove(((unsigned char *)(buf->buf)) + pos + len2,
1028 ((unsigned char *)(buf->buf)) + pos + len1,
1029 buf->len - pos - len1);
1030 }
1031 /* and now insert the given string */
1032 memcpy(((unsigned char *)(buf->buf)) + pos, with, len2);
1033 buf->len += len2 - len1;
1034 memset(((unsigned char *)(buf->buf)) + buf->len, 0, 1);
1035 return EINA_TRUE;
1036}
1037
983EAPI int 1038EAPI int
984eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with) 1039eina_strbuf_replace_all(Eina_Strbuf *buf, const char *str, const char *with)
985{ 1040{
diff --git a/src/tests/eina/eina_test_strbuf.c b/src/tests/eina/eina_test_strbuf.c
index 1d9f52c0d2..ac99992a31 100644
--- a/src/tests/eina/eina_test_strbuf.c
+++ b/src/tests/eina/eina_test_strbuf.c
@@ -303,8 +303,12 @@ EFL_START_TEST(eina_test_strbuf_replace)
303 fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf)); 303 fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
304 fail_if(strcmp(eina_strbuf_string_get(buf), "baaaab")); 304 fail_if(strcmp(eina_strbuf_string_get(buf), "baaaab"));
305 305
306 fail_if(eina_strbuf_replace_last(buf, "a", "x") == 0);
307 fail_if(strlen(eina_strbuf_string_get(buf)) != eina_strbuf_length_get(buf));
308 fail_if(strcmp(eina_strbuf_string_get(buf), "baaaxb"));
309
306 fail_if(eina_strbuf_replace_first(buf, "a", "b") == 0); 310 fail_if(eina_strbuf_replace_first(buf, "a", "b") == 0);
307 fail_if(strcmp(eina_strbuf_string_get(buf), "bbaaab")); 311 fail_if(strcmp(eina_strbuf_string_get(buf), "bbaaxb"));
308 312
309 eina_strbuf_free(buf); 313 eina_strbuf_free(buf);
310 314