Assembly language > Assembly discussions

RightTrim function

<< < (2/2)

Vortex:
Hi Grincheux,


--- Quote ---Why do you return a pointer on lTable?
lTable could be got by using VirtualAlloc.
--- End quote ---

ltable is a lookup table and VirtualAlloc would require you to initialize the empty memory buffer : you would need to set the TAB 9 and SPACE 32 characters. A ready string in the .data section can be effective if you call multiple times the same RightTrim function : No need to allocate and deallocate memory portions for every function call. I don't tell that VirtualAlloc is wrong. It's probably a matter of programming choice.


--- Quote ---The resulting string cannot be longer than the one passed as an argument.
It can only be shorter or the same size.
So why not use lstrcpy?
The string argument is limited to 256 bytes!
If the string is longer than 256 the function must be called like this RightTrim(&_szString[lstrlen(_szString) - 256]) ;
--- End quote ---

The size of the ASCII table ( the lookup table )  is 256 bytes but this should not guide you to the conclusion that the target string to trim should have a maximum size of of 256. It's the range of ASCII codes falling to this limitation, not the size of the string to be trimmed. Here is an example :


--- Code: ---#include <stdio.h>
#include <windows.h>

extern char* RightTrim(char *);

char teststr[]="This is a long string to test the function RightTrim. The string includes a lot space and TAB characters.                                                                                                                                                           ";

int __cdecl main(int argc,char *argv[])
{
printf("The initial length of the string = %u\n",lstrlen(teststr));

printf("The length of the string after calling RightTrim  = %u\n",lstrlen(RightTrim(teststr)));

return 0;
}

--- End code ---

Running the 64-bit test application built with Pelles C V8 :


--- Code: ---The initial length of the string = 300
The length of the string after calling RightTrim  = 105
--- End code ---

Notice that you need to be careful while inserting very long strings to your Pelles C code. My string is declared as global and it's placed directly to the .data section. If the compiler moves the long string to the .const section, the RightTrim function cannot insert the NULL character trimming the string. You can check this by disassembling RightTrimTest.obj with Agner Fog's objconv tool.

Grincheux:
Thank You Professor. :)

Navigation

[0] Message Index

[*] Previous page

Go to full version