]> granicus.if.org Git - neomutt/commitdiff
add mutt_str_inline_replace()
authorRichard Russon <rich@flatcap.org>
Fri, 24 Aug 2018 14:38:55 +0000 (15:38 +0100)
committerRichard Russon <rich@flatcap.org>
Sat, 25 Aug 2018 10:51:58 +0000 (11:51 +0100)
mutt/string.c
mutt/string2.h

index 9b872f7cb5dba2c180c258e484225ab22cb34df8..d68ad435e69269cf3070ab974ce4174ecca093a4 100644 (file)
@@ -959,3 +959,27 @@ const char *mutt_str_getenv(const char *name)
 
   return NULL;
 }
+
+/**
+ * mutt_str_inline_replace - Replace the beginning of a string
+ * @param buf    Buffer to modify
+ * @param buflen Length of buffer
+ * @param xlen   Length of string to overwrite
+ * @param rstr   Replacement string
+ * @retval true Success
+ *
+ * String (`XX<OOOOOO>......`, 16, 2, `RRRR`) becomes `RRRR<OOOOOO>....`
+ */
+bool mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char *rstr)
+{
+  if (!buf || !rstr || (xlen >= buflen))
+    return false;
+
+  size_t slen = mutt_str_strlen(buf + xlen);
+  size_t rlen = mutt_str_strlen(rstr);
+
+  memmove(buf + rlen, buf + xlen, slen + 1);
+  memmove(buf, rstr, rlen);
+
+  return true;
+}
index cd931a7315a07213b8aa842d14603093b89c8bb2..ce67d6ed1855678db9c2fa6818888cf82260bfb0 100644 (file)
@@ -71,6 +71,7 @@ int         mutt_str_atoul(const char *str, unsigned long *dst);
 void        mutt_str_dequote_comment(char *s);
 const char *mutt_str_find_word(const char *src);
 const char *mutt_str_getenv(const char *name);
+bool        mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char *rstr);
 bool        mutt_str_is_ascii(const char *p, size_t len);
 bool        mutt_str_is_email_wsp(char c);
 size_t      mutt_str_lws_len(const char *s, size_t n);