VK3KYY wrote: ↑Sun Sep 20, 2020 11:46 pm
This is not a mistake.
Right, that's basically all I wanted to confirm.
(I got thinking about the different ways the code could backfire, ie. calling the function twice with the same inBuf ptr, etc, but I didn't check all the calls of the function in the code.)
The codeplug format does not use the standard string terminator of 0, it uses 0xFF
Got it, that's why you use the conversion in the first place
The function could be rewritten to not change the input buffer (see below)
However, I don't know whether the resultant assembler / binary code would be larger and slower.
I doubt it would make any meaningful difference for size or performance.
I see the function has a return statement, which is unnecessary in a function that returns void, but I'm sure the compiler is smart enough to ignore it.
Return is implied at the end of the function block anyway (call stack is unwound, execution is returned to the caller). It's valid to write the return explicitly at the end or leave it out. It has no effect in a void returning function. Return statement can be of course useful also in void function() to get out in the middle of function, for example in a loop, when a condition is met.
With firmware, often come optimisations are made, which you would not bother with on a PC etc.
Yes, naturally
The first snippet in you your reply is close to what I would have expected to see when my suspicion of a mistake crept. One could use the return from mid loop, mentioned above, here too:
Code: Select all
void codeplugUtilConvertBufToString(const char *inBuf, char *outBuf, int len)
{
/* warning: outBuf is written even if len == 0 or negative */
int i;
for(i = 0; i < len; i++)
{
if (inBuf[i] == 0xff)
{
outBuf[i] = 0;
return; <--- Here
}
outBuf[i] = inBuf[i];
}
outBuf[i] = 0;
return;
}
(But leaving the example aside, you would really rather just
instead, in this particular case.)
For the second snippet of your reply, with my simple mind, I find it hard to reason about. Personally, I'd prefer the first one, for what that's worth.
Another side note for the second one (which you probably are well aware of), duplicating the buffer pointer is not required. You would have to use **buf to be able to modify the callers pointer. The pointer is copied when used as a function parameter and modifying it inside the function has only local effect. Just wanted to mention this, because it's often overlooked. You could probably formulate it like this to make it minimalistic:
Code: Select all
void CodePlugBufToString(char *plugBuf, int maxLen)
{
/* same warning applies as for the previous one */
int i = 0;
while( i < maxLen && plugBuf[i] != 0xff ) i++;
plugBuf[i] = 0;
}
Anyway, I think I have wasted enough of your time.
I fully understand that not everything worth doing is worth doing perfectly. One does not have infinitely time. I am grateful to the past and current developers for the work on the project. I love to be able to see what's actually going on instead of just staring at the black box.
Cheers,
Ville