[mw-devel] [Git][arthur/mw][master] 2 commits: Tweak display_message()

Andrew Price welshbyte at sucs.org
Tue Jul 25 09:57:03 BST 2017


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
c3cc1458 by Andrew Price at 2017-07-24T23:42:14+01:00
Tweak display_message()

Mainly for readability but also removes a duplicate strlen() call on the
input text.

- - - - -
e80f14d6 by Andrew Price at 2017-07-24T23:53:59+01:00
Remove dead code relating to 'concealed' text

- - - - -


3 changed files:

- src/client/colour.c
- src/client/colour.h
- src/client/main.c


Changes:

=====================================
src/client/colour.c
=====================================
--- a/src/client/colour.c
+++ b/src/client/colour.c
@@ -23,13 +23,12 @@ static char *colour_set=NULL;
 extern struct user * const user;
 
 /* return colour code sequence */
-char *colour(char *text, int *concealed)
+char *colour(char *text)
 {
 	static char line[40];
 	int i;
 
 	line[0]=0;
-	*concealed = 0;
 
 	/* system colour chart */
 	if (strchr("0123456789",text[0]))
@@ -59,19 +58,6 @@ char *colour(char *text, int *concealed)
 		return(line);
 	}
 
-#ifdef CHILDISH
-	/* hidden text ? */
-	if (strchr("Hh", text[0]) &&
-	    strchr("Hh", text[1]) )
-	{
-		/* return 'normal' colour code, and set 'concealed' */
-		line[i++]='m';
-		line[i]=0;
-		*concealed = 1;
-		return(line);
-	}
-#endif
-
 	/* high intensity mode? */
 	if (isupper(text[0]))
 	{


=====================================
src/client/colour.h
=====================================
--- a/src/client/colour.h
+++ b/src/client/colour.h
@@ -7,7 +7,7 @@ void init_colour(void);
 void destroy_colours(void);
 void colour_load(char *file, int quiet);
 void colour_free(void);
-char *colour(char *text, int *concealed);
+char *colour(char *text);
 char *get_colour(void);
 
 #endif /* COLOUR_H */


=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -1063,9 +1063,6 @@ void format_message(const char *format, ...)
 void display_message(const char *text, int beeps, int newline)
 {
 	static int	count = 0;
-	int		len;
-	int		ptr;
-	int		concealed = 0;
 	char		line[MAXTEXTLENGTH];
 	int		i, j, colrstart;
 	int		hascolour;
@@ -1073,73 +1070,56 @@ void display_message(const char *text, int beeps, int newline)
 	char		*colr = NULL;
 	int		endline;
 	int		convert_warnings=0;
-	size_t	not_in_local=0;
+	const unsigned char *ptr = (const unsigned char *)text;
+	const unsigned char *end;
+	size_t not_in_local = 0;
+	size_t len;
 
-	if (text==NULL || strlen(text)==0)
-	{
-		sprintf(line,_("Error: Urk, no message to print.\n"));
-		write(1,line,strlen(line));
+	if (text == NULL || (len = strlen(text)) == 0) {
+		printf("%s", _("Error: Urk, no message to print.\n"));
 		return;
 	}
 	if (UseRL && disable_rl(1)) count = 0;
 
-	len=strlen(text);
-	ptr=0;
 	i=0;
 	hascolour=0;
 	colrstart=-1;
+	end = ptr + len;
 
-	while (len-ptr > 0)
-	{
-		if (text[ptr]==033)
-		{
+	while (ptr < end) {
+		if (*ptr == 033) {
 			char str[3];
 			ptr++;
-			if(len-ptr>0)
-			{
-				if( ((unsigned char)text[ptr] & 192) == 192 )
-				{
+			if (ptr < end) {
+				if ((*ptr & 192) == 192) {
 					ptr++;
-					str[0]='-';
-					while( ((unsigned char)text[ptr] & 192) == 128 && len-ptr > 0)
-					{
+					str[0] = '-';
+					while ((*ptr & 192) == 128 && ptr < end)
 						ptr++;
-					}
-				}
-				else
-				{
-					str[0]=text[ptr];
+				} else {
+					str[0] = *ptr;
 					ptr++;
 				}
 			}
-			if(len-ptr>0)
-			{
-				if(((unsigned char)text[ptr] & 192)==192 )
-				{
+			if (ptr < end) {
+				if((*ptr & 192) == 192) {
 					ptr++;
-					str[1]='-';
-					while( ((unsigned char)text[ptr] & 192) == 128 && len-ptr > 0)
-					{
+					str[1] = '-';
+					while((*ptr & 192) == 128 && ptr < end)
 						ptr++;
-					}
-				}
-				else
-				{
-					str[1]=text[ptr];
+				} else {
+					str[1] = *ptr;
 					ptr++;
 				}
 			}
 
 			/* escape sequence, skip next two chars */
 			if (s_colouroff(user))
-			{
 				goto eolprint;
-			}
-			hascolour++;
-
-			str[2]=0;
 
-			colr=colour(str, &concealed);
+			hascolour++;
+			str[2] = 0;
+			colr = colour(str);
 
 			if (colr!=NULL)
 			{
@@ -1148,52 +1128,26 @@ void display_message(const char *text, int beeps, int newline)
 				else
 					colrstart = i;
 
-				for (j=0;j<strlen(colr);j++)
-					line[i++]=colr[j];
-			}
-		}else
-		if (text[ptr]>=040 && text[ptr]<=0176)
-		{
-			if (concealed)
-			{
-				ptr++;
+				for (j = 0; j < strlen(colr); j++)
+					line[i++] = colr[j];
 			}
-			else
-			{
-				line[i++]=text[ptr++];
-				count++;
+		} else if (*ptr >= 040 && *ptr <= 0176) {
+			line[i++] = *ptr;
+			count++;
+			colrstart = -1;
+			ptr++;
+		} else if ((*ptr & 192) == 192) {
+			line[i++] = *ptr;
+			count++;
+			colrstart = -1;
+			ptr++;
+			// stops us randomly splitting over a unicode multibyte character
+			while ((*ptr & 192) == 128 && ptr < end) {
+				line[i++] = *ptr;
 				colrstart = -1;
-			}
-		}else
-		if ( (text[ptr] & 192) == 192 )
-		{
-			if (concealed)
-			{
 				ptr++;
 			}
-			else
-			{
-				line[i++]=text[ptr++];
-				count++;
-				colrstart = -1;
-			}
-			// stops us randomly spliting over a unicode multibyte character
-			while( ((unsigned char)text[ptr] & 192) == 128 && len-ptr > 0 )
-			{
-				if (concealed)
-				{
-					ptr++;
-				}
-				else
-				{
-					line[i++]=text[ptr++];
-					colrstart = -1;
-				}
-			}
-
-		}
-		else
-		{
+		} else {
 			ptr++;
 		}
 		if (i >= (MAXTEXTLENGTH-20))
@@ -1204,9 +1158,9 @@ void display_message(const char *text, int beeps, int newline)
 		}
 eolprint:
 		if (s_nolinewrap(user))
-			endline = (ptr >= len);
+			endline = (ptr >= end);
 		else
-			endline = ((count >= screen_width) || (ptr >= len));
+			endline = ((count >= screen_width) || (ptr >= end));
 
 		if (endline)
 		{
@@ -1217,7 +1171,7 @@ eolprint:
 				line[i++]='m';
 			}
 
-			if (newline || (ptr<len))
+			if (newline || (ptr < end))
 			{
 				line[i++]='\n';
 				count=0;
@@ -1226,7 +1180,7 @@ eolprint:
 			line[i]='\0';
 			printline_in_local(line, &convert_warnings, &not_in_local);
 
-			if (ptr>=len)
+			if (ptr >= end)
 			{
 				i=0;
 				if (newline) count=0;
@@ -1240,8 +1194,8 @@ eolprint:
 				    hascolour &&
 				    colr!=NULL)
 				{
-				    for (j=0;j<strlen(colr);j++)
-					    line[i++]=colr[j];
+				    for (j=0; j < strlen(colr); j++)
+					    line[i++] = colr[j];
 				}
 			}
 		}



View it on GitLab: https://projects.sucs.org/arthur/mw/compare/02fc046aad4c8f2eeed94e766ea3cfd4a092a628...e80f14d6e8245e8a7a3572b59afa160615054c9e

---
View it on GitLab: https://projects.sucs.org/arthur/mw/compare/02fc046aad4c8f2eeed94e766ea3cfd4a092a628...e80f14d6e8245e8a7a3572b59afa160615054c9e
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20170725/4c47affc/attachment-0001.html>


More information about the mw-devel mailing list