strings.pp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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;
  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. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  81. { also define alias which can be used inside the system unit }
  82. function fpc_pchar_to_shortstr(p:pchar):shortstring;[external name 'FPC_PCHAR_TO_SHORTSTR'];
  83. {$else FPC_STRTOSHORTSTRINGPROC}
  84. { also define alias which can be used inside the system unit }
  85. procedure fpc_pchar_to_shortstr(var res : openstring;p:pchar);[external name 'FPC_PCHAR_TO_SHORTSTR'];
  86. {$endif FPC_STRTOSHORTSTRINGPROC}
  87. function strpas(p:pchar):shortstring;{$ifdef SYSTEMINLINE}inline;{$endif}
  88. begin
  89. {$ifndef FPC_STRTOSHORTSTRINGPROC}
  90. strpas:=fpc_pchar_to_shortstr(p);
  91. {$else FPC_STRTOSHORTSTRINGPROC}
  92. fpc_pchar_to_shortstr(strpas,p);
  93. {$endif FPC_STRTOSHORTSTRINGPROC}
  94. end;
  95. function stralloc(L : SizeInt) : pchar;
  96. begin
  97. StrAlloc:=Nil;
  98. GetMem (Stralloc,l);
  99. end;
  100. function strnew(p : pchar) : pchar;
  101. var
  102. len : SizeInt;
  103. begin
  104. strnew:=nil;
  105. if (p=nil) or (p^=#0) then
  106. exit;
  107. len:=strlen(p)+1;
  108. getmem(strnew,len);
  109. if strnew<>nil then
  110. strmove(strnew,p,len);
  111. end;
  112. procedure strdispose(p : pchar);
  113. begin
  114. if p<>nil then
  115. begin
  116. freemem(p);
  117. p:=nil;
  118. end;
  119. end;
  120. end.