Pages: [1]   Go Down
Author Topic: Stream::findUntil shall use const char* for argument 2 (terminator)  (Read 703 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
  ...
Logged

Pages: [1]   Go Up
Jump to: