[RFC PATCH 3/4] ui-blame: create needed html_ntxt_noellipsis function

Jeff Smith whydoubt at gmail.com
Thu Jun 8 04:18:09 CEST 2017


For implementing a ui-blame page, there is need for a function that
outputs a selection from a block of text, transformed for HTML output,
but with no further modifications or additions.

Signed-off-by: Jeff Smith <whydoubt at gmail.com>
---
 html.c | 37 ++++++++++++++++---------------------
 html.h |  1 +
 2 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/html.c b/html.c
index e7e6e07..c7d1c64 100644
--- a/html.c
+++ b/html.c
@@ -122,10 +122,10 @@ void html_vtxtf(const char *format, va_list ap)
 	strbuf_release(&buf);
 }
 
-void html_txt(const char *txt)
+static int _html_txt(int len, const char *txt)
 {
 	const char *t = txt;
-	while (t && *t) {
+	while (t && *t && len--) {
 		int c = *t;
 		if (c == '<' || c == '>' || c == '&') {
 			html_raw(txt, t - txt);
@@ -140,32 +140,27 @@ void html_txt(const char *txt)
 		t++;
 	}
 	if (t != txt)
-		html(txt);
+		html_raw(txt, t - txt);
+	return len;
+}
+
+void html_txt(const char *txt)
+{
+	if (txt)
+		_html_txt(strlen(txt), txt);
 }
 
 void html_ntxt(int len, const char *txt)
 {
-	const char *t = txt;
-	while (t && *t && len--) {
-		int c = *t;
-		if (c == '<' || c == '>' || c == '&') {
-			html_raw(txt, t - txt);
-			if (c == '>')
-				html(">");
-			else if (c == '<')
-				html("<");
-			else if (c == '&')
-				html("&");
-			txt = t + 1;
-		}
-		t++;
-	}
-	if (t != txt)
-		html_raw(txt, t - txt);
-	if (len < 0)
+	if (_html_txt(len, txt) < 0)
 		html("...");
 }
 
+void html_ntxt_noellipsis(int len, const char *txt)
+{
+	_html_txt(len, txt);
+}
+
 void html_attrf(const char *fmt, ...)
 {
 	va_list ap;
diff --git a/html.h b/html.h
index 1b24e55..e1b85b3 100644
--- a/html.h
+++ b/html.h
@@ -20,6 +20,7 @@ extern void html_attrf(const char *format,...);
 
 extern void html_txt(const char *txt);
 extern void html_ntxt(int len, const char *txt);
+extern void html_ntxt_noellipsis(int len, const char *txt);
 extern void html_attr(const char *txt);
 extern void html_url_path(const char *txt);
 extern void html_url_arg(const char *txt);
-- 
2.9.3



More information about the CGit mailing list