S
Shao Miller
It's a security feature. If you do not desire any security at all, it'sLew said:I haven't followed this thread, and my suggestions may be redundant...
[snip]If you have any concerns about subtracting pointers and 'size_t', how
about this? (Some parentheses are redundant but included as visual aids):
/**
* Trim whitespace on the left and right of a string
*/
#include <stdlib.h>
#include <ctype.h>
/* Return a pointer to the terminator for the trimmed string */
static char *trim_unsafe(char *string) {
char *i = string;
It looks like you're making two passes of the right side space, once
when copying, and again when trimming.
easy enough to replace that function with:
/* Return a pointer to the terminator for the trimmed string */
static char *trim_unsafe(char *string) {
char *i = string;
char *end = string;
/* Trim left */
while (isspace(*string = *i))
++i;
/* Copy remaining string */
while (*string = *i) {
if (!isspace(*string))
end = string + 1;
++string;
++i;
}
/* Trim right */
*end = 0;
/* Return a pointer to the terminator */
return end;
}
Like what, the erasure? Ok. At the cost of an additional pointer, the
code above shouldn't bother with erasure.
If you wish to have a single read pass and a single write pass, the
single read pass must walk the entirety of the string. How much extra
work is it to have the write pass copy any spaces on the right? How
much extra work is involved in calling 'memmove' after you've performed
your own read pass to determine end-points?
Or rather than counting, keep pointers to those interesting boundsAgreed.
ISTM that you are correct, and a good programmer could reduce this whole
process to /one/ read pass through the input string, followed by (assuming
the "unsafe" method of reusing the input string as the output) a judicious
placement of a '\0', and the return of an offset-adjusted pointer.
Here's how I would do it...
In a single loop from the start of the string to the end,
count the number of characters in the string,
count the number of leading whitespace characters, and
count the number of trailing whitespace characters
(amounting to the same thing).
But is returning a pointer to within the string really "trimming" theNow, place a '\0' at string[length_of_string - trailing_whitespace_count]
to truncate the string and discard the trailing whitespace characters.
Finally, return to the caller the value (string + leading_whitespace_count),
to return a pointer to the first non-whitespace of the string.
string? If other parts of a program have copies of the original
pointer, they will still point to a string which has not been left-trimmed.
By returning a pointer to the terminator, one can make it the caller's
problem if they want to count how many chars remain; with associated
'ptrdiff_t' value limitations.
Right. Maybe someone doesn't want to destructively trim, but wants:Granted, this approach modifies the input string, and cannot be used on a
const string type (i.e. " something ").
HTH
int trim_bounds(const char *string, const char **left, const char **right);