GOUJON Alexandre ale.goujon@gmail.com wrote:
On 11/01/2010 03:12 PM, Vitaliy Margolen wrote:
- static const char escaped_space[] = "@space@";
- DWORD len_space = strlen(escaped_space);
The better way to calculate size of a static string, which is a compile time calculation. strlen() call is a runtime.
The better way is ... ? In compare_line(), sizeof(space_cmd) is used but I guess sizeof(space_cmd/space_cmd[0]) is more portable, isn't it ? I thought strlen() was optimized in that case because used with a const string.
Only if you expect that a compiler knows what strlen() is. Better to not rely on internal compiler knowledge.
+static char* replace_escaped_spaces(const char *data, DWORD size, DWORD *new_size)
- char *a, *b, *new_data;
- a = b = (char*)data;
a, b should be "const char*" as well.
Unfortunately, I can't do that : strncpy and HeapFree complains about const strings.
In general never cast away const. I don't see where you are using 'a' or 'b' in strncpy() or HeapFree() as a target pointer that prevents making them const, only 'new_data' needs to be non-const.
a = b += len_space;
This is absolutely unreadable.
Also if you could add some spaces around '+'/'-'/'=' in the cases below it would improve readability as well:
- *new_size=0;
strncpy(new_data+*new_size, a, b-a+1);
*new_size += b-a +1;
new_data[*new_size-1] = ' ';
- strncpy(new_data+*new_size, a, strlen(a)+1);
etc.