Color codes counting as characters in strings

Posted by Lmclarke on Sun 17 Apr 2016 02:03 AM — 8 posts, 33,245 views.

#0
What I'm trying to accomplish is this:

I have a character limit on objects' short descriptions. It works fine -- restricts the short desc to 59 characters.

However, it's counting color codes. So while it might be 59 visible characters ("a pair of blue jeans with some noticeable wear on the knees"), adding color to it ("a pair of #cblue jeans#n with some noticeable wear on the knees") counts #c and #n as 4 extra characters, thus disallowing it.

How does one make it so color codes aren't counted? Does anyone have any experience with this?
Amended on Sun 17 Apr 2016 02:18 AM by Lmclarke
Australia Forum Administrator #1
In your code where you test for the maximum length, make a special version of "strlen" which skips over "#x" sequences.
#2
What I have:


	if (!str_cmp (arg2, "room") || !str_cmp (arg2, "short"))
	{
		if (strlen (argument) > 59)
		{
			send_to_char ("Short descriptions should be 59 characters.\n\r", ch);
			return;
		}
		endchar[0] = (argument[strlen (argument) - 1]);
		endchar[1] = '\0';
		free_string (obj->short_descr);
		obj->short_descr = str_dup (argument);
		if (obj->questmaker != NULL)
			free_string (obj->questmaker);
		obj->questmaker = str_dup (ch->name);
		if ( obj->item_type == ITEM_VEHICLE ) {
			
		}
		send_to_char ("#eOkay. #wShort description#e set.#n\n\r", ch);
		return;
	}


I know this is probably a very simple thing to do, but my code knowledge is very... informal. I've googled "strlen" and looked at some examples, but I can't seem to get the syntax right.

I would add the strlen check in this line, right?

send_to_char ("Short descriptions should be 59 characters.\n\r", ch);
USA #3
It needs to be done where
if (strlen (argument) > 59) 
is calculated, probably the block of code directly before what you posted.
Australia Forum Administrator #4
Example code:


#include <stdio.h>
#include <ctype.h>

size_t strlen_exclude_colors(const char * str)
{
  const char *s;
  size_t count = 0;
  for (s = str; *s; ++s)
    {
    if (s [0] == '#' && isalpha (s [1]))
      ++s;  // skip the letter
    else
      count++;  // otherwise count the character
    }
  return count;
}

int main ()
{
  const char test [] = "#cblue #nwith";   // 13 chars
  printf ("Calculated length = %i\n", (int) strlen_exclude_colors (test));
  return 0;
}


Output:


Calculated length = 9


Once you add the function "strlen_exclude_colors" to your code, change the line:


if (strlen (argument) > 59) 


to:



if (strlen_exclude_colors (argument) > 59) 
Amended on Sun 17 Apr 2016 07:45 PM by Nick Gammon
#5
It's working! Tested in-game, functions just as intended (Thank you!).

I'm a little bit concerned about this:


sys.c: In function `do_restring':
sys.c:1749: warning: implicit declaration of function `strlen_exclude_colors'


It compiles anyway, but the warning makes me uneasy.
Amended on Sun 17 Apr 2016 09:09 PM by Lmclarke
Australia Forum Administrator #6
Put the new function physically before where you call it, in the source file, and the warning will go away.
#7
Thank you very much, Nick. You're the best.