strings.pp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1993,97 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. interface
  14. { Returns the length of a string }
  15. function strlen(p : pchar) : longint;
  16. { Converts a Pascal string to a null-terminated string }
  17. function strpcopy(d : pchar;const s : string) : pchar;
  18. { Converts a null-terminated string to a Pascal string }
  19. function strpas(p : pchar) : string;
  20. { Copies source to dest, returns a pointer to dest }
  21. function strcopy(dest,source : pchar) : pchar;
  22. { Copies at most maxlen bytes from source to dest. }
  23. { Returns a pointer to dest }
  24. function strlcopy(dest,source : pchar;maxlen : longint) : pchar;
  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) : longint;
  37. { The same as strcomp, but at most l characters are compared }
  38. function strlcomp(str1,str2 : pchar;l : longint) : longint;
  39. { The same as strcomp but case insensitive }
  40. function stricomp(str1,str2 : pchar) : longint;
  41. { Copies l characters from source to dest, returns dest. }
  42. function strmove(dest,source : pchar;l : longint) : pchar;
  43. { Appends at most l characters from source to dest }
  44. function strlcat(dest,source : pchar;l : longint) : pchar;
  45. { Returns a pointer to the first occurrence of c in p }
  46. { If c doesn't occur, nil is returned }
  47. function strscan(p : pchar;c : char) : pchar;
  48. { Returns a pointer to the last occurrence of c in p }
  49. { If c doesn't occur, nil is returned }
  50. function strrscan(p : pchar;c : char) : pchar;
  51. { converts p to all-lowercase, returns p }
  52. function strlower(p : pchar) : pchar;
  53. { converts p to all-uppercase, returns p }
  54. function strupper(p : pchar) : pchar;
  55. { The same al stricomp, but at most l characters are compared }
  56. function strlicomp(str1,str2 : pchar;l : longint) : longint;
  57. { Returns a pointer to the first occurrence of str2 in }
  58. { str2 Otherwise returns nil }
  59. function strpos(str1,str2 : pchar) : pchar;
  60. { Makes a copy of p on the heap, and returns a pointer to this copy }
  61. function strnew(p : pchar) : pchar;
  62. { Allocates L bytes on the heap, returns a pchar pointer to it }
  63. function stralloc(L : longint) : pchar;
  64. { Releases a null-terminated string from the heap }
  65. procedure strdispose(p : pchar);
  66. implementation
  67. { Read Processor dependent part, shared with sysutils unit }
  68. {$i strings.inc }
  69. { Read processor denpendent part, NOT shared with sysutils unit }
  70. {$i stringss.inc }
  71. { Functions not in assembler, but shared with sysutils unit }
  72. {$i stringsi.inc}
  73. { Functions, different from the one in sysutils }
  74. function stralloc(L : longint) : pchar;
  75. begin
  76. StrAlloc:=Nil;
  77. GetMem (Stralloc,l);
  78. end;
  79. function strnew(p : pchar) : pchar;
  80. var
  81. len : longint;
  82. begin
  83. strnew:=nil;
  84. if (p=nil) or (p^=#0) then
  85. exit;
  86. len:=strlen(p)+1;
  87. getmem(strnew,len);
  88. if strnew<>nil then
  89. strmove(strnew,p,len);
  90. end;
  91. procedure strdispose(p : pchar);
  92. begin
  93. if p<>nil then
  94. freemem(p,strlen(p)+1);
  95. end;
  96. end.
  97. {
  98. $Log$
  99. Revision 1.2 1999-12-10 15:02:12 peter
  100. * strnew is ofcourse also different between sysutils and strings, just
  101. like stralloc/strdispose.
  102. Revision 1.1 1999/02/25 07:42:03 michael
  103. * Joined strings and sysutils
  104. Revision 1.7 1998/08/05 08:59:53 michael
  105. reverted to non-assmebler version, florians fix is applied.
  106. Revision 1.4 1998/05/31 14:15:52 peter
  107. * force to use ATT or direct parsing
  108. Revision 1.3 1998/05/30 14:30:22 peter
  109. * force att reading
  110. Revision 1.2 1998/05/23 01:14:06 peter
  111. + I386_ATT switch
  112. }