home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vp21beta.zip / ARTLSRC.RAR / STRINGS.PAS < prev    next >
Pascal/Delphi Source File  |  2000-08-15  |  17KB  |  535 lines

  1. //█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
  2. //█                                                       █
  3. //█      Virtual Pascal Runtime Library.  Version 2.1.    █
  4. //█      String Handling Unit (ASCIIZ)                    █
  5. //█      ─────────────────────────────────────────────────█
  6. //█      Copyright (C) 1995-2000 vpascal.com              █
  7. //█                                                       █
  8. //▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  9.  
  10. {$S-,R-,Q-,I-,Cdecl-,OrgName-,AlignRec-,H-,Use32+}
  11.  
  12. unit Strings;
  13.  
  14. interface
  15.  
  16. function StrLen(Str: PChar): Word;
  17. function StrEnd(Str: PChar): PChar;
  18. function StrMove(Dest, Source: PChar; Count: Word): PChar;
  19. function StrCopy(Dest, Source: PChar): PChar;
  20. function StrECopy(Dest, Source: PChar): PChar;
  21. function StrLCopy(Dest, Source: PChar; MaxLen: Word): PChar;
  22. function StrPCopy(Dest: PChar; Source: String): PChar;
  23. function StrCat(Dest, Source: PChar): PChar;
  24. function StrLCat(Dest, Source: PChar; MaxLen: Word): PChar;
  25. function StrComp(Str1, Str2: PChar): Integer;
  26. function StrIComp(Str1, Str2: PChar): Integer;
  27. function StrLComp(Str1, Str2: PChar; MaxLen: Word): Integer;
  28. function StrLIComp(Str1, Str2: PChar; MaxLen: Word): Integer;
  29. function StrScan(Str: PChar; Chr: Char): PChar;
  30. function StrRScan(Str: PChar; Chr: Char): PChar;
  31. function StrPos(Str1, Str2: PChar): PChar;
  32. function StrUpper(Str: PChar): PChar;
  33. function StrLower(Str: PChar): PChar;
  34. function StrPas(Str: PChar): String;
  35. function StrNew(Str: PChar): PChar;
  36. procedure StrDispose(Str: PChar);
  37.  
  38. implementation
  39.  
  40. { Returns the number of characters in Str, not counting the null        }
  41. { terminator.                                                           }
  42.  
  43. function StrLen(Str: PChar): Word; assembler; {$USES edi} {$FRAME-}
  44. asm
  45.                 cld
  46.                 mov     edi,Str
  47.                 or      ecx,-1
  48.                 xor     eax,eax
  49.                 repne   scasb
  50.                 sub     eax,ecx
  51.                 sub     eax,2
  52. end;
  53.  
  54. { Returns a pointer to the null character that terminates Str.          }
  55.  
  56. function StrEnd(Str: PChar): PChar; assembler; {$USES edi} {$FRAME-}
  57. asm
  58.                 cld
  59.                 mov     edi,Str
  60.                 or      ecx,-1
  61.                 xor     al,al
  62.                 repne   scasb
  63.                 lea     eax,[edi-1]
  64. end;
  65.  
  66. { Copies exactly Count characters from Source to Dest and returns Dest. }
  67. { Source and Dest may overlap.                                          }
  68.  
  69. function StrMove(Dest, Source: PChar; Count: Word): PChar; assembler; {$USES esi,edi} {$FRAME-}
  70. asm
  71.                 mov     esi,Source
  72.                 mov     edi,Dest
  73.                 mov     edx,edi
  74.                 mov     ecx,Count
  75.                 cmp     esi,edi
  76.                 jae     @@1
  77.                 std
  78.                 add     esi,ecx
  79.                 add     edi,ecx
  80.                 mov     eax,ecx
  81.                 and     ecx,11b
  82.                 shr     eax,2
  83.                 dec     esi
  84.                 dec     edi
  85.                 rep     movsb
  86.                 mov     ecx,eax
  87.                 sub     esi,3
  88.                 sub     edi,3
  89.                 rep     movsd
  90.                 cld
  91.                 jmp     @@2
  92.               @@1:
  93.                 cld
  94.                 mov     eax,ecx
  95.                 shr     ecx,2
  96.                 and     al,11b
  97.                 rep     movsd
  98.                 mov     cl,al
  99.                 rep     movsb
  100.               @@2:
  101.                 mov     eax,edx
  102. end;
  103.  
  104. { Copies Source to Dest and returns Dest.                               }
  105.  
  106. function StrCopy(Dest, Source: PChar): PChar; assembler; {$USES esi,edi} {$FRAME-}
  107. asm
  108.                 cld
  109.                 mov     edi,Source
  110.                 mov     esi,edi
  111.                 xor     al,al
  112.                 or      ecx,-1
  113.                 repne   scasb
  114.                 not     ecx
  115.                 mov     dl,cl
  116.                 mov     edi,Dest
  117.                 mov     eax,edi
  118.                 shr     ecx,2
  119.                 and     dl,11b
  120.                 rep     movsd
  121.                 mov     cl,dl
  122.                 rep     movsb
  123. end;
  124.  
  125. { Copies Source to Dest and returns StrEnd(Dest).                       }
  126.  
  127. function StrECopy(Dest, Source: PChar): PChar; assembler; {$USES esi,edi} {$FRAME-}
  128. asm
  129.                 cld
  130.                 mov     edi,Source
  131.                 mov     esi,edi
  132.                 xor     al,al
  133.                 or      ecx,-1
  134.                 repne   scasb
  135.                 not     ecx
  136.                 mov     al,cl
  137.                 mov     edi,Dest
  138.                 shr     ecx,2
  139.                 and     al,11b
  140.                 rep     movsd
  141.                 mov     cl,al
  142.                 rep     movsb
  143.                 lea     eax,[edi-1]
  144. end;
  145.  
  146. { Copies at most MaxLen characters from Source to Dest and returns Dest.}
  147.  
  148. function StrLCopy(Dest, Source: PChar; MaxLen: Word): PChar; assembler; {$USES esi,edi} {$FRAME-}
  149. asm
  150.                 cld
  151.                 mov     edi,Source
  152.                 mov     esi,edi
  153.                 mov     ecx,MaxLen
  154.                 mov     eax,Dest
  155. //                mov     Byte Ptr [eax],0
  156.                 jecxz   @@RET
  157.                 mov     edx,ecx
  158.                 xor     al,al
  159.                 repne   scasb
  160.                 sub     edx,ecx
  161.                 mov     ecx,edx
  162.                 mov     edi,Dest
  163.                 mov     eax,edi
  164.                 shr     ecx,2
  165.                 and     dl,11b
  166.                 rep     movsd
  167.                 mov     cl,dl
  168.                 rep     movsb
  169.                 mov     [edi].Byte,0
  170.               @@RET:
  171. end;
  172.  
  173. { Copies the Pascal style string Source into Dest and returns Dest.     }
  174.  
  175. function StrPCopy(Dest: PChar; Source: String): PChar; assembler; {$USES esi,edi} {$FRAME-}
  176. asm
  177.                 cld
  178.                 mov     esi,Source
  179.                 mov     edi,Dest
  180.                 mov     eax,edi
  181.                 xor     ecx,ecx
  182.                 mov     cl,[esi]
  183.                 inc     esi
  184.                 mov     dl,cl
  185.                 shr     ecx,2
  186.                 and     dl,11b
  187.                 rep     movsd
  188.                 mov     cl,dl
  189.                 rep     movsb
  190.                 mov     [edi].Byte,0
  191. end;
  192.  
  193. { Appends a copy of Source to the end of Dest and returns Dest.         }
  194.  
  195. function StrCat(Dest, Source: PChar): PChar; assembler; {$USES None} {$FRAME+}
  196. asm
  197.                 push    Dest
  198.                 Call    StrEnd
  199.                 push    eax
  200.                 push    Source
  201.                 Call    StrCopy
  202.                 mov     eax,Dest
  203. end;
  204.  
  205. { Appends at most MaxLen - StrLen(Dest) characters from Source to the   }
  206. { end of Dest, and returns Dest.                                        }
  207.  
  208. function StrLCat(Dest, Source: PChar; MaxLen: Word): PChar; assembler; {$USES None} {$FRAME+}
  209. asm
  210.                 push    Dest
  211.                 Call    StrEnd
  212.                 mov     ecx,Dest
  213.                 add     ecx,MaxLen
  214.                 sub     ecx,eax
  215.                 jbe     @@1
  216.                 push    eax
  217.                 push    Source
  218.                 push    ecx
  219.                 Call    StrLCopy
  220.               @@1:
  221.                 mov     eax,Dest
  222. end;
  223.  
  224. { Compares Str1 to Str2. The return value is less than 0 if Str1 < Str2,}
  225. { 0 if Str1 = Str2, or greater than 0 if Str1 > Str2.                   }
  226.  
  227. function StrComp(Str1, Str2: PChar): Integer; assembler; {$USES esi,edi} {$FRAME-}
  228. asm
  229.                 cld
  230.                 mov     edi,Str2
  231.                 mov     esi,edi
  232.                 or      ecx,-1
  233.                 xor     eax,eax
  234.                 xor     edx,edx
  235.                 repne   scasb
  236.                 not     ecx
  237.                 mov     edi,esi
  238.                 mov     esi,Str1
  239.                 repe    cmpsb
  240.                 mov     al,[esi-1]
  241.                 mov     dl,[edi-1]
  242.                 sub     eax,edx
  243. end;
  244.  
  245. { Compares Str1 to Str2, without case sensitivity. The return value is  }
  246. { the same as StrComp.                                                  }
  247.  
  248. function StrIComp(Str1, Str2: PChar): Integer; assembler; {$USES esi,edi} {$FRAME-}
  249. asm
  250.                 cld
  251.                 mov     edi,Str2
  252.                 mov     esi,edi
  253.                 or      ecx,-1
  254.                 xor     eax,eax
  255.                 xor     edx,edx
  256.                 repne   scasb
  257.                 not     ecx
  258.                 mov     edi,esi
  259.                 mov     esi,Str1
  260.               @@1:
  261.                 repe    cmpsb
  262.                 je      @@4
  263.                 mov     al,[esi-1]
  264.                 cmp     al,'a'
  265.                 jb      @@2
  266.                 cmp     al,'z'
  267.                 ja      @@2
  268.                 sub     al,'a'-'A'
  269.               @@2:
  270.                 mov     dl,[edi-1]
  271.                 cmp     dl,'a'
  272.                 jb      @@3
  273.                 cmp     dl,'z'
  274.                 ja      @@3
  275.                 sub     dl,'a'-'A'
  276.               @@3:
  277.                 sub     eax,edx
  278.                 je      @@1
  279.               @@4:
  280. end;
  281.  
  282. { Compares Str1 to Str2, for a maximum length of MaxLen characters. The }
  283. { return value is the same as StrComp.                                  }
  284.  
  285. function StrLComp(Str1, Str2: PChar; MaxLen: Word): Integer; assembler; {$USES esi,edi} {$FRAME-}
  286. asm
  287.                 cld
  288.                 mov     edi,Str2
  289.                 mov     esi,edi
  290.                 mov     eax,MaxLen
  291.                 mov     ecx,eax
  292.                 jecxz   @@1
  293.                 mov     edx,eax
  294.                 xor     eax,eax
  295.                 repne   scasb
  296.                 sub     edx,ecx
  297.                 mov     ecx,edx
  298.                 mov     edi,esi
  299.                 mov     esi,Str1
  300.                 repe    cmpsb
  301.                 xor     edx,edx
  302.                 mov     al,[esi-1]
  303.                 mov     dl,[edi-1]
  304.                 sub     eax,edx
  305.               @@1:
  306. end;
  307.  
  308. { Compares Str1 to Str2, for a maximum length of MaxLen characters,     }
  309. { without case sensitivity. The return value is the same as StrComp.    }
  310.  
  311. function StrLIComp(Str1, Str2: PChar; MaxLen: Word): Integer; assembler; {$USES esi,edi} {$FRAME-}
  312. asm
  313.                 mov     edi,Str2
  314.                 mov     esi,edi
  315.                 mov     eax,MaxLen
  316.                 mov     ecx,eax
  317.                 jecxz   @@4
  318.                 cld
  319.                 mov     edx,eax
  320.                 xor     eax,eax
  321.                 repne   scasb
  322.                 sub     edx,ecx
  323.                 mov     ecx,edx
  324.                 mov     edi,esi
  325.                 mov     esi,Str1
  326.                 xor     edx,edx
  327.               @@1:
  328.                 repe    cmpsb
  329.                 je      @@4
  330.                 mov     al,[esi-1]
  331.                 cmp     al,'a'
  332.                 jb      @@2
  333.                 cmp     al,'z'
  334.                 ja      @@2
  335.                 sub     al,'a'-'A'
  336.               @@2:
  337.                 mov     dl,[edi-1]
  338.                 cmp     dl,'a'
  339.                 jb      @@3
  340.                 cmp     dl,'z'
  341.                 ja      @@3
  342.                 sub     dl,'a'-'A'
  343.               @@3:
  344.                 sub     eax,edx
  345.                 je      @@1
  346.               @@4:
  347. end;
  348.  
  349. { Returns a pointer to the first occurrence of Chr in Str. If Chr does  }
  350. { not occur in Str, StrScan returns NIL. The null terminator is         }
  351. { considered to be part of the string.                                  }
  352.  
  353. function StrScan(Str: PChar; Chr: Char): PChar; assembler; {$USES edi} {$FRAME-}
  354. asm
  355.                 cld
  356.                 mov     edi,Str
  357.                 mov     edx,edi
  358.                 or      ecx,-1
  359.                 xor     eax,eax
  360.                 repne   scasb
  361.                 not     ecx
  362.                 mov     edi,edx
  363.                 mov     al,Chr
  364.                 repne   scasb
  365.                 mov     al,0
  366.                 jne     @@1
  367.                 lea     eax,[edi-1]
  368.               @@1:
  369. end;
  370.  
  371. { Returns a pointer to the last occurrence of Chr in Str. If Chr does   }
  372. { not occur in Str, StrRScan returns NIL. The null terminator is        }
  373. { considered to be part of the string.                                  }
  374.  
  375. function StrRScan(Str: PChar; Chr: Char): PChar; assembler; {$USES edi} {$FRAME-}
  376. asm
  377.                 cld
  378.                 mov     edi,Str
  379.                 or      ecx,-1
  380.                 xor     eax,eax
  381.                 repne   scasb
  382.                 not     ecx
  383.                 std
  384.                 dec     edi
  385.                 mov     al,Chr
  386.                 repne   scasb
  387.                 mov     al,0
  388.                 jne     @@1
  389.                 lea     eax,[edi+1]
  390.               @@1:
  391.                 cld
  392. end;
  393.  
  394. { Returns a pointer to the first occurrence of Str2 in Str1. If Str2    }
  395. { does not occur in Str1, StrPos returns NIL.                           }
  396.  
  397. function StrPos(Str1, Str2: PChar): PChar; assembler; {$USES ebx,esi,edi} {$FRAME-}
  398. asm
  399.                 cld
  400.                 xor     al,al
  401.                 mov     edi,Str2
  402.                 or      ecx,-1
  403.                 repne   scasb
  404.                 not     ecx
  405.                 dec     ecx
  406.                 je      @@2
  407.                 mov     edx,ecx
  408.                 mov     edi,Str1
  409.                 mov     ebx,edi
  410.                 or      ecx,-1
  411.                 repne   scasb
  412.                 not     ecx
  413.                 sub     ecx,edx
  414.                 jbe     @@2
  415.                 mov     edi,ebx
  416.               @@1:
  417.                 mov     esi,Str2
  418.                 lodsb
  419.                 repne   scasb
  420.                 jne     @@2
  421.                 mov     eax,ecx
  422.                 mov     ebx,edi
  423.                 mov     ecx,edx
  424.                 dec     ecx
  425.                 repe    cmpsb
  426.                 mov     ecx,eax
  427.                 mov     edi,ebx
  428.                 jne     @@1
  429.                 lea     eax,[edi-1]
  430.                 jmp     @@3
  431.               @@2:
  432.                 xor     eax,eax
  433.               @@3:
  434. end;
  435.  
  436. { Converts Str to upper case and returns Str.                           }
  437.  
  438. function StrUpper(Str: PChar): PChar; assembler; {$USES esi} {$FRAME-}
  439. asm
  440.                 cld
  441.                 mov     esi,Str
  442.                 mov     eax,esi
  443.               @@1:
  444.                 mov     dl,[esi]
  445.                 test    dl,dl
  446.                 jz      @@2
  447.                 inc     esi
  448.                 cmp     dl,'a'
  449.                 jb      @@1
  450.                 cmp     dl,'z'
  451.                 ja      @@1
  452.                 sub     dl,'a'-'A'
  453.                 mov     [esi-1],dl
  454.                 jmp     @@1
  455.               @@2:
  456. end;
  457.  
  458. { Converts Str to lower case and returns Str.                           }
  459.  
  460. function StrLower(Str: PChar): PChar; assembler; {$USES esi} {$FRAME-}
  461. asm
  462.                 cld
  463.                 mov     esi,Str
  464.                 mov     eax,esi
  465.               @@1:
  466.                 mov     dl,[esi]
  467.                 test    dl,dl
  468.                 jz      @@2
  469.                 inc     esi
  470.                 cmp     dl,'A'
  471.                 jb      @@1
  472.                 cmp     dl,'Z'
  473.                 ja      @@1
  474.                 add     dl,'a'-'A'
  475.                 mov     [esi-1],dl
  476.                 jmp     @@1
  477.               @@2:
  478. end;
  479.  
  480. { StrPas converts Str to a Pascal style string.                         }
  481.  
  482. function StrPas(Str: PChar): String; assembler; {$USES esi,edi} {$FRAME-}
  483. asm
  484.                 cld
  485.                 mov     edi,Str
  486.                 or      ecx,-1
  487.                 xor     al,al
  488.                 repne   scasb
  489.                 not     ecx
  490.                 dec     ecx
  491.                 cmp     ecx,255
  492.                 jbe     @@1
  493.                 mov     ecx,255
  494.               @@1:
  495.                 mov     esi,Str
  496.                 mov     edi,@Result
  497.                 mov     al,cl
  498.                 stosb
  499.                 shr     ecx,2
  500.                 and     al,11b
  501.                 rep     movsd
  502.                 mov     cl,al
  503.                 rep     movsb
  504. end;
  505.  
  506. { Allocates a copy of Str on the heap. If Str is NIL or points to an    }
  507. { empty string, StrNew returns NIL and doesn't allocate any heap space. }
  508. { Otherwise, StrNew makes a duplicate of Str, obtaining space with a    }
  509. { call to the GetMem standard procedure, and returns a pointer to the   }
  510. { duplicated string. The allocated space is StrLen(Str) + 1 bytes long. }
  511.  
  512. function StrNew(Str: PChar): PChar;
  513. var
  514.   L: Word;
  515.   P: PChar;
  516. begin
  517.   StrNew := nil;
  518.   if (Str <> nil) and (Str^ <> #0) then
  519.   begin
  520.     L := StrLen(Str) + 1;
  521.     GetMem(P, L);
  522.     if P <> nil then StrNew := StrMove(P, Str, L);
  523.   end;
  524. end;
  525.  
  526. { Disposes a string that was previously allocated with StrNew. If Str   }
  527. { is NIL, StrDispose does nothing.                                      }
  528.  
  529. procedure StrDispose(Str: PChar);
  530. begin
  531.   if Str <> nil then FreeMem(Str, StrLen(Str) + 1);
  532. end;
  533.  
  534. end.
  535.