J'ai dérivé de la fonction Posex de Delphi une fonction PosExact qui renvoie vrai si la sous-chaine recherchée est à une place donnée.
par exemple la fonction PosExact('GET',Str1) va répondre True si Str1 commence par GET et false sinon.
la fontion PosExact(url,Str1,6) va répondre Vrai si l'url se trouve à la 6ème place dans un GET /url.

Cette fonction est particulièrement rapide puisqu'elle ne commence la recherche qu'à l'offset indiqué (1 par défaut) et cesse dès qu'il y a un écart : la chaine entière n'est pas parcourue en vain....

elle est très utile dans un serveur...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function PosExact(const SubStr, S: string; Offset: Integer = 1): Boolean;
asm
       test  eax, eax
       jz    @Nil
       test  edx, edx
       jz    @Nil
       dec   ecx
       jl    @Nil
 
       push  esi
       push  ebx
 
       mov   esi, [edx-4]  //Length(Str)
       mov   ebx, [eax-4]  //Length(Substr)
       sub   esi, ecx      //effective length of Str
       add   edx, ecx      //addr of the first char at starting position
       cmp   esi, ebx
       jl    @Past         //jump if EffectiveLength(Str)<Length(Substr)
       test  ebx, ebx
       jle   @Past         //jump if Length(Substr)<=0
 
       add   esp, -12
       add   ebx, -1       //Length(Substr)-1
       add   esi, edx      //addr of the terminator
       add   edx, ebx      //addr of the last char at starting position
       mov   [esp+8], esi  //save addr of the terminator
       add   eax, ebx      //addr of the last char of Substr
       sub   ecx, edx      //-@Str[Length(Substr)]
       neg   ebx           //-(Length(Substr)-1)
       mov   [esp+4], ecx  //save -@Str[Length(Substr)]
       mov   [esp], ebx    //save -(Length(Substr)-1)
       movzx ecx, byte ptr [eax] //the last char of Substr
 
@Loop:
       cmp   cl, [edx]
       jz    @Test0
       jmp    @Exit        //Le caractère est différent
@AfterTest0:
       cmp   cl, [edx+1]
       jz    @TestT
@AfterTestT:
       add   edx, 4
       cmp   edx, [esp+8]
       jb   @Continue
@EndLoop:
       add   edx, -2
       cmp   edx, [esp+8]
       jb    @Loop
@Exit:
       add   esp, 12
@Past:
       pop   ebx
       pop   esi
@Nil:
       xor   eax, eax
       ret
@Continue:
       cmp   cl, [edx-2]
       jz    @Test2
       cmp   cl, [edx-1]
       jnz   @Loop
@Test1:
       add   edx,  1
@Test2:
       add   edx, -2
@Test0:
       add   edx, -1
@TestT:                      //le premier caractère colle
       mov   esi, [esp]      //En reste-t-il d'autres  ?
       test  esi, esi
       jz    @Found
@String:                                //Commencer à explorer la suite
       movzx ebx, word ptr [esi+eax]
       cmp   bx, word ptr [esi+edx+1]   //mettre les caractères suivants dans Bx
       jnz   @exit
       cmp   esi, -2                    //reste caractères ?
       jge   @Found
       movzx ebx, word ptr [esi+eax+2]  //prendre deux autres caractères
       cmp   bx, word ptr [esi+edx+3]
       jnz   @exit                //si cela ne coincide pas aller vers un autre test
       add   esi, 4                     //decrémenter le reste à explorer
       jl    @String                    //si négatif il reste plus d'un caractères
@Found:
       mov   eax, [esp+4]
       add   edx, 2
 
       cmp   edx, [esp+8]
       ja    @Exit
 
       add   esp, 12                    //
       add   eax, edx                   //mettre la position dans eax
       pop   ebx
       pop   esi
end;