strings.pp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by the Free Pascal development team.
  4. Strings unit for PChar (asciiz/C compatible strings) handling
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit Strings;
  12. {$S-}
  13. {$inline on}
  14. interface
  15. { Implemented in System Unit }
  16. function strpas(p:pchar):shortstring;inline;
  17. function strlen(p:pchar):sizeint;external name 'FPC_PCHAR_LENGTH';
  18. { Converts a Pascal string to a null-terminated string }
  19. function strpcopy(d : pchar;const s : string) : pchar;
  20. { Copies source to dest, returns a pointer to dest }
  21. function strcopy(dest,source : pchar) : pchar; overload;
  22. { Copies at most maxlen bytes from source to dest. }
  23. { Returns a pointer to dest }
  24. function strlcopy(dest,source : pchar;maxlen : SizeInt) : pchar; overload;
  25. { Copies source to dest and returns a pointer to the terminating }
  26. { null character. }
  27. function strecopy(dest,source : pchar) : pchar;
  28. { Returns a pointer tro the terminating null character of p }
  29. function strend(p : pchar) : pchar;
  30. { Appends source to dest, returns a pointer do dest}
  31. function strcat(dest,source : pchar) : pchar;
  32. { Compares str1 und str2, returns }
  33. { a value <0 if str1<str2; }
  34. { 0 when str1=str2 }
  35. { and a value >0 if str1>str2 }
  36. function strcomp(str1,str2 : pchar) : SizeInt;
  37. { The same as strcomp, but at most l characters are compared }
  38. function strlcomp(str1,str2 : pchar;l : SizeInt) : SizeInt;
  39. { The same as strcomp but case insensitive }
  40. function stricomp(str1,str2 : pchar) : SizeInt;
  41. { The same as stricomp, but at most l characters are compared }
  42. function strlicomp(str1,str2 : pchar;l : SizeInt) : SizeInt;
  43. { Copies l characters from source to dest, returns dest. }
  44. function strmove(dest,source : pchar;l : SizeInt) : pchar;
  45. { Appends at most l characters from source to dest }
  46. function strlcat(dest,source : pchar;l : SizeInt) : pchar;
  47. { Returns a pointer to the first occurrence of c in p }
  48. { If c doesn't occur, nil is returned }
  49. function strscan(p : pchar;c : char) : pchar;
  50. { The same as strscan but case insensitive }
  51. function striscan(p : pchar;c : char) : pchar;
  52. { Returns a pointer to the last occurrence of c in p }
  53. { If c doesn't occur, nil is returned }
  54. function strrscan(p : pchar;c : char) : pchar;
  55. { The same as strrscan but case insensitive }
  56. function strriscan(p : pchar;c : char) : pchar;
  57. { converts p to all-lowercase, returns p }
  58. function strlower(p : pchar) : pchar;
  59. { converts p to all-uppercase, returns p }
  60. function strupper(p : pchar) : pchar;
  61. { Returns a pointer to the first occurrence of str2 in }
  62. { str1 Otherwise returns nil }
  63. function strpos(str1,str2 : pchar) : pchar;
  64. { The same as strpos but case insensitive }
  65. function stripos(str1,str2 : pchar) : pchar;
  66. { Makes a copy of p on the heap, and returns a pointer to this copy }
  67. function strnew(p : pchar) : pchar;
  68. { Allocates L bytes on the heap, returns a pchar pointer to it }
  69. function stralloc(L : SizeInt) : pchar;
  70. { Releases a null-terminated string from the heap }
  71. procedure strdispose(p : pchar);
  72. implementation
  73. {$ifdef FPC_USE_LIBC}
  74. {$i cgenstr.inc}
  75. {$endif FPC_USE_LIBC}
  76. { Read Processor dependent part, shared with sysutils unit }
  77. {$i strings.inc }
  78. { Read processor denpendent part, NOT shared with sysutils unit }
  79. {$i stringss.inc }
  80. { Read generic string functions that are not implemented for the processor }
  81. {$i genstr.inc}
  82. {$i genstrs.inc}
  83. { Functions not in assembler, but shared with sysutils unit }
  84. {$i stringsi.inc}
  85. { Functions, different from the one in sysutils }
  86. procedure fpc_pchar_to_shortstr(var res : openstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  87. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  88. begin
  89. fpc_pchar_to_shortstr(strpas,p);
  90. end;
  91. function stralloc(L : SizeInt) : pchar;
  92. begin
  93. StrAlloc:=Nil;
  94. GetMem (Stralloc,l);
  95. end;
  96. function strnew(p : pchar) : pchar;
  97. var
  98. len : SizeInt;
  99. begin
  100. strnew:=nil;
  101. if (p=nil) or (p^=#0) then
  102. exit;
  103. len:=strlen(p)+1;
  104. getmem(strnew,len);
  105. if strnew<>nil then
  106. move(p^,strnew^,len);
  107. end;
  108. procedure strdispose(p : pchar);
  109. begin
  110. if p<>nil then
  111. freemem(p);
  112. end;
  113. end.