Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Source Help

Trim function

A community discussion post from the original code:zIRON forum.

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Ziron does not support indirect access to pointers (i will consider this to be added)

lea eax, len
[eax] = ecx;
Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

ok thanks ,

one more thing , the Tirm Function has two parameters now , so can i call it like this

const name = '    Emil Halim    ';
int32 len = 0;
pointer str = Trim(name,@len);   // as usual 

pointer str2 = Trim(@name); // Optional parameter , so ignore the second paramter

here is the Trim code

function Trim(char* str;int32* len)
{  
  uses Ebx Ecx;
  Ebx = str;
  al = char[Ebx];
@Rept:     
 if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ebx++;
    al = char[Ebx];
    goto @Rept;
  }     
 Ecx = Ebx;
 while(char[Ecx] != 0)
  {
     Ecx++;
  }
 al = char[Ecx-1];
@Rept1:
  if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ecx--;
    al = char[Ecx-1];
    goto @Rept1;
  }     
 sub Ecx,Ebx 
 if(len != nil )
  {
    //*len = Ecx;
    Eax = len;
    Dword[Eax] = Ecx;  
  }
 Eax = GetTmpStr(Ecx);
 push Eax
 strlcpy(Eax,Ebx,Ecx);
 pop Eax
 char[Eax+Ecx]=0;
}

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

so for to have default parameters? if this is what you mean, it is on my todo list :)

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

yes, this is what i mean.

http://www.freewebs.com/ogremagic/index.htm

Colin admin
Posts
928
Threads
49
Location
Bulgaria

a quick trim function i've just put together

//
// Copyright (c) OverHertz Ltd.
// Trim buffer and return the new length
//
function Trim(char* buffer) {
	uses edx esi;
	esi = buffer;
	edx = esi;
	
	repeat {
		lodsb
		if (al == 0 or al != " ") {
			break;		
		}
	};
	
	repeat {
		[edx] = al;
		edx++;
		
		if (al == 0) {
			break;
		}
		lodsb		
	};	
	
	std;	
	
	lodsw
	edx--;
	
	repeat {
		edx--;
		lodsb
	} while (al == " ");
	
	cld;

	eax = edx;
	byte[edx] = 0;
	edx = buffer;
	sub eax, edx
}

sample with usage:

program WIN32CUI 'test';

#include 'console.zir';
#include 'ch.zir';
#include 'smm32.zir';


//
// Copyright (c) OverHertz Ltd.
// Trim buffer and return the new length
//
function Trim(char* buffer) {
	uses edx esi;
	esi = buffer;
	edx = esi;
	
	repeat {
		lodsb
		if (al == 0 or al != " ") {
			break;		
		}
	};
	
	repeat {
		[edx] = al;
		edx++;
		
		if (al == 0) {
			break;
		}
		lodsb		
	};	
	
	std;	
	
	lodsw
	edx--;
	
	repeat {
		edx--;
		lodsb
	} while (al == " ");
	
	cld;

	eax = edx;
	byte[edx] = 0;
	edx = buffer;
	sub eax, edx
}


edi = malloc(1024);
strlcpy(edi, '                   THIS IS A TEST                   ', 52);

eax = Trim(edi);

print('my str: "', edi:string, '" = ',eax,' chars\r\n');

freemem(edi);

wait_key(nil);
ExitProcess(0);

output:

mystr: "THIS IS A TEST" = 14 chars 
Colin admin
Posts
928
Threads
49
Location
Bulgaria

another version allowing parameter of which char you want trimmed

//
// Copyright (c) OverHertz Ltd.
// Trim buffer and return the new length
//
function Trim(char* buffer; char trimchar) {
	uses ecx edx esi;
	esi = buffer;
	edx = esi;
	
	cl = trimchar;
	if (cl == 0) {
		cl = " ";
	}
	
	repeat {
		lodsb
		if (al == 0 or al != cl) {
			break;		
		}
	};
	
	repeat {
		[edx] = al;
		edx++;
		
		if (al == 0) {
			break;
		}
		lodsb		
	};	
	
	std;	
	
	lodsw
	edx--;
	
	repeat {
		edx--;
		lodsb
	} while (al == cl);
	
	cld;

	eax = edx;
	byte[edx] = 0;
	edx = buffer;
	sub eax, edx
}

usage:

strlcpy(edi, '  --------------------THIS IS A TEST--------------------  ', 54);

eax = Trim(edi, '-');

print('my str: "', edi:string, '" = ',eax:int,' chars\r\n');

it is also possible to pass nil instead of a char and it will trim only spaces, once i implement default parameters this will be much better :)

Emil_halim member
Posts
639
Threads
104
Location
Alex, Egypt

default parameters this will be much better ofcourse yes.

BTW , the Trim function not only remove space , but also remove any \r , \n , tab , and space.

i have added 3 functions of Trim

/*************************************************************************/

function GetTmpStr(DWord sizeInByte)
{
   uses Ebx Ecx;   
   global int32   StrCnt;
   global char*   StrFunc[2048];
   //StrCnt=(StrCnt + 1) & 2047;
   Eax = StrCnt;
   Eax++;
   and Eax,2047
   StrCnt = Eax;
   //if(StrFunc[StrCnt]) free (StrFunc[StrCnt]);
   Ebx = @StrFunc;
   lea Ebx,[Ebx+Eax]
   Eax=DWord[Ebx];
   if( Eax> 0)
    {
       freemem( Eax);
    }
   //return StrFunc[StrCnt]=(char*)calloc(Bites+128,sizeof(char));
   [Ebx]= malloc(sizeInByte);
}
/*************************************************************************/
function Trim(char* str;int32* len)
{  
  uses Ebx Ecx;
  Ebx = str;
  al = char[Ebx];
/*
  while(al == 32 or al == 9 or al == 10 or al == 11 or al == 13)
   {
     Ebx++;
     al = char[Ebx];
   }
*/
@Rept:     
 if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ebx++;
    al = char[Ebx];
    goto @Rept;
  }       
 Ecx = Ebx;
 while(char[Ecx] != 0)
  {
     Ecx++;
  }
 al = char[Ecx-1];
@Rept1:
  if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ecx--;
    al = char[Ecx-1];
    goto @Rept1;
  }     
 sub Ecx,Ebx 
 if(len != nil )
  {
    //*len = Ecx;
    Eax = len;
    Dword[Eax] = Ecx;  
  }
 Eax = GetTmpStr(Ecx);
 push Eax
 strlcpy(Eax,Ebx,Ecx);
 pop Eax
 char[Eax+Ecx]=0;
}

/*************************************************************************/

function LTrim (char* str)
{  
  uses Ebx Ecx;
  Ebx = str;
  al = char[Ebx];
@Rept:     
 if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ebx++;
    al = char[Ebx];
    goto @Rept;
  }     
 Ecx = Ebx;
 while(char[Ecx] != 0)
  {
     Ecx++;
  }
 sub Ecx,Ebx 
 Eax = GetTmpStr(Ecx);
 push Eax
 strlcpy(Eax,Ebx,Ecx);
 pop Eax
}

/*************************************************************************/
function RTrim(char* str)
{  
 uses Ebx Ecx;
 Ebx = str;
 Ecx = Ebx;
 while(char[Ecx] != 0)
  {
     Ecx++;
  }
 al = char[Ecx-1];
@Rept1:
  if(al==32 or al==9 or al==10 or al==11 or al==13)
  { 
    Ecx--;
    al = char[Ecx-1];
    goto @Rept1;
  }     
 sub Ecx,Ebx 
 Eax = GetTmpStr(Ecx);
 push Eax
 strlcpy(Eax,Ebx,Ecx);
 pop Eax
 char[Eax+Ecx]=0;
}

/*************************************************************************/

http://www.freewebs.com/ogremagic/index.htm