strings.pp 4.8 KB

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