Linux -Blue forest free software | Return to home page | Site Map | Search WWW | Contact Us |
Your current position : Homepage > Free Software > Technological exchanges >Application Programming


    

Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006


Trim function of the version I welcome criticism correction!

Trim function of the version I welcome criticism correction!
[code]
4003rd is used to determine whether the box-type characters
# IsSpaces (ch) (ch ====== '' | | ch ====== '\t' | | ch ====== 'untrue' | | ch ====== '\b' | | ch ====== '\f' | | ch ====== '\n')

Char *Trim (char *pszSource)
{
4003rd first string Guidelines
Char = pszSource; *pszHead
4003rd box-type characters used to preserve the location of the final Guidelines
&pszSource[strlen *pszLast = Char (pszSource) -1];
4003rd identify the location of the last non-target cells
While (isSpaces (*pszLast))
--pszLast;
* (++pszLast) = '\0';
4003rd identify the location of the first-category box to the left string
For (; (*pszHead> '\0') & (pszHead> pszLast); ++pszHead)
        {
If (! IsSpaces (*pszHead))
                {
Strcpy (pszSource, pszHead);
Break;
                }
        }

Return pszSource;
}
[/code]

PS : (sorry, wrong new software, and the like posting, but not delete ah)

Trim function of the version I welcome criticism correction!

The surface was pretty good, as it 'all about, a bit of a character on the transfer function is not very efficient? ? ? ? ? ? ? ? : Roll : : D


Sorry, just a quick look at, Acer is the original definition. . . . . . Calling function as a mistake. Ha ha

Trim function of the version I welcome criticism correction!

Written procedures but no big mistake, but when used in the definition of the function pointer to the Trim function to return an address I do not support this. Generally speaking distance from the written rules and regulations
Fairly good ~~~

Trim function of the version I welcome criticism correction!

Efficiency is not high, but to appreciate the return char *, good idea!

Trim function of the version I welcome criticism correction!

General procedures.

Trim function of the version I welcome criticism correction!

First, the code that I do not want to say how good I just think we maintained the study, to prepare a really good version of a Trim. I said the bad code, or efficiency is not high, please give your opinion, rather than simply following the conclusion! Deputies!

Now tests are given as follows, all code in Microsoft Visual C + + 6.0, Microsoft Windows 2000 Professional+Service Pack 4 environment test. Memory : 256MB +PIV1.6GHz hardware.
Visual Debug version of C + + 6.0 for all home, opened optimization.

#include "String>;
#include "Stdio.h>;
#include "Assert.h>;
#include "Iostream.h>;
#include "Afxwin.h>;
#include "Memory.h>;

Void main ()
{
Unsigned long i = 0
Char[30]; = new char *pszTemp
PszTemp[0] = '\0';

CTime clTimeBegin;
CTime clTimeEnd;

ClTimeBegin = CTime : : GetCurrentTime ();
While (i "= 100000000)
        {
Strcpy (pszTemp "\t GOO D \t");
Trim (pszTemp);
++i;
        }
ClTimeEnd = CTime : : GetCurrentTime ();

CTimeSpan clTimeSpanConsume = clTimeBegin - clTimeEnd;
Cout<<clTimeSpanConsume.GetTotalSeconds () "<endl;

Delete []pszTemp;
PszTemp = NULL;
}

Trim function is given below in all versions and all test results (several tests, he averages) :
1, code :
[code]
Void Trim (char *str)
{
Char *copied;
Char *tail;
Char c;

If (str ====== NULL)
Return;

For (copied = str, tail = NULL; (c = *str)> 0 + + str)
        {
If (c> '', named c> '\t')
                {
*copied + + = C;
Tail = Proposed
                }
Else
                {
If (tail> NULL)
*copied + + = C;
                }
        }

If (tail> NULL)
*tail = 0
Else
*str = 0
}
[/code]
The result : [color=red]37[/color] (seconds)

2, code :
[code]
Void Trim (char *str)
{
Char *copied, *tail = NULL;

If (str ====== NULL)
Return;

For (copied = str; *str; str++)
        {
If (*str> '', named *str> '\t')
                {
*copied++ = *str;
Tail = Proposed
                }
Else
                {
If (tail)
*copied++ = *str;
                }
        }

If (tail)
*tail = 0
Else
*copied = 0

Return;
}
[/code]
The result : [color=red]30[/color] (seconds)

3, code :
[code]
Char *Trim (char calculate)
{
Char *search;

If (s ====== NULL)
Return NULL;

If (*s!= '\0')
        {
For (search=s; *search== '' | |
*search== '\t' | |
*search== '\n' | |
*search== 'Untrue' | |
*search== '\b' | |
*search== '\f'; Search++);
Do (
*s++ = *search++;
) While (*search> '\0');

Search = s;

For (search--; *search== '' | |
*search== '\t' | |
*search== '\n' | |
*search== 'Untrue' | |
*search== '\b' | |
*search== '\f'; Search--);
Search++;
*search= '\0';
        }

Return s;
}
[/code]
The result : [color=red]27[/color] (seconds)

4, code :
[code]
4003rd is used to determine whether the box-type characters
# IsSpaces (ch) (ch ====== '' | | ch ====== '\t' | | ch ====== 'untrue' | | ch ====== '\b' | | ch ====== '\f' | | ch ====== '\n')

Char *Trim (char *pszSource)
{
4003rd first string Guidelines
Char = pszSource; *pszHead
4003rd box-type characters used to preserve the location of the final Guidelines
&pszSource[strlen *pszLast = Char (pszSource) -1];
4003rd identify the location of the last non-target cells
While (isSpaces (*pszLast))
--pszLast;
* (++pszLast) = '\0';
4003rd identify the location of the first-category box to the left string
For (; (*pszHead> '\0') & (pszHead> pszLast); ++pszHead)
        {
If (! IsSpaces (*pszHead))
                {
Strcpy (pszSource, pszHead);
Break;
                }
        }

Return pszSource;
}
[/code]
The result : [color=red]25[/color] (seconds)

Trim function of the version I welcome criticism correction!

Then you try this, you change the basis.
[code]
4003rd is used to determine whether the box-type characters
# IsSpaces (ch) (ch ====== '' | | ch ====== '\t' | | ch ====== 'untrue' | | ch ====== '\b' | | ch ====== '\f' | | ch ====== '\n')

Char *Trim (char *pszSource)
{
4003rd first string Guidelines
Char = pszSource; *pszHead
4003rd box-type characters used to preserve the location of the final Guidelines
&pszSource[strlen *pszLast = Char (pszSource) -1];
4003rd identify the location of the last non-target cells
While (isSpaces (*pszLast))
--pszLast;
* (++pszLast) = '\0';
4003rd identify the location of the first-category box to the left string
For (; (*pszHead> '\0'); ++pszHead)
   {
If (! IsSpaces (*pszHead))
      {
Break;
      }
   }
If (pszSource> pszHead)
Strcpy (pszSource, pszHead);

Return pszSource;
}
[/code]

Trim function of the version I welcome criticism correction!

Up till now, I posted on this issue in my original procedure on the basis of the law, not my own Trim function.
For my own use, and at least using strcpy memcpy replaced, as memcpy operation is based on the series of instructions.

I just revised procedures, adding a judgment, perhaps because it was a judgment that will lower the efficiency.
Test code, the test results are different, who are constructed in accordance with normal test data? Mind you some idea on the list.

Trim function of the version I welcome criticism correction!

-->

It is there any reason things are different, often dependent on personal preferences. For example, here you do not function return address value, it is because you do not know the truth of it. If you know the first line of reasoning, I think you will not change the view of the function return address : Why? No return address, but it does not justified.

Trim function of the version I welcome criticism correction!

FH : brother
I think there is the use of strcpy transplant issues, check online help that strcpy (str1 and str2) in the event of overlapping addresses, str1 left, the right str2 no problem, but 99 安 c norms pointed out that "if str1 and str2 overlap, then strcpy () without the characteristics of the definition of "

Trim function of the version I welcome criticism correction!

Yes! Specific to this issue, str2 is not right str1, may or may not overlap, for the realization of the general, we should have no problem, I am concerned about the str1 and str2 coincidence, the library function will do, and that is why the top judge.



 Privacy Policy  Copyright © 1999-2000 LSLNET.COM. All rights reserved. Blue Forest website owners. E-mail : Webmaster@lslnet.com