Register | Login
Forum Index > Source Help > Trim function
Author Message
Pages: 1 2
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[200] - posted: 2011-10-29 18:47:10
Ziron does not support indirect access to pointers (i will consider this to be added)

Code:
lea eax, len
[eax] = ecx;




Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[201] - posted: 2011-10-29 18:54:44

ok thanks ,

one more thing , the Tirm Function has two parameters now , so can i call it like this
Code:
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
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
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[202] - posted: 2011-10-29 19:34:45
so for to have default parameters? if this is what you mean, it is on my todo list smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[204] - posted: 2011-10-29 22:43:46

yes, this is what i mean.

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

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[239] - posted: 2011-10-31 17:44:34
a quick trim function i've just put together

Code:
//
// 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:

Code:
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:
Code:
mystr: "THIS IS A TEST" = 14 chars 


Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Admin
Site Admin

avatar

(send private message)

Posts: 933
Topics: 55

Location:
OverHertz Studio
[240] - posted: 2011-10-31 17:59:28
another version allowing parameter of which char you want trimmed

Code:
//
// 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:

Code:
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 smile

Download Ziron
Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.
Emil_halim
Ziron Beta Tester

avatar

(send private message)

Posts: 639
Topics: 104

Location:
Alex, Egypt
[243] - posted: 2011-10-31 20:29:18

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
Code:
/*************************************************************************/

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
Pages: 1 2
create new reply


Quick reply:

Message:



Currently Active Users:
There are currently 5 user(s) online. 0 member(s) and 5 guest(s)
Most users ever online was 1046, January 28, 2022, 2:08 pm.


Statistics:
Threads: 225 | Posts: 1848 | Members: 51 | Active Members: 51
Welcome to our newest member, yecate
const Copyright = '2011-2024 © OverHertz Ltd. All rights reserved.';
Web development by OverHertz Ltd