{"thread":{"id":"40097","subject":"[PATCH v11 04/13] utf8: add function to align a string into given strbuf","startedAt":"2015-08-15T18:00:34Z","lastAt":"2015-08-17T13:08:08Z","messageCount":3,"participants":["Karthik Nayak","Eric Sunshine"],"isPatch":true,"patchVersion":11,"patchTotal":13},"messages":[{"id":"268128","messageId":"1439661643-16094-5-git-send-email-Karthik.188@gmail.com","threadId":"40097","inReplyTo":null,"subject":"[PATCH v11 04/13] utf8: add function to align a string into given strbuf","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-08-15T18:00:34Z","receivedAt":"2015-08-15T18:00:34Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"Add strbuf_utf8_align() which will align a given string into a strbuf\nas per given align_type and width. If the width is greater than the\nstring length then no alignment is performed.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>\nSigned-off-by: Karthik Nayak <karthik.188@gmail.com>\n---\n utf8.c | 21 +++++++++++++++++++++\n utf8.h | 15 +++++++++++++++\n 2 files changed, 36 insertions(+)\n\ndiff --git a/utf8.c b/utf8.c\nindex 28e6d76..0fb8e9d 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)\n \t*text += strlen(utf8_bom);\n \treturn 1;\n }\n+\n+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n+\t\t       const char *s)\n+{\n+\tint slen = strlen(s);\n+\tint display_len = utf8_strnwidth(s, slen, 0);\n+\tint utf8_compensation = slen - display_len;\n+\n+\tif (display_len >= width) {\n+\t\tstrbuf_addstr(buf, s);\n+\t\treturn;\n+\t}\n+\n+\tif (position == ALIGN_LEFT)\n+\t\tstrbuf_addf(buf, \"%-*s\", width + utf8_compensation, s);\n+\telse if (position == ALIGN_MIDDLE) {\n+\t\tint left = (width - display_len)/2;\n+\t\tstrbuf_addf(buf, \"%*s%-*s\", left, \"\", width - left + utf8_compensation, s);\n+\t} else if (position == ALIGN_RIGHT)\n+\t\tstrbuf_addf(buf, \"%*s\", width + utf8_compensation, s);\n+}\ndiff --git a/utf8.h b/utf8.h\nindex 5a9e94b..7930b44 100644\n--- a/utf8.h\n+++ b/utf8.h\n@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);\n  */\n int is_hfs_dotgit(const char *path);\n \n+typedef enum {\n+\tALIGN_LEFT,\n+\tALIGN_MIDDLE,\n+\tALIGN_RIGHT\n+} align_type;\n+\n+/*\n+ * Align the string given and store it into a strbuf as per the\n+ * 'position' and 'width'. If the given string length is larger than\n+ * 'width' than then the input string is not truncated and no\n+ * alignment is done.\n+ */\n+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n+\t\t       const char *s);\n+\n #endif\n-- \n2.5.0\n"},{"id":"268158","messageId":"CAPig+cSV_cfF8y0HVKSU1yBgOxJ0qrejc-7bQPvt-UkUNAtxKw@mail.gmail.com","threadId":"40097","inReplyTo":"1439661643-16094-5-git-send-email-Karthik.188@gmail.com","subject":"Re: [PATCH v11 04/13] utf8: add function to align a string into given strbuf","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-16T23:48:04Z","receivedAt":"2015-08-16T23:48:04Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n> Add strbuf_utf8_align() which will align a given string into a strbuf\n> as per given align_type and width. If the width is greater than the\n> string length then no alignment is performed.\n\nA couple minor comments below...\n\n> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n> ---\n> diff --git a/utf8.c b/utf8.c\n> index 28e6d76..0fb8e9d 100644\n> --- a/utf8.c\n> +++ b/utf8.c\n> @@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)\n>         *text += strlen(utf8_bom);\n>         return 1;\n>  }\n> +\n> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n> +                      const char *s)\n> +{\n> +       int slen = strlen(s);\n> +       int display_len = utf8_strnwidth(s, slen, 0);\n> +       int utf8_compensation = slen - display_len;\n\nBased upon the previous round review, I think you had intended to name\nthis merely 'compensation'.\n\n> +       if (display_len >= width) {\n> +               strbuf_addstr(buf, s);\n> +               return;\n> +       }\n> +\n> +       if (position == ALIGN_LEFT)\n> +               strbuf_addf(buf, \"%-*s\", width + utf8_compensation, s);\n> +       else if (position == ALIGN_MIDDLE) {\n> +               int left = (width - display_len)/2;\n\nStyle: spaces around '/'\n\n> +               strbuf_addf(buf, \"%*s%-*s\", left, \"\", width - left + utf8_compensation, s);\n> +       } else if (position == ALIGN_RIGHT)\n> +               strbuf_addf(buf, \"%*s\", width + utf8_compensation, s);\n> +}\n> diff --git a/utf8.h b/utf8.h\n> index 5a9e94b..7930b44 100644\n> --- a/utf8.h\n> +++ b/utf8.h\n> @@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);\n>   */\n>  int is_hfs_dotgit(const char *path);\n>\n> +typedef enum {\n> +       ALIGN_LEFT,\n> +       ALIGN_MIDDLE,\n> +       ALIGN_RIGHT\n> +} align_type;\n> +\n> +/*\n> + * Align the string given and store it into a strbuf as per the\n> + * 'position' and 'width'. If the given string length is larger than\n> + * 'width' than then the input string is not truncated and no\n> + * alignment is done.\n> + */\n> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n> +                      const char *s);\n> +\n>  #endif\n> --\n> 2.5.0\n"},{"id":"268182","messageId":"CAOLa=ZRR8Ytmtj5uo3bPBnkYcGr-vgu-n4hbb4CvKndwc3JeLA@mail.gmail.com","threadId":"40097","inReplyTo":"CAPig+cSV_cfF8y0HVKSU1yBgOxJ0qrejc-7bQPvt-UkUNAtxKw@mail.gmail.com","subject":"Re: [PATCH v11 04/13] utf8: add function to align a string into given strbuf","fromName":"Karthik Nayak","fromEmail":"karthik.188@gmail.com","sentAt":"2015-08-17T13:08:08Z","receivedAt":"2015-08-17T13:08:08Z","isPatch":true,"sender":{"key":"karthik.188@gmail.com","avatar":"https://avatars.githubusercontent.com/u/1786334?v=4"},"body":"On Mon, Aug 17, 2015 at 5:18 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:\n>> Add strbuf_utf8_align() which will align a given string into a strbuf\n>> as per given align_type and width. If the width is greater than the\n>> string length then no alignment is performed.\n>\n> A couple minor comments below...\n>\n>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>\n>> ---\n>> diff --git a/utf8.c b/utf8.c\n>> index 28e6d76..0fb8e9d 100644\n>> --- a/utf8.c\n>> +++ b/utf8.c\n>> @@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)\n>>         *text += strlen(utf8_bom);\n>>         return 1;\n>>  }\n>> +\n>> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n>> +                      const char *s)\n>> +{\n>> +       int slen = strlen(s);\n>> +       int display_len = utf8_strnwidth(s, slen, 0);\n>> +       int utf8_compensation = slen - display_len;\n>\n> Based upon the previous round review, I think you had intended to name\n> this merely 'compensation'.\n>\n\nIn the last patch it was suggested because I spelled 'compensation' wrong.\nI think the \"utf8_\" makes a good addition to the variable name.\n\n>> +       if (display_len >= width) {\n>> +               strbuf_addstr(buf, s);\n>> +               return;\n>> +       }\n>> +\n>> +       if (position == ALIGN_LEFT)\n>> +               strbuf_addf(buf, \"%-*s\", width + utf8_compensation, s);\n>> +       else if (position == ALIGN_MIDDLE) {\n>> +               int left = (width - display_len)/2;\n>\n> Style: spaces around '/'\n>\n\nwill add.\n\n>> +               strbuf_addf(buf, \"%*s%-*s\", left, \"\", width - left + utf8_compensation, s);\n>> +       } else if (position == ALIGN_RIGHT)\n>> +               strbuf_addf(buf, \"%*s\", width + utf8_compensation, s);\n>> +}\n>> diff --git a/utf8.h b/utf8.h\n>> index 5a9e94b..7930b44 100644\n>> --- a/utf8.h\n>> +++ b/utf8.h\n>> @@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);\n>>   */\n>>  int is_hfs_dotgit(const char *path);\n>>\n>> +typedef enum {\n>> +       ALIGN_LEFT,\n>> +       ALIGN_MIDDLE,\n>> +       ALIGN_RIGHT\n>> +} align_type;\n>> +\n>> +/*\n>> + * Align the string given and store it into a strbuf as per the\n>> + * 'position' and 'width'. If the given string length is larger than\n>> + * 'width' than then the input string is not truncated and no\n>> + * alignment is done.\n>> + */\n>> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,\n>> +                      const char *s);\n>> +\n>>  #endif\n>> --\n>> 2.5.0\n\nThanks for the review.\n\n-- \nRegards,\nKarthik Nayak\n"}]}