Go Down

Topic: Stream::findUntil shall use const char* for argument 2 (terminator) (Read 778 times) previous topic - next topic

astir13

As the 2nd argument of function Stream::findUntil(char *target, char *terminator) is not being modified by the function's code, it is advised to use const here.

problem:
if findUntil would modify the terminator string, the compiler would not complain and the programmer would be very surprised.

improvment:
using const in such situations allows other classes to properly define this parameter as const where indicated, telling the compiler to complain if the terminator string is modified by a function (by mistake).

Proposed code the the findUntil() function header and implementation is, thus:

bool  Stream::findUntil(char *target, const char *terminator)
{
  return findUntil(target, strlen(target), terminator, strlen(terminator));
}

In the second function declaration for findUntil(), following changes would be adviced:

bool Stream::findUntil(char *target, size_t targetLen, const char *terminator, const size_t termLen)
{
  size_t index = 0;  // maximum target string length is 64k bytes!
  size_t termIndex = 0;
  int c;
  ...

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy