whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

50 Visitors Online


 
...split a string *VERY* fast using delimiters (function) (part2)
Autor: Juhani Suhonen
[ Print tip ]  

Tip Rating (34):  
     


// after optimizations with delphi it comes a
// time for some assembler; also the memory
// management (slow result allocation) has been
// taken care of...

// note : You MUST have the following global
// variables :

// var
//    Splitresult : Array[0..1023] of Char;
//    P_SplitResArray : Pointer;

// then on form1.create assign Pointer :
//    P_SplitResArray := @SplitResult;

// variable Splitresult will contain
// the result after procedure has been completed


procedure split5(const input: string; schar: Char; s: Byte); register;
asm
          
PUSH ECX
          PUSH EAX           //registers :
          
PUSH EDX           //EAX = input
          
PUSH EDI           //DH = s
          
TEST EAX, EAX         //if input = '' then
          
JZ @NoWords   //                  goto @NoInput
@WeHaveIt:MOV DH, CL         //DL = schar
          
MOV EDI, EAX       //EDI := Addr(input);
          
MOV ECX, [EDI-4]   //ECX := length(input);
@GoGo:    MOV AL, DL         //AL := Schar;
          
CMP DH, 2      //IF DH = 2 then
          
JE @StartCo    //              goto @StartCo
          
MOV AH, 2      //AH (upper 8 bits of AX) will be our separator counter initial value is 2, max 256
@Lop:     INC AH        //AH := AH + 1
          
REPNE SCASB    //while (EDI^<>AL) and (ECX>0) begin INC EDI; DEC ECX; end;
          
CMP AH, DH     //if AH < DH then
          
JB @Lop        //               goto @Lop
          
TEST ECX, ECX     //IF ECX > 0 then
          
JG @StartCo    //               goto @StartCp
@NoWords: MOV EDI,[P_SplitResArray]  //result :=
          
MOV BYTE PTR [EDI], $0     //          ''
          
JMP @TotalEnd
@StartCo: PUSH ESI
          MOV ESI, EDI
          MOV EDI, [P_SplitResArray]
@CopyLoop:MOV AH, BYTE PTR[ESI+0]    //32 bit aligned loop.. hopefully :)
          
CMP AH, DL                 //1. test, 8 bits
          
JE @Endword
          MOV BYTE PTR [EDI+0], AH
          INC EDI
          MOV AH, BYTE PTR[ESI+1]
          CMP AH, DL                 //2. test, 8 bits -> 16 bits..
          
JE @Endword
          MOV BYTE PTR [EDI+0], AH
          INC EDI
          MOV AH, BYTE PTR[ESI+2]
          CMP AH, DL                 //3. test, 8 bits -> 24 bits..
          
JE @Endword
          MOV BYTE PTR [EDI+0], AH
          INC EDI
          MOV AH, BYTE PTR[ESI+3]
          CMP AH, DL                 //4. test, 8 bits -> 32 bits..
          
JE @Endword
          MOV BYTE PTR [EDI+0], AH
          INC EDI
          ADD ESI, 4
          SUB ECX, 4
          CMP ECX, 0
          JG @Copyloop
@EndWord: MOV BYTE PTR [EDI], $0 //lisää nolla
          
POP ESI
@TotalEnd:POP EDI
          POP EDX
          POP EAX
          POP ECX
@Final:   RET


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners