strings.pp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. { Implemented in System Unit }
  16. function strpas(p:pchar):shortstring;external name 'FPC_PCHAR_TO_SHORTSTR';
  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;
  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;
  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. { Copies l characters from source to dest, returns dest. }
  42. function strmove(dest,source : pchar;l : SizeInt) : pchar;
  43. { Appends at most l characters from source to dest }
  44. function strlcat(dest,source : pchar;l : SizeInt) : 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 : SizeInt) : SizeInt;
  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 : SizeInt) : pchar;
  64. { Releases a null-terminated string from the heap }
  65. procedure strdispose(p : pchar);
  66. implementation
  67. {$ifdef FPC_USE_LIBC}
  68. {$i cgenstr.inc}
  69. {$endif FPC_USE_LIBC}
  70. { Read Processor dependent part, shared with sysutils unit }
  71. {$i strings.inc }
  72. { Read processor denpendent part, NOT shared with sysutils unit }
  73. {$i stringss.inc }
  74. { Read generic string functions that are not implemented for the processor }
  75. {$i genstr.inc}
  76. {$i genstrs.inc}
  77. { Functions not in assembler, but shared with sysutils unit }
  78. {$i stringsi.inc}
  79. { Functions, different from the one in sysutils }
  80. function stralloc(L : SizeInt) : pchar;
  81. begin
  82. StrAlloc:=Nil;
  83. GetMem (Stralloc,l);
  84. end;
  85. function strnew(p : pchar) : pchar;
  86. var
  87. len : SizeInt;
  88. begin
  89. strnew:=nil;
  90. if (p=nil) or (p^=#0) then
  91. exit;
  92. len:=strlen(p)+1;
  93. getmem(strnew,len);
  94. if strnew<>nil then
  95. strmove(strnew,p,len);
  96. end;
  97. procedure strdispose(p : pchar);
  98. begin
  99. if p<>nil then
  100. begin
  101. freemem(p);
  102. p:=nil;
  103. end;
  104. end;
  105. end.
  106. {
  107. $Log$
  108. Revision 1.9 2004-11-21 15:35:23 peter
  109. * float routines all use internproc and compilerproc helpers
  110. Revision 1.8 2004/05/01 23:55:18 peter
  111. * replace strlenint with sizeint
  112. Revision 1.7 2004/05/01 15:26:33 jonas
  113. * use some more string routines from libc if FPC_USE_LIBC is used
  114. Revision 1.6 2004/02/18 22:00:59 peter
  115. * use SizeInt instead of longint
  116. Revision 1.5 2003/07/07 20:22:05 peter
  117. * generic string routines added
  118. Revision 1.4 2002/09/07 15:07:46 peter
  119. * old logs removed and tabs fixed
  120. }