修剪char *的最佳方式(对吗?) [英] The best, (right?), way to trim a char*

查看:76
本文介绍了修剪char *的最佳方式(对吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我写了一个修剪char *的函数,但我被告知我的方式

可能是危险的,我应该改为使用memmove(...)。

但我不确定为什么我的代码可能危险,甚至为什么可能会出现问题。

这里是代码


////////

const char * TrimLeft(char * dest)

{

if(!dest)return dest; //全部完成

size_t size = 0;

//修剪左边

while(size> = 0&&(_istspace (dest [size])||

dest [size] == 10 ||

dest [size] == 13))

{

for(size_t loop = 0; loop< strlen(dest)-1; loop ++)

dest [loop] = dest [loop +1];

dest [strlen(dest)-1] =''\ 0'';

}

返回dest;

}


const char * TrimRight(char * dest)

{

if(!dest)return dest; //全部完成

int size = int(strlen(dest));


//修剪右边

size-- ;

while(size> = 0&&(_istspace(dest [size])||

dest [size] == 10 ||

dest [size] == 13))

{

dest [size] =''\ 0'';

size--;

}

返回dest;

}


const char *修剪(char * dest)

{

TrimLeft(dest);

TrimRight(dest);

返回dest;

}


/////////////////////

//一些测试

/////////////////////

int main(int argc,char ** argv)

{

char a [10 + 1];

strcpy(a," 12345678");


char * b =新字符[10 + 1];

strcpy(b," 12345678");


修剪(a);

修剪(b);

//清洁

删除[] b;


...

返回1;

}


/////////

我想这不可能,但我会问以防万一,会

有没有办法修剪分配的内存?

我的意思是如果我这样做

char * a = new char [1024];

strcpy(a,"一个 );

修剪(a);


将''a''仍然分配1024个字符或者它可能是

''修剪''到2并释放剩余的内存?

非常感谢


Simon

Hi,

I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be ''dangerous'' or even why there could
be a problem.

here is the code

////////
const char* TrimLeft( char *dest)
{
if (!dest ) return dest; //all done
size_t size = 0;
// trim left
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
for ( size_t loop = 0; loop < strlen( dest ) -1; loop++ )
dest[ loop] = dest[ loop +1];
dest[ strlen( dest ) -1 ] = ''\0'';
}
return dest;
}

const char* TrimRight( char *dest)
{
if (!dest ) return dest; //all done
int size = int(strlen( dest ));

// trim right
size--;
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
dest[ size] = ''\0'';
size--;
}
return dest;
}

const char* Trim( char *dest)
{
TrimLeft ( dest );
TrimRight ( dest );
return dest;
}

/////////////////////
// some test
/////////////////////
int main( int argc, char **argv )
{
char a[10+1];
strcpy( a, " 12345678 " );

char * b = new char[10+1];
strcpy( b, " 12345678 " );

Trim(a);
Trim(b);
// clean
delete [] b;

...
return 1;
}

/////////
Also I guess it is not really possible but I''ll ask just in case, would
there be a way of trimming the memory allocated?
by that I mean if I do
char *a = new char[1024];
strcpy( a, " a " );
Trim( a);

would ''a'' still have 1024 characters allocated to it or could it be
''trimmed'' to 2 and free the rest of the memory?
Many thanks

Simon

推荐答案




" Simon" < SP ******** @ schoolsofafrica.com>在留言中写道

news:2q ************ @ uni-berlin.de ...
Hi,

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2q************@uni-berlin.de...

<我已经写了一个修饰char *的函数,但我被告知我的方式
可能很危险,我应该使用memmove(...)而不是。
但我不确定为什么我的代码可能危险,甚至为什么会出现问题。

这里是代码

/////// /
const char * TrimLeft(char * dest)
{
if(!dest)返回dest; //全部完成
size_t size = 0;
//修剪左边
while(size> = 0&&(_istspace(dest [size])||
dest [size] == 10 ||
dest [size] == 13))
{
for(size_t loop = 0; loop< strlen(dest)-1; loop ++)
dest [loop] = dest [loop +1];
dest [strlen(dest)-1] =''\ 0'';
}
返回dest;
}
即使在普通的C中也是很多代码:-)


好​​了,因为这是一个C ++组

#include< string>

#include< algorithm>

using namespace std;


string& TrimRight(string& String)

{

string :: size_type Pos = String.find_last_not_of('''')+ 1;

if (Pos< String.size())String.erase(Pos);

return String;

}


string& ; TrimLeft(string& String)

{

string :: size_type Pos = String.find_first_not_of('''');

if(Pos < String.size())

{

copy(String.begin()+ Pos,String.end(),String.begin());

String.erase(String.begin()+ String.size() - Pos,String.end());

}

return String ;

}

问候,Ron AF Greve。

const char * TrimRight(char * dest)
{
if(!dest)返回dest; //全部完成
int size = int(strlen(dest));

//修剪右边
size--;
while(size> = 0 &&(_istspace(dest [size])||
dest [size] == 10 ||
dest [size] == 13))
{
dest [size] =''\ 0'';
size--;
}
返回dest;
}

const char * Trim( char * dest)
{TrimLeft(dest);
TrimRight(dest);
返回dest;
}
/// //////////////////
//一些测试
//////////////////// /
int main(int argc,char ** argv)
{char a [10 + 1];
strcpy(a," 12345678");

char * b =新字符[10 + 1];
strcpy(b," 12345678");

修剪(a);
修剪(b);
//清洁
删除[] b;

...
返回1;
}

/////////

我想这不可能,但我会问以防万一,会有一个修剪分配的内存的方法是什么?
我的意思是如果我这样做
char * a = new char [1024];
strcpy(a,"一个 );
修剪(a);

将'a''仍然有1024个字符分配给它或者它可以被'修剪''到2并释放其余的记忆?

不,你可以尝试新的,复制和删除原始但通常的

性能惩罚并不超过可能但不保证的记忆

获得。


问候,Ron AF Greve
非常感谢

Simon
Hi,

I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be ''dangerous'' or even why there could
be a problem.

here is the code

////////
const char* TrimLeft( char *dest)
{
if (!dest ) return dest; //all done
size_t size = 0;
// trim left
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
for ( size_t loop = 0; loop < strlen( dest ) -1; loop++ )
dest[ loop] = dest[ loop +1];
dest[ strlen( dest ) -1 ] = ''\0'';
}
return dest;
} Ouch that is a lot of code even in plain C :-)

Well since this is a C++ group
#include <string>
#include <algorithm>

using namespace std;

string& TrimRight( string& String )
{
string::size_type Pos = String.find_last_not_of( '' '' ) + 1;
if( Pos < String.size() ) String.erase( Pos );
return String;
}

string& TrimLeft( string& String )
{
string::size_type Pos = String.find_first_not_of( '' '' ) ;
if( Pos < String.size() )
{
copy( String.begin() + Pos, String.end(), String.begin() );
String.erase( String.begin() + String.size() - Pos, String.end() );
}
return String;
}
Regards, Ron AF Greve.


const char* TrimRight( char *dest)
{
if (!dest ) return dest; //all done
int size = int(strlen( dest ));

// trim right
size--;
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
dest[ size] = ''\0'';
size--;
}
return dest;
}

const char* Trim( char *dest)
{
TrimLeft ( dest );
TrimRight ( dest );
return dest;
}

/////////////////////
// some test
/////////////////////
int main( int argc, char **argv )
{
char a[10+1];
strcpy( a, " 12345678 " );

char * b = new char[10+1];
strcpy( b, " 12345678 " );

Trim(a);
Trim(b);
// clean
delete [] b;

...
return 1;
}

/////////
Also I guess it is not really possible but I''ll ask just in case, would
there be a way of trimming the memory allocated?
by that I mean if I do
char *a = new char[1024];
strcpy( a, " a " );
Trim( a);

would ''a'' still have 1024 characters allocated to it or could it be
''trimmed'' to 2 and free the rest of the memory?
No, you could try to new, copy and delete the original but usual the
performance penalty doesn''t outweigh a possible but not guarenteed memory
gain.

Regards, Ron AF Greve
Many thanks

Simon





" Simon" < SP ******** @ schoolsofafrica.com>在留言中写道

news:2q ************ @ uni-berlin.de ...

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2q************@uni-berlin.de...

<我已经写了一个修饰char *的函数,但我被告知我的方式
可能很危险,我应该使用memmove(...)而不是。
但我不确定为什么我的代码可能危险,甚至为什么会出现问题。


除了你的TrimLeft函数是<我没有问题我能看到br />
效率低下。你应该计算字符串开头的空白字符数,然后只复制一次。你的方式

如果你的字符串开头有多个空白字符

然后你反复复制。


你正在谈论的人可能是关于何时使用

memcpy以及何时使用memmove的建议,但这与你的案件无关,因为

你不是''使用memcpy。

这里是代码

////////
const char * TrimLeft(char * dest)
{
if(!dest)返回dest; //全部完成
size_t size = 0;
//修剪左边
while(size> = 0&&(_istspace(dest [size])||
dest [size] == 10 ||
dest [size] == 13))
{
for(size_t loop = 0; loop< strlen(dest)-1; loop ++)
dest [loop] = dest [loop +1];
dest [strlen(dest)-1] =''\ 0'';
}
返回dest;
}

const char * TrimRight(char * dest)
{
if(!dest)返回dest; //全部完成
int size = int(strlen(dest));

//修剪右边
size--;
while(size> = 0 &&(_istspace(dest [size])||
dest [size] == 10 ||
dest [size] == 13))
{
dest [size] =''\ 0'';
size--;
}
返回dest;
}

const char * Trim( char * dest)
{TrimLeft(dest);
TrimRight(dest);
返回dest;
}
/// //////////////////
//一些测试
//////////////////// /
int main(int argc,char ** argv)
{char a [10 + 1];
strcpy(a," 12345678");

char * b =新字符[10 + 1];
strcpy(b," 12345678");

修剪(a);
修剪(b);
//清洁
删除[] b;

...
返回1;
}

/////////

我想这不可能,但我会问以防万一,会有一个修剪分配的内存的方法是什么?
我的意思是如果我这样做
char * a = new char [1024];
strcpy(a,"一个 );
修剪(a);

将'a''仍然有1024个字符分配给它或者它可以被'修剪''到2并释放剩下的内存?
Hi,

I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be ''dangerous'' or even why there could
be a problem.

There is no problem I can see, except that your TrimLeft function is
inefficient. You should count the number of whitespace chars at the
beginning of the string and copy backward once only. The way you are doing
it if there are multiple whitespace chars at the beginning of your string
then you copy backward repeatedly.

The person you are talking is probably garbling advice about when to use
memcpy and when to use memmove, but thats not relevant to your case since
you aren''t using memcpy.
here is the code

////////
const char* TrimLeft( char *dest)
{
if (!dest ) return dest; //all done
size_t size = 0;
// trim left
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
for ( size_t loop = 0; loop < strlen( dest ) -1; loop++ )
dest[ loop] = dest[ loop +1];
dest[ strlen( dest ) -1 ] = ''\0'';
}
return dest;
}

const char* TrimRight( char *dest)
{
if (!dest ) return dest; //all done
int size = int(strlen( dest ));

// trim right
size--;
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
dest[ size] = ''\0'';
size--;
}
return dest;
}

const char* Trim( char *dest)
{
TrimLeft ( dest );
TrimRight ( dest );
return dest;
}

/////////////////////
// some test
/////////////////////
int main( int argc, char **argv )
{
char a[10+1];
strcpy( a, " 12345678 " );

char * b = new char[10+1];
strcpy( b, " 12345678 " );

Trim(a);
Trim(b);
// clean
delete [] b;

...
return 1;
}

/////////
Also I guess it is not really possible but I''ll ask just in case, would
there be a way of trimming the memory allocated?
by that I mean if I do
char *a = new char[1024];
strcpy( a, " a " );
Trim( a);

would ''a'' still have 1024 characters allocated to it or could it be
''trimmed'' to 2 and free the rest of the memory?




使用std :: string类,更简单,更安全,更高效,更实用

C ++。目前您正在编写C而不是C ++。请阅读一本关于C ++的书,该书解释了关于标准C ++库的
,例如: Josuttis的C ++标准库。


john



Use the std::string class instead, easier, safer, more efficient and real
C++. At the moment you are coding C not C++.Get a book on C++ that explains
about the standard C++ library, e.g. ''The C++ Standard Library'' by Josuttis.

john




" Simon" ; < SP ******** @ schoolsofafrica.com>在留言中写道

news:2q ************ @ uni-berlin.de ...

"Simon" <sp********@schoolsofafrica.com> wrote in message
news:2q************@uni-berlin.de...

<我已经写了一个修饰char *的函数,但我被告知我的方式
可能很危险,我应该使用memmove(...)而不是。
但我不确定为什么我的代码可能危险,甚至为什么会出现问题。

这里是代码

/////// /
const char * TrimLeft(char * dest)


你接受一个char *,但返回与const char *相同。你真的这么做吗?b $ b意味着这样做吗?


{
if(!dest)返回dest; //全部完成
size_t size = 0;
//修剪左边
while(size> = 0&&(_istspace(dest [size])||
dest [size] == 10 ||
dest [size] == 13))
{
for(size_t loop = 0; loop< strlen(dest)-1; loop ++)
dest [loop] = dest [loop +1];
dest [strlen(dest)-1] =''\ 0'';
}
返回dest;
}
Hi,

I have written a function to trim char *, but I have been told that my way
could be dangerous and that I should use memmove(...) instead.
but I am not sure why my code could be ''dangerous'' or even why there could
be a problem.

here is the code

////////
const char* TrimLeft( char *dest)
you take in a char*, but return the same as a const char*. Did you really
mean to do that?

{
if (!dest ) return dest; //all done
size_t size = 0;
// trim left
while( size >= 0 && ( _istspace( dest[ size]) ||
dest[ size] == 10 ||
dest[ size] == 13))
{
for ( size_t loop = 0; loop < strlen( dest ) -1; loop++ )
dest[ loop] = dest[ loop +1];
dest[ strlen( dest ) -1 ] = ''\0'';
}
return dest;
}




非常低效。你有一个O(n ^ 3)算法(而* for * strlen)对于

应该只是一个线性的。

怎么样


void trim_left(char * s)

{

size_t sz = strlen(s);

char * p = find(s,s + sz,not1(isspace)));

if(p!= s&& p!= s + sz)

memmove(s,p,sz - (p - s)+ 1);

}


或者如果你要去要返回指针,只需返回p而不是

移动字符。



Very inefficient. You have an O(n^3) algorithm (while * for * strlen) for
what should only be a linear one.
how about:

void trim_left(char* s)
{
size_t sz = strlen(s);
char* p = find(s, s + sz, not1(isspace)));
if (p != s && p != s + sz)
memmove(s, p, sz - (p - s) + 1);
}

or if you are just going to return the pointer, just return p instead of
moving the chars.


这篇关于修剪char *的最佳方式(对吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆