IdStrings.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. {
  2. $Project$
  3. $Workfile$
  4. $Revision$
  5. $DateUTC$
  6. $Id$
  7. This file is part of the Indy (Internet Direct) project, and is offered
  8. under the dual-licensing agreement described on the Indy website.
  9. (http://www.indyproject.org/)
  10. Copyright:
  11. (c) 1993-2005, Chad Z. Hower and the Indy Pit Crew. All rights reserved.
  12. }
  13. {
  14. $Log$
  15. }
  16. {
  17. Rev 1.7 2/15/2005 9:25:00 AM DSiders
  18. Modified StrHtmlEncode, StrHtmlDecode to ignore apostrophe character and
  19. entity (not defined for HTML 4).
  20. Added StrXHtmlEncode, StrXHtmlDecode.
  21. Added comments to describe various functions.
  22. Rev 1.6 7/30/2004 7:49:30 AM JPMugaas
  23. Removed unneeded DotNET excludes.
  24. Rev 1.5 2004.02.03 5:44:26 PM czhower
  25. Name changes
  26. Rev 1.4 2004.02.03 2:12:20 PM czhower
  27. $I path change
  28. Rev 1.3 24/01/2004 19:30:28 CCostelloe
  29. Cleaned up warnings
  30. Rev 1.2 10/12/2003 2:01:48 PM BGooijen
  31. Compiles in DotNet
  32. Rev 1.1 10/10/2003 11:06:54 PM SPerry
  33. Rev 1.0 11/13/2002 08:02:02 AM JPMugaas
  34. 2000-03-27 Pete Mee
  35. - Added FindFirstOf, FindFirstNotOf,TrimAllOf functions.
  36. 2002-01-03 Andrew P.Rybin
  37. - StrHTMLEnc/Dec,BinToHexStr,IsWhiteString
  38. }
  39. unit IdStrings;
  40. interface
  41. {$i IdCompilerDefines.inc}
  42. {
  43. IsWhiteString
  44. Returns TRUE when AStr contains only whitespace characters
  45. TAB (decimal 9), SPACE (decimal 32), or an empty string.
  46. }
  47. function IsWhiteString(const AStr: String): Boolean;
  48. {
  49. BinToHexStr
  50. converts the byte value in AData to its representation as
  51. a 2-byte hexadecimal value without the '$' prefix.
  52. For instance: 'FF', 'FE', or '0A'
  53. }
  54. function BinToHexStr(AData: Byte): String;
  55. {
  56. Encode and decode characters representing pre-defined character
  57. entities for HTML 4.
  58. handles &<>" characters
  59. }
  60. function StrHtmlEncode (const AStr: String): String;
  61. function StrHtmlDecode (const AStr: String): String;
  62. {
  63. Encode and decode characters representing pre-defined character
  64. entities for XHTML, XML.
  65. handles &<>"' characters
  66. }
  67. function StrXHtmlEncode(const ASource: String): String;
  68. function StrXHtmlDecode(const ASource: String): String;
  69. {
  70. SplitString splits a string into left and right parts,
  71. i.e. SplitString('Namespace:tag', ':'..) will return 'Namespace' and 'tag'
  72. }
  73. procedure SplitString(const AStr, AToken: String; var VLeft, VRight: String);
  74. {
  75. CommaAdd
  76. Appends AStr2 to the right of AStr1 and returns the result.
  77. If there is any content in AStr1, a comma will be appended
  78. prior to the value of AStr2.
  79. }
  80. function CommaAdd(Const AStr1, AStr2:String):string;
  81. implementation
  82. uses
  83. SysUtils,
  84. IdGlobal,
  85. IdGlobalProtocols;
  86. function StrHtmlEncode (const AStr: String): String;
  87. begin
  88. // TODO: use StringsReplace() instead
  89. {
  90. Result := StringsReplace(AStr,
  91. ['&', '<', '>', '"'], {do not localize
  92. ['&amp;', '&lt;', '&gt;', '&quot;'] {do not localize
  93. );
  94. }
  95. Result := ReplaceAll(AStr, '&', '&amp;'); {do not localize}
  96. Result := ReplaceAll(Result, '<', '&lt;'); {do not localize}
  97. Result := ReplaceAll(Result, '>', '&gt;'); {do not localize}
  98. Result := ReplaceAll(Result, '"', '&quot;'); {do not localize}
  99. end;
  100. function StrHtmlDecode (const AStr: String): String;
  101. begin
  102. // TODO: use StringsReplace() instead
  103. {
  104. Result := StringsReplace(AStr,
  105. ['&quot;', '&gt;', '&lt;', '&amp;'], {do not localize
  106. ['"', '>', '<', '&'] {do not localize
  107. );
  108. }
  109. Result := ReplaceAll(AStr, '&quot;', '"'); {do not localize}
  110. Result := ReplaceAll(Result, '&gt;', '>'); {do not localize}
  111. Result := ReplaceAll(Result, '&lt;', '<'); {do not localize}
  112. Result := ReplaceAll(Result, '&amp;', '&'); {do not localize}
  113. end;
  114. function StrXHtmlEncode(const ASource: String): String;
  115. begin
  116. //TODO: use StringsReplace() instead
  117. {
  118. Result := StringsReplace(ASource,
  119. ['&', '<', '>', '"', ''''], {do not localize
  120. ['&amp;', '&lt;', '&gt;', '&quot;', '&apos;'] {do not localize
  121. );
  122. }
  123. Result := ReplaceAll(ASource, '&', '&amp;'); {do not localize}
  124. Result := ReplaceAll(Result, '<', '&lt;'); {do not localize}
  125. Result := ReplaceAll(Result, '>', '&gt;'); {do not localize}
  126. Result := ReplaceAll(Result, '"', '&quot;'); {do not localize}
  127. Result := ReplaceAll(Result, '''', '&apos;'); {do not localize}
  128. end;
  129. function StrXHtmlDecode(const ASource: String): String;
  130. begin
  131. // TODO: use StringsReplace() instead
  132. {
  133. Result := StringsReplace(ASource,
  134. ['&apos;', '&quot;', '&gt;', '&lt;', '&amp;'], {do not localize
  135. ['''', '"', '>', '<', '&'] {do not localize
  136. );
  137. }
  138. Result := ReplaceAll(ASource, '&apos;', ''''); {do not localize}
  139. Result := ReplaceAll(Result, '&quot;', '"'); {do not localize}
  140. Result := ReplaceAll(Result, '&gt;', '>'); {do not localize}
  141. Result := ReplaceAll(Result, '&lt;', '<'); {do not localize}
  142. Result := ReplaceAll(Result, '&amp;', '&'); {do not localize}
  143. end;
  144. // SP - 10/10/2003
  145. function BinToHexStr(AData: Byte): String;
  146. begin
  147. Result := IdHexDigits[AData shr 4] + IdHexDigits[AData and $F];
  148. end;
  149. function IsWhiteString(const AStr: String): Boolean;
  150. var
  151. i: Integer;
  152. LLen: Integer;
  153. begin
  154. Result := True;
  155. LLen := Length(AStr);
  156. if LLen > 0 then
  157. begin
  158. for i := 1 to LLen do
  159. begin
  160. if not CharIsInSet(AStr, i, LWS) then
  161. begin
  162. Result := FALSE;
  163. Break;
  164. end;
  165. end;
  166. end;
  167. end;
  168. procedure SplitString(const AStr, AToken: String; var VLeft, VRight: String);
  169. var
  170. i: Integer;
  171. LLocalStr: String;
  172. begin
  173. { It is possible that VLeft or VRight may be the same variable as AStr.
  174. So we copy it first }
  175. LLocalStr := AStr;
  176. i := Pos(AToken, LLocalStr);
  177. if i = 0 then
  178. begin
  179. VLeft := LLocalStr;
  180. VRight := '';
  181. end
  182. else
  183. begin
  184. VLeft := Copy(LLocalStr, 1, i - 1);
  185. VRight := Copy(LLocalStr, i + Length(AToken), Length(LLocalStr));
  186. end;
  187. end;
  188. function CommaAdd(Const AStr1, AStr2:String):string;
  189. begin
  190. if AStr1 = '' then
  191. result := AStr2
  192. else
  193. result := AStr1 + ',' + AStr2;
  194. end;
  195. end.