Allow %c format to output null characters (GitHub issue #209)

Currently $display et al. output nothing when the expression
corresponding to a %c format specification has the value 0. As
Verilog provides no other way to write raw bytes to a file, we
should allow 0 values to be written. Other simulators allow this.

(cherry picked from commit b066a5815e)
This commit is contained in:
Martin Whitaker 2018-09-29 21:22:17 +01:00
parent d5be428e45
commit 5474f9d5ac
1 changed files with 14 additions and 14 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 1999-2017 Stephen Williams (steve@icarus.com) * Copyright (c) 1999-2018 Stephen Williams (steve@icarus.com)
* *
* This source code is free software; you can redistribute it * This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU * and/or modify it in source code form under the terms of the GNU
@ -414,23 +414,23 @@ static unsigned int get_format_char(char **rtn, int ljust, int plus,
} else { } else {
char ch = value.value.str[strlen(value.value.str)-1]; char ch = value.value.str[strlen(value.value.str)-1];
/* If the default buffer is too small, make it big enough. */
size = width + 1;
if (size > ini_size) result = realloc(result, size*sizeof(char));
/* If the width is less than one then use a width of one. */ /* If the width is less than one then use a width of one. */
if (width < 1) width = 1; if (width < 1) width = 1;
size = width + 1;
assert(size <= ini_size);
if (ljust == 0) { if (ljust == 0) {
if (width > 1) { if (width > 1) {
char *cp = malloc((width+1)*sizeof(char)); memset(result, (ld_zero == 1 ? '0': ' '), width-1);
memset(cp, (ld_zero == 1 ? '0': ' '), width-1); }
cp[width-1] = ch; result[width-1] = ch;
cp[width] = '\0'; } else {
sprintf(result, "%*s", width, cp); result[0] = ch;
free(cp); if (width > 1) {
} else sprintf(result, "%c", ch); memset(result+1, ' ', width-1);
} else sprintf(result, "%-*c", width, ch); }
size = strlen(result) + 1; }
result[width] = '\0';
} }
} }
break; break;