git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v11 04/13] utf8: add function to align a string into given strbuf
@ 2015-08-15 18:00 Karthik Nayak
  2015-08-16 23:48 ` Eric Sunshine
  0 siblings, 1 reply; 3+ messages in thread
From: Karthik Nayak @ 2015-08-15 18:00 UTC (permalink / raw)
  To: git; +Cc: christian.couder, Matthieu.Moy, gitster, Karthik Nayak,
	Karthik Nayak

Add strbuf_utf8_align() which will align a given string into a strbuf
as per given align_type and width. If the width is greater than the
string length then no alignment is performed.

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Mentored-by: Christian Couder <christian.couder@gmail.com>
Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr>
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 utf8.c | 21 +++++++++++++++++++++
 utf8.h | 15 +++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/utf8.c b/utf8.c
index 28e6d76..0fb8e9d 100644
--- a/utf8.c
+++ b/utf8.c
@@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)
 	*text += strlen(utf8_bom);
 	return 1;
 }
+
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s)
+{
+	int slen = strlen(s);
+	int display_len = utf8_strnwidth(s, slen, 0);
+	int utf8_compensation = slen - display_len;
+
+	if (display_len >= width) {
+		strbuf_addstr(buf, s);
+		return;
+	}
+
+	if (position == ALIGN_LEFT)
+		strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
+	else if (position == ALIGN_MIDDLE) {
+		int left = (width - display_len)/2;
+		strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
+	} else if (position == ALIGN_RIGHT)
+		strbuf_addf(buf, "%*s", width + utf8_compensation, s);
+}
diff --git a/utf8.h b/utf8.h
index 5a9e94b..7930b44 100644
--- a/utf8.h
+++ b/utf8.h
@@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
  */
 int is_hfs_dotgit(const char *path);
 
+typedef enum {
+	ALIGN_LEFT,
+	ALIGN_MIDDLE,
+	ALIGN_RIGHT
+} align_type;
+
+/*
+ * Align the string given and store it into a strbuf as per the
+ * 'position' and 'width'. If the given string length is larger than
+ * 'width' than then the input string is not truncated and no
+ * alignment is done.
+ */
+void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
+		       const char *s);
+
 #endif
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v11 04/13] utf8: add function to align a string into given strbuf
  2015-08-15 18:00 [PATCH v11 04/13] utf8: add function to align a string into given strbuf Karthik Nayak
@ 2015-08-16 23:48 ` Eric Sunshine
  2015-08-17 13:08   ` Karthik Nayak
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Sunshine @ 2015-08-16 23:48 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
> Add strbuf_utf8_align() which will align a given string into a strbuf
> as per given align_type and width. If the width is greater than the
> string length then no alignment is performed.

A couple minor comments below...

> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---
> diff --git a/utf8.c b/utf8.c
> index 28e6d76..0fb8e9d 100644
> --- a/utf8.c
> +++ b/utf8.c
> @@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)
>         *text += strlen(utf8_bom);
>         return 1;
>  }
> +
> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
> +                      const char *s)
> +{
> +       int slen = strlen(s);
> +       int display_len = utf8_strnwidth(s, slen, 0);
> +       int utf8_compensation = slen - display_len;

Based upon the previous round review, I think you had intended to name
this merely 'compensation'.

> +       if (display_len >= width) {
> +               strbuf_addstr(buf, s);
> +               return;
> +       }
> +
> +       if (position == ALIGN_LEFT)
> +               strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
> +       else if (position == ALIGN_MIDDLE) {
> +               int left = (width - display_len)/2;

Style: spaces around '/'

> +               strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
> +       } else if (position == ALIGN_RIGHT)
> +               strbuf_addf(buf, "%*s", width + utf8_compensation, s);
> +}
> diff --git a/utf8.h b/utf8.h
> index 5a9e94b..7930b44 100644
> --- a/utf8.h
> +++ b/utf8.h
> @@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
>   */
>  int is_hfs_dotgit(const char *path);
>
> +typedef enum {
> +       ALIGN_LEFT,
> +       ALIGN_MIDDLE,
> +       ALIGN_RIGHT
> +} align_type;
> +
> +/*
> + * Align the string given and store it into a strbuf as per the
> + * 'position' and 'width'. If the given string length is larger than
> + * 'width' than then the input string is not truncated and no
> + * alignment is done.
> + */
> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
> +                      const char *s);
> +
>  #endif
> --
> 2.5.0

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v11 04/13] utf8: add function to align a string into given strbuf
  2015-08-16 23:48 ` Eric Sunshine
@ 2015-08-17 13:08   ` Karthik Nayak
  0 siblings, 0 replies; 3+ messages in thread
From: Karthik Nayak @ 2015-08-17 13:08 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Git List, Christian Couder, Matthieu Moy, Junio C Hamano

On Mon, Aug 17, 2015 at 5:18 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sat, Aug 15, 2015 at 2:00 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
>> Add strbuf_utf8_align() which will align a given string into a strbuf
>> as per given align_type and width. If the width is greater than the
>> string length then no alignment is performed.
>
> A couple minor comments below...
>
>> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
>> ---
>> diff --git a/utf8.c b/utf8.c
>> index 28e6d76..0fb8e9d 100644
>> --- a/utf8.c
>> +++ b/utf8.c
>> @@ -644,3 +644,24 @@ int skip_utf8_bom(char **text, size_t len)
>>         *text += strlen(utf8_bom);
>>         return 1;
>>  }
>> +
>> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
>> +                      const char *s)
>> +{
>> +       int slen = strlen(s);
>> +       int display_len = utf8_strnwidth(s, slen, 0);
>> +       int utf8_compensation = slen - display_len;
>
> Based upon the previous round review, I think you had intended to name
> this merely 'compensation'.
>

In the last patch it was suggested because I spelled 'compensation' wrong.
I think the "utf8_" makes a good addition to the variable name.

>> +       if (display_len >= width) {
>> +               strbuf_addstr(buf, s);
>> +               return;
>> +       }
>> +
>> +       if (position == ALIGN_LEFT)
>> +               strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
>> +       else if (position == ALIGN_MIDDLE) {
>> +               int left = (width - display_len)/2;
>
> Style: spaces around '/'
>

will add.

>> +               strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
>> +       } else if (position == ALIGN_RIGHT)
>> +               strbuf_addf(buf, "%*s", width + utf8_compensation, s);
>> +}
>> diff --git a/utf8.h b/utf8.h
>> index 5a9e94b..7930b44 100644
>> --- a/utf8.h
>> +++ b/utf8.h
>> @@ -55,4 +55,19 @@ int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding);
>>   */
>>  int is_hfs_dotgit(const char *path);
>>
>> +typedef enum {
>> +       ALIGN_LEFT,
>> +       ALIGN_MIDDLE,
>> +       ALIGN_RIGHT
>> +} align_type;
>> +
>> +/*
>> + * Align the string given and store it into a strbuf as per the
>> + * 'position' and 'width'. If the given string length is larger than
>> + * 'width' than then the input string is not truncated and no
>> + * alignment is done.
>> + */
>> +void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
>> +                      const char *s);
>> +
>>  #endif
>> --
>> 2.5.0

Thanks for the review.

-- 
Regards,
Karthik Nayak

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-08-17 13:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-15 18:00 [PATCH v11 04/13] utf8: add function to align a string into given strbuf Karthik Nayak
2015-08-16 23:48 ` Eric Sunshine
2015-08-17 13:08   ` Karthik Nayak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).