Trim function in C
$begingroup$
I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?
void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}
c strings
$endgroup$
add a comment |
$begingroup$
I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?
void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}
c strings
$endgroup$
4
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
2
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05
add a comment |
$begingroup$
I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?
void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}
c strings
$endgroup$
I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it?
void trim(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace(input[i])) {
result[j++] = input[i];
}
}
}
c strings
c strings
asked Jan 25 '13 at 15:01
jay rulherjay rulher
21112
21112
4
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
2
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05
add a comment |
4
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
2
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05
4
4
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
2
2
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces
.
There's a really subtle bug here:
... isspace(input[i]) ...
isspace
takes the value of an unsigned char or EOF
. Passing it a char
, which is usually signed, will produce undefined behavior. Instead, say:
... isspace((unsigned char) input[i]) ...
Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).
Fixing these bugs gives us:
void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '';
}
@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1
. But the caller might be lazy and just say char result[100]
. Adding an output buffer size parameter will likely guard against such a mistake:
void remove_spaces(const char *input, char *output, size_t output_size);
See if you can implement this. Some things to bear in mind:
Don't forget about the NUL terminator when checking the output buffer size.
Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.
If you use
int
fori
andj
andsize_t
foroutput_size
, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typinggcc -Wall -W
.
$endgroup$
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.
$endgroup$
– Deduplicator
4 hours ago
add a comment |
$begingroup$
We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while
loops are enough. You'll notice that the right walk count is less than the left walk count.
Right-trim code:
#include <stdio.h>
#include <ctype.h>
void trim_both(char *, char *);
int main (void) {
char title[100] = " My long string ";
char title_t[100] = "";
(void) printf("String before left trim is:[%s]n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]n", title_t);
}
// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;
// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}
// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '';
}
}
$endgroup$
add a comment |
$begingroup$
Easiest way (removes only spaces):
Trim.Start:
- Compare chars until they equal
' '
(space or other chars liken
ort
) at the string start and increment temp (i
) variable. - Move pointer about
i
(str+=i
). Now string starts from char which is not a space char (or any other white char).
Trim.End:
- Do the same what for Trim.Start but from the end of the string.
- Set last char (last space) as
.
The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);
char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;
// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;
if (start < length) // Required for empty (ex. " ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}
return *pointerToString;
}
Usage:
char str1 = " test1 ";
char * p1 = str1;
Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);
char str2 = " test2";
char * p2 = str2;
Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);
char str3 = "test3 ";
char * p3 = str3;
Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);
char str4 = " ";
char * p4 = str4;
Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);
char str5 = "";
char * p5 = str5;
Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);
Result:
1. before trim: [ test1 ]
1. after trim [test1]
2. before trim: [ test2]
2. after trim [test2]
3. before trim: [test3 ]
3. after trim [test3]
4. before trim: [ ]
4. after trim
5. before trim:
5. after trim
$endgroup$
add a comment |
protected by Jamal♦ 19 mins ago
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces
.
There's a really subtle bug here:
... isspace(input[i]) ...
isspace
takes the value of an unsigned char or EOF
. Passing it a char
, which is usually signed, will produce undefined behavior. Instead, say:
... isspace((unsigned char) input[i]) ...
Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).
Fixing these bugs gives us:
void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '';
}
@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1
. But the caller might be lazy and just say char result[100]
. Adding an output buffer size parameter will likely guard against such a mistake:
void remove_spaces(const char *input, char *output, size_t output_size);
See if you can implement this. Some things to bear in mind:
Don't forget about the NUL terminator when checking the output buffer size.
Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.
If you use
int
fori
andj
andsize_t
foroutput_size
, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typinggcc -Wall -W
.
$endgroup$
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.
$endgroup$
– Deduplicator
4 hours ago
add a comment |
$begingroup$
As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces
.
There's a really subtle bug here:
... isspace(input[i]) ...
isspace
takes the value of an unsigned char or EOF
. Passing it a char
, which is usually signed, will produce undefined behavior. Instead, say:
... isspace((unsigned char) input[i]) ...
Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).
Fixing these bugs gives us:
void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '';
}
@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1
. But the caller might be lazy and just say char result[100]
. Adding an output buffer size parameter will likely guard against such a mistake:
void remove_spaces(const char *input, char *output, size_t output_size);
See if you can implement this. Some things to bear in mind:
Don't forget about the NUL terminator when checking the output buffer size.
Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.
If you use
int
fori
andj
andsize_t
foroutput_size
, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typinggcc -Wall -W
.
$endgroup$
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.
$endgroup$
– Deduplicator
4 hours ago
add a comment |
$begingroup$
As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces
.
There's a really subtle bug here:
... isspace(input[i]) ...
isspace
takes the value of an unsigned char or EOF
. Passing it a char
, which is usually signed, will produce undefined behavior. Instead, say:
... isspace((unsigned char) input[i]) ...
Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).
Fixing these bugs gives us:
void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '';
}
@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1
. But the caller might be lazy and just say char result[100]
. Adding an output buffer size parameter will likely guard against such a mistake:
void remove_spaces(const char *input, char *output, size_t output_size);
See if you can implement this. Some things to bear in mind:
Don't forget about the NUL terminator when checking the output buffer size.
Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.
If you use
int
fori
andj
andsize_t
foroutput_size
, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typinggcc -Wall -W
.
$endgroup$
As @JeffMercado pointed out, this removes spaces instead of trimming leading and trailing spaces. Assuming you want to keep the current functionality, let's call it remove_spaces
.
There's a really subtle bug here:
... isspace(input[i]) ...
isspace
takes the value of an unsigned char or EOF
. Passing it a char
, which is usually signed, will produce undefined behavior. Instead, say:
... isspace((unsigned char) input[i]) ...
Another bug: you don't emit a NUL terminator, meaning the caller would have no way of knowing how long the string is (unless it zeroed out the buffer before calling your function).
Fixing these bugs gives us:
void remove_spaces(const char *input, char *result)
{
int i, j = 0;
for (i = 0; input[i] != ''; i++) {
if (!isspace((unsigned char) input[i])) {
result[j++] = input[i];
}
}
result[j] = '';
}
@JeffMercado also said this function is vulnerable to buffer overflow. In a sense, this is not true, provided the caller knows to allocate a buffer of at least strlen(input) + 1
. But the caller might be lazy and just say char result[100]
. Adding an output buffer size parameter will likely guard against such a mistake:
void remove_spaces(const char *input, char *output, size_t output_size);
See if you can implement this. Some things to bear in mind:
Don't forget about the NUL terminator when checking the output buffer size.
Don't be like strncpy and omit the NUL terminator when you have to truncate the string, as it can lead to subtle bugs.
If you use
int
fori
andj
andsize_t
foroutput_size
, you should get compiler warnings about comparison between signed and unsigned. If you don't, turn up your compiler warnings. If you're using GCC from the command line, get in the habit of typinggcc -Wall -W
.
answered Jan 26 '13 at 1:02
Joey AdamsJoey Adams
673413
673413
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.
$endgroup$
– Deduplicator
4 hours ago
add a comment |
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.
$endgroup$
– Deduplicator
4 hours ago
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.$endgroup$
– Deduplicator
4 hours ago
$begingroup$
strncpy()
is not a string-function, even though some assume. So the result being a string would be happenstance anyway. Which makes the analogy sketchy at best.$endgroup$
– Deduplicator
4 hours ago
add a comment |
$begingroup$
We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while
loops are enough. You'll notice that the right walk count is less than the left walk count.
Right-trim code:
#include <stdio.h>
#include <ctype.h>
void trim_both(char *, char *);
int main (void) {
char title[100] = " My long string ";
char title_t[100] = "";
(void) printf("String before left trim is:[%s]n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]n", title_t);
}
// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;
// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}
// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '';
}
}
$endgroup$
add a comment |
$begingroup$
We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while
loops are enough. You'll notice that the right walk count is less than the left walk count.
Right-trim code:
#include <stdio.h>
#include <ctype.h>
void trim_both(char *, char *);
int main (void) {
char title[100] = " My long string ";
char title_t[100] = "";
(void) printf("String before left trim is:[%s]n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]n", title_t);
}
// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;
// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}
// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '';
}
}
$endgroup$
add a comment |
$begingroup$
We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while
loops are enough. You'll notice that the right walk count is less than the left walk count.
Right-trim code:
#include <stdio.h>
#include <ctype.h>
void trim_both(char *, char *);
int main (void) {
char title[100] = " My long string ";
char title_t[100] = "";
(void) printf("String before left trim is:[%s]n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]n", title_t);
}
// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;
// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}
// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '';
}
}
$endgroup$
We know that we can move a pointer forward and backward, and we also know that we can trim a string from the left. If we increment the pointer and decrement the pointer to trim from the right, then two while
loops are enough. You'll notice that the right walk count is less than the left walk count.
Right-trim code:
#include <stdio.h>
#include <ctype.h>
void trim_both(char *, char *);
int main (void) {
char title[100] = " My long string ";
char title_t[100] = "";
(void) printf("String before left trim is:[%s]n", title);
trim_both(title, title_t);
(void) printf("String after left trim is:[%s]n", title_t);
}
// trim spaces from left
void trim_both(char *title_p, char *title_tp) {
int flag = 0;
// from left
while(*title_p) {
if(!isspace((unsigned char) *title_p) && flag == 0) {
*title_tp++ = *title_p;
flag = 1;
}
title_p++;
if(flag == 1) {
*title_tp++ = *title_p;
}
}
// from right
while(1) {
title_tp--;
if(!isspace((unsigned char) *title_tp) && flag == 0) {
break;
}
flag = 0;
*title_tp = '';
}
}
edited Jul 16 '14 at 4:36
Jamal♦
30.6k11121227
30.6k11121227
answered Jul 16 '14 at 1:49
Asim MasoodAsim Masood
11
11
add a comment |
add a comment |
$begingroup$
Easiest way (removes only spaces):
Trim.Start:
- Compare chars until they equal
' '
(space or other chars liken
ort
) at the string start and increment temp (i
) variable. - Move pointer about
i
(str+=i
). Now string starts from char which is not a space char (or any other white char).
Trim.End:
- Do the same what for Trim.Start but from the end of the string.
- Set last char (last space) as
.
The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);
char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;
// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;
if (start < length) // Required for empty (ex. " ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}
return *pointerToString;
}
Usage:
char str1 = " test1 ";
char * p1 = str1;
Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);
char str2 = " test2";
char * p2 = str2;
Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);
char str3 = "test3 ";
char * p3 = str3;
Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);
char str4 = " ";
char * p4 = str4;
Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);
char str5 = "";
char * p5 = str5;
Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);
Result:
1. before trim: [ test1 ]
1. after trim [test1]
2. before trim: [ test2]
2. after trim [test2]
3. before trim: [test3 ]
3. after trim [test3]
4. before trim: [ ]
4. after trim
5. before trim:
5. after trim
$endgroup$
add a comment |
$begingroup$
Easiest way (removes only spaces):
Trim.Start:
- Compare chars until they equal
' '
(space or other chars liken
ort
) at the string start and increment temp (i
) variable. - Move pointer about
i
(str+=i
). Now string starts from char which is not a space char (or any other white char).
Trim.End:
- Do the same what for Trim.Start but from the end of the string.
- Set last char (last space) as
.
The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);
char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;
// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;
if (start < length) // Required for empty (ex. " ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}
return *pointerToString;
}
Usage:
char str1 = " test1 ";
char * p1 = str1;
Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);
char str2 = " test2";
char * p2 = str2;
Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);
char str3 = "test3 ";
char * p3 = str3;
Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);
char str4 = " ";
char * p4 = str4;
Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);
char str5 = "";
char * p5 = str5;
Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);
Result:
1. before trim: [ test1 ]
1. after trim [test1]
2. before trim: [ test2]
2. after trim [test2]
3. before trim: [test3 ]
3. after trim [test3]
4. before trim: [ ]
4. after trim
5. before trim:
5. after trim
$endgroup$
add a comment |
$begingroup$
Easiest way (removes only spaces):
Trim.Start:
- Compare chars until they equal
' '
(space or other chars liken
ort
) at the string start and increment temp (i
) variable. - Move pointer about
i
(str+=i
). Now string starts from char which is not a space char (or any other white char).
Trim.End:
- Do the same what for Trim.Start but from the end of the string.
- Set last char (last space) as
.
The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);
char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;
// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;
if (start < length) // Required for empty (ex. " ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}
return *pointerToString;
}
Usage:
char str1 = " test1 ";
char * p1 = str1;
Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);
char str2 = " test2";
char * p2 = str2;
Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);
char str3 = "test3 ";
char * p3 = str3;
Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);
char str4 = " ";
char * p4 = str4;
Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);
char str5 = "";
char * p5 = str5;
Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);
Result:
1. before trim: [ test1 ]
1. after trim [test1]
2. before trim: [ test2]
2. after trim [test2]
3. before trim: [test3 ]
3. after trim [test3]
4. before trim: [ ]
4. after trim
5. before trim:
5. after trim
$endgroup$
Easiest way (removes only spaces):
Trim.Start:
- Compare chars until they equal
' '
(space or other chars liken
ort
) at the string start and increment temp (i
) variable. - Move pointer about
i
(str+=i
). Now string starts from char which is not a space char (or any other white char).
Trim.End:
- Do the same what for Trim.Start but from the end of the string.
- Set last char (last space) as
.
The important thing is that function takes pointer to pointer (string). Watch for the function call: StringTrim(&p2);
char * StringTrim(char * *pointerToString)
{
u8 start=0, length=0;
// Trim.Start:
length = strlen(*pointerToString);
while ((*pointerToString)[start]==' ') start++;
(*pointerToString) += start;
if (start < length) // Required for empty (ex. " ") input
{
// Trim.End:
u8 end = strlen(*pointerToString)-1; // Get string length again (after Trim.Start)
while ((*pointerToString)[end]==' ') end--;
(*pointerToString)[end+1] = 0;
}
return *pointerToString;
}
Usage:
char str1 = " test1 ";
char * p1 = str1;
Debug("1. before trim: [%s]", p1);
StringTrim(&p1);
Debug("1. after trim [%s]", p1);
char str2 = " test2";
char * p2 = str2;
Debug("2. before trim: [%s]", p2);
StringTrim(&p2);
Debug("2. after trim [%s]", p2);
char str3 = "test3 ";
char * p3 = str3;
Debug("3. before trim: [%s]", p3);
StringTrim(&p3);
Debug("3. after trim [%s]", p3);
char str4 = " ";
char * p4 = str4;
Debug("4. before trim: [%s]", p4);
StringTrim(&p4);
Debug("4. after trim [%s]", p4);
char str5 = "";
char * p5 = str5;
Debug("5. before trim: [%s]", p5);
StringTrim(&p5);
Debug("5. after trim [%s]", p5);
Result:
1. before trim: [ test1 ]
1. after trim [test1]
2. before trim: [ test2]
2. after trim [test2]
3. before trim: [test3 ]
3. after trim [test3]
4. before trim: [ ]
4. after trim
5. before trim:
5. after trim
edited Aug 4 '14 at 13:52
Jamal♦
30.6k11121227
30.6k11121227
answered Aug 1 '14 at 14:04
tBlabstBlabs
1
1
add a comment |
add a comment |
protected by Jamal♦ 19 mins ago
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
4
$begingroup$
There's a number of problems with that code, it's vulnerable to buffer overflow attacks and it doesn't do what a typical "trim" function does. Trim removes leading and trailing whitespace. This strips them all out.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 15:12
$begingroup$
Thanks. Can you please elaborate on how to handle buffer overflow attacks?
$endgroup$
– jay rulher
Jan 25 '13 at 15:44
2
$begingroup$
You should never blindly copy data to some buffer when you don't know how much space is allocated to it, that's just asking for trouble. A simple thing to do would be to add a parameter which takes in the size of the buffer. That way it's all on the caller to tell you how big it really is. Then it's up to you to never attempt to read/write beyond the given length. Of course it's not fool proof, the caller can give you bogus lengths, but that would be a problem on their end, not yours.
$endgroup$
– Jeff Mercado
Jan 25 '13 at 17:05