builtin/merge.c: drop a parameter that is never used
[gitweb.git] / utf8.c
diff --git a/utf8.c b/utf8.c
index e7ba33c2355d1f4a3ab7b679a3e98dec25e0707d..0d20e0acb2b6fb2dd1d63abed676a036b2b7ec2f 100644 (file)
--- a/utf8.c
+++ b/utf8.c
@@ -9,7 +9,7 @@ struct interval {
   int last;
 };
 
-static size_t display_mode_esc_sequence_len(const char *s)
+size_t display_mode_esc_sequence_len(const char *s)
 {
        const char *p = s;
        if (*p++ != '\033')
@@ -421,6 +421,52 @@ void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
        free(tmp);
 }
 
+void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
+                        const char *subst)
+{
+       struct strbuf sb_dst = STRBUF_INIT;
+       char *src = sb_src->buf;
+       char *end = src + sb_src->len;
+       char *dst;
+       int w = 0, subst_len = 0;
+
+       if (subst)
+               subst_len = strlen(subst);
+       strbuf_grow(&sb_dst, sb_src->len + subst_len);
+       dst = sb_dst.buf;
+
+       while (src < end) {
+               char *old;
+               size_t n;
+
+               while ((n = display_mode_esc_sequence_len(src))) {
+                       memcpy(dst, src, n);
+                       src += n;
+                       dst += n;
+               }
+
+               old = src;
+               n = utf8_width((const char**)&src, NULL);
+               if (!src)       /* broken utf-8, do nothing */
+                       return;
+               if (n && w >= pos && w < pos + width) {
+                       if (subst) {
+                               memcpy(dst, subst, subst_len);
+                               dst += subst_len;
+                               subst = NULL;
+                       }
+                       w += n;
+                       continue;
+               }
+               memcpy(dst, old, src - old);
+               dst += src - old;
+               w += n;
+       }
+       strbuf_setlen(&sb_dst, dst - sb_dst.buf);
+       strbuf_swap(sb_src, &sb_dst);
+       strbuf_release(&sb_dst);
+}
+
 int is_encoding_utf8(const char *name)
 {
        if (!name)
@@ -468,7 +514,7 @@ int utf8_fprintf(FILE *stream, const char *format, ...)
 #else
        typedef char * iconv_ibp;
 #endif
-char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv)
+char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, int *outsz_p)
 {
        size_t outsz, outalloc;
        char *out, *outpos;
@@ -502,13 +548,17 @@ char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv)
                }
                else {
                        *outpos = '\0';
+                       if (outsz_p)
+                               *outsz_p = outpos - out;
                        break;
                }
        }
        return out;
 }
 
-char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
+char *reencode_string_len(const char *in, int insz,
+                         const char *out_encoding, const char *in_encoding,
+                         int *outsz)
 {
        iconv_t conv;
        char *out;
@@ -534,7 +584,7 @@ char *reencode_string(const char *in, const char *out_encoding, const char *in_e
                        return NULL;
        }
 
-       out = reencode_string_iconv(in, strlen(in), conv);
+       out = reencode_string_iconv(in, insz, conv, outsz);
        iconv_close(conv);
        return out;
 }