| [35] | 1 | Unit StringUtilsUnit; | 
|---|
|  | 2 |  | 
|---|
|  | 3 | // NewView - a new OS/2 Help Viewer | 
|---|
| [75] | 4 | // Copyright 2006-2007 Ronald Brill (rbri at rbri dot de) | 
|---|
|  | 5 | // This software is released under the GNU Public License - see readme.txt | 
|---|
| [35] | 6 |  | 
|---|
|  | 7 | // Helper functions to work with strings | 
|---|
|  | 8 |  | 
|---|
|  | 9 | Interface | 
|---|
|  | 10 |  | 
|---|
|  | 11 | uses | 
|---|
|  | 12 | Classes; | 
|---|
|  | 13 |  | 
|---|
| [43] | 14 | const | 
|---|
|  | 15 | StrCR = chr(13); | 
|---|
|  | 16 | StrLF = chr(10); | 
|---|
|  | 17 | StrCRLF = StrCR + StrLF; | 
|---|
|  | 18 |  | 
|---|
|  | 19 |  | 
|---|
|  | 20 | TYPE | 
|---|
| [35] | 21 | TSerializableStringList = class | 
|---|
|  | 22 | private | 
|---|
|  | 23 | stringList : TStringList; | 
|---|
|  | 24 |  | 
|---|
|  | 25 | public | 
|---|
|  | 26 | CONSTRUCTOR Create; | 
|---|
|  | 27 | DESTRUCTOR Destroy; override; | 
|---|
|  | 28 | FUNCTION getCount : LongInt; | 
|---|
|  | 29 | PROCEDURE add(const aString : String); | 
|---|
|  | 30 | FUNCTION get(const anIndex : LongInt) : String; | 
|---|
|  | 31 | FUNCTION getSerializedString : String; | 
|---|
|  | 32 | PROCEDURE readValuesFromSerializedString(const aSerializedString : String); | 
|---|
|  | 33 | end; | 
|---|
|  | 34 |  | 
|---|
| [43] | 35 | TYPE | 
|---|
|  | 36 | TSetOfChars = set of char; | 
|---|
|  | 37 |  | 
|---|
| [35] | 38 | // prefices all occurences of one of the chars in aStringWithChars with anEscape char | 
|---|
|  | 39 | // if the escapeChar itself is found, then it is doubled | 
|---|
| [43] | 40 | Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String; | 
|---|
| [35] | 41 |  | 
|---|
| [43] | 42 | // Extract all fields in a String given a set of delimiter characters and | 
|---|
|  | 43 | // an optional escape character usable to escape field delimits. | 
|---|
|  | 44 | // Example: | 
|---|
|  | 45 | //     StrExtractStrings('1x2x3\x4', "x", '\') -> | 
|---|
|  | 46 | //     returns 4 strings: "1", "", "2" and "3x4" | 
|---|
|  | 47 | Procedure StrExtractStrings(Var aResult : TStringList; Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char); | 
|---|
|  | 48 |  | 
|---|
|  | 49 | // removes all occurences of char from aSetOfChars from the beginning | 
|---|
|  | 50 | // end the end of a String. | 
|---|
|  | 51 | Function StrTrimChars(const aReceiver: String; const aSetOfChars: TSetOfChars): String; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | // removes all blanks from beginning and end | 
|---|
|  | 54 | Function StrTrim(const aReceiver: String): String; | 
|---|
|  | 55 |  | 
|---|
|  | 56 | // returns true if the String ends with the provides one | 
|---|
|  | 57 | // this is case SENSITIVE | 
|---|
|  | 58 | Function StrEndsWith(const aReceiver: String; const anEndString: String): Boolean; | 
|---|
|  | 59 |  | 
|---|
|  | 60 | // returns true if the String ends with the provides one | 
|---|
|  | 61 | // this is case INsensitive | 
|---|
|  | 62 | Function StrEndsWithIgnoringCase(const aString: String; const anEndString: String): Boolean; | 
|---|
|  | 63 |  | 
|---|
| [68] | 64 | // the IntToStr generates wrong results | 
|---|
|  | 65 | // in normal cases IntToStr returns a negative value | 
|---|
|  | 66 | // and somtimes completly wrong values | 
|---|
|  | 67 | Function LongWordToStr(const aLongWord: LongWord) : String; | 
|---|
|  | 68 |  | 
|---|
| [65] | 69 | Function BoolToStr(const aBoolean : boolean ): string; | 
|---|
| [43] | 70 |  | 
|---|
| [65] | 71 |  | 
|---|
| [35] | 72 | Implementation | 
|---|
|  | 73 |  | 
|---|
| [65] | 74 | uses | 
|---|
|  | 75 | DebugUnit; | 
|---|
|  | 76 |  | 
|---|
| [35] | 77 | constructor TSerializableStringList.Create; | 
|---|
|  | 78 | begin | 
|---|
| [65] | 79 | LogEvent(LogObjConstDest, 'TSerializableStringList createdestroy'); | 
|---|
|  | 80 |  | 
|---|
| [35] | 81 | inherited Create; | 
|---|
|  | 82 | stringList := TStringList.Create; | 
|---|
|  | 83 | end; | 
|---|
|  | 84 |  | 
|---|
|  | 85 |  | 
|---|
|  | 86 | destructor TSerializableStringList.Destroy; | 
|---|
|  | 87 | begin | 
|---|
| [65] | 88 | LogEvent(LogObjConstDest, 'TSerializableStringList destroy'); | 
|---|
|  | 89 | if Nil <> stringList then stringList.Destroy; | 
|---|
|  | 90 |  | 
|---|
| [35] | 91 | inherited Destroy; | 
|---|
|  | 92 | end; | 
|---|
|  | 93 |  | 
|---|
|  | 94 |  | 
|---|
|  | 95 | FUNCTION TSerializableStringList.getCount : LongInt; | 
|---|
|  | 96 | begin | 
|---|
|  | 97 | result := stringList.count; | 
|---|
|  | 98 | end; | 
|---|
|  | 99 |  | 
|---|
|  | 100 |  | 
|---|
|  | 101 | PROCEDURE TSerializableStringList.add(const aString : String); | 
|---|
|  | 102 | begin | 
|---|
|  | 103 | stringList.add(aString); | 
|---|
|  | 104 | end; | 
|---|
|  | 105 |  | 
|---|
|  | 106 | FUNCTION TSerializableStringList.get(const anIndex : LongInt) : String; | 
|---|
|  | 107 | begin | 
|---|
|  | 108 | result := stringList[anIndex]; | 
|---|
|  | 109 | end; | 
|---|
|  | 110 |  | 
|---|
|  | 111 | FUNCTION TSerializableStringList.getSerializedString : String; | 
|---|
|  | 112 | Var | 
|---|
|  | 113 | i : Integer; | 
|---|
|  | 114 | begin | 
|---|
|  | 115 | result := ''; | 
|---|
|  | 116 | for i := 0 To stringList.count-1 do | 
|---|
|  | 117 | begin | 
|---|
|  | 118 | if (i > 0) then result := result + '&'; | 
|---|
| [43] | 119 | result := result + StrEscapeAllCharsBy(stringList[i], ['&'], '\'); | 
|---|
| [35] | 120 | end; | 
|---|
|  | 121 | end; | 
|---|
|  | 122 |  | 
|---|
|  | 123 |  | 
|---|
|  | 124 | PROCEDURE TSerializableStringList.readValuesFromSerializedString(const aSerializedString : String); | 
|---|
| [43] | 125 | Begin | 
|---|
|  | 126 | if (length(aSerializedString) < 1) then exit; | 
|---|
|  | 127 |  | 
|---|
| [65] | 128 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString'); | 
|---|
|  | 129 | stringList.Destroy; | 
|---|
|  | 130 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString destroy done'); | 
|---|
| [43] | 131 | stringList := TStringList.Create; | 
|---|
|  | 132 | StrExtractStrings(stringList, aSerializedString, ['&'], '\'); | 
|---|
|  | 133 | end; | 
|---|
|  | 134 |  | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // ---------------------------------------------------------- | 
|---|
|  | 137 |  | 
|---|
|  | 138 |  | 
|---|
|  | 139 | Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String; | 
|---|
| [35] | 140 | Var | 
|---|
|  | 141 | i : Integer; | 
|---|
| [43] | 142 | tmpChar : Char; | 
|---|
|  | 143 | Begin | 
|---|
|  | 144 | Result := ''; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | for i := 1 To length(aReceiver) do | 
|---|
|  | 147 | begin | 
|---|
|  | 148 | tmpChar := aReceiver[i]; | 
|---|
|  | 149 |  | 
|---|
|  | 150 | if (tmpChar = anEscapeChar) or (tmpChar IN aSetOfChars) then | 
|---|
|  | 151 | result := result + anEscapeChar + tmpChar | 
|---|
|  | 152 | else | 
|---|
|  | 153 | result := result + tmpChar; | 
|---|
|  | 154 | end; | 
|---|
|  | 155 | end; | 
|---|
|  | 156 |  | 
|---|
|  | 157 |  | 
|---|
|  | 158 | Procedure StrExtractStrings(Var aResult: TStringList; Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char); | 
|---|
|  | 159 | Var | 
|---|
|  | 160 | i : Integer; | 
|---|
| [35] | 161 | tmpChar,tmpNextChar : Char; | 
|---|
|  | 162 | tmpPart: String; | 
|---|
| [43] | 163 | Begin | 
|---|
|  | 164 | if (length(aReceiver) < 1) then exit; | 
|---|
| [35] | 165 |  | 
|---|
|  | 166 | tmpPart := ''; | 
|---|
|  | 167 |  | 
|---|
|  | 168 | i := 1; | 
|---|
| [43] | 169 | while i <= length(aReceiver) do | 
|---|
| [35] | 170 | begin | 
|---|
| [43] | 171 | tmpChar := aReceiver[i]; | 
|---|
|  | 172 | if i < length(aReceiver) then | 
|---|
|  | 173 | tmpNextChar := aReceiver[i+1] | 
|---|
| [35] | 174 | else | 
|---|
|  | 175 | tmpNextChar := #0; | 
|---|
|  | 176 |  | 
|---|
| [43] | 177 | if (tmpChar = anEscapeChar) and (tmpNextChar = anEscapeChar) then | 
|---|
| [35] | 178 | begin | 
|---|
| [43] | 179 | tmpPart := tmpPart + anEscapeChar; | 
|---|
| [35] | 180 | i := i + 2; | 
|---|
|  | 181 | end | 
|---|
|  | 182 | else | 
|---|
| [43] | 183 | if (tmpChar = anEscapeChar) and (tmpNextChar IN aSetOfChars) then | 
|---|
| [35] | 184 | begin | 
|---|
| [43] | 185 | tmpPart := tmpPart + tmpNextChar; | 
|---|
| [35] | 186 | i := i + 2; | 
|---|
|  | 187 | end | 
|---|
|  | 188 | else | 
|---|
| [43] | 189 | if (tmpChar IN aSetOfChars) then | 
|---|
| [35] | 190 | begin | 
|---|
| [43] | 191 | aResult.add(tmpPart); | 
|---|
| [35] | 192 | tmpPart := ''; | 
|---|
|  | 193 | i := i + 1; | 
|---|
|  | 194 | end | 
|---|
|  | 195 | else | 
|---|
|  | 196 | begin | 
|---|
|  | 197 | tmpPart := tmpPart + tmpChar; | 
|---|
|  | 198 | i := i + 1; | 
|---|
|  | 199 | end; | 
|---|
|  | 200 | end; | 
|---|
| [43] | 201 | aResult.add(tmpPart); | 
|---|
| [35] | 202 | end; | 
|---|
|  | 203 |  | 
|---|
|  | 204 |  | 
|---|
| [43] | 205 | Function StrTrimChars(const aReceiver: String; const aSetOfChars: TSetOfChars): String; | 
|---|
| [35] | 206 | Var | 
|---|
| [43] | 207 | i : Longint; | 
|---|
|  | 208 | j : Longint; | 
|---|
| [35] | 209 | Begin | 
|---|
| [43] | 210 | i := 1; | 
|---|
|  | 211 | while i < Length(aReceiver) do | 
|---|
|  | 212 | begin | 
|---|
|  | 213 | if aReceiver[i] in aSetOfChars then | 
|---|
|  | 214 | inc(i) | 
|---|
|  | 215 | else | 
|---|
|  | 216 | break; | 
|---|
|  | 217 | end; | 
|---|
| [35] | 218 |  | 
|---|
| [43] | 219 | j := Length(aReceiver); | 
|---|
|  | 220 | while j >= i do | 
|---|
| [35] | 221 | begin | 
|---|
| [43] | 222 | if aReceiver[j] in aSetOfChars then | 
|---|
|  | 223 | dec(j) | 
|---|
|  | 224 | else | 
|---|
|  | 225 | break; | 
|---|
|  | 226 | end; | 
|---|
|  | 227 |  | 
|---|
|  | 228 | result := Copy(aReceiver, i, j-i+1); | 
|---|
|  | 229 | end; | 
|---|
|  | 230 |  | 
|---|
|  | 231 |  | 
|---|
|  | 232 | Function StrTrim(const aReceiver: String): String; | 
|---|
|  | 233 | Begin | 
|---|
|  | 234 | result := StrTrimChars(aReceiver, [' ']); | 
|---|
|  | 235 | end; | 
|---|
|  | 236 |  | 
|---|
|  | 237 |  | 
|---|
|  | 238 | Function StrEndsWith(const aReceiver: String; const anEndString: String): Boolean; | 
|---|
|  | 239 | Var | 
|---|
|  | 240 | tmpStringPos : Longint; | 
|---|
|  | 241 | tmpMatchPos : Longint; | 
|---|
|  | 242 | Begin | 
|---|
|  | 243 | tmpStringPos := length(aReceiver); | 
|---|
|  | 244 | tmpMatchPos := length(anEndString); | 
|---|
|  | 245 |  | 
|---|
|  | 246 | if tmpMatchPos > tmpStringPos then | 
|---|
|  | 247 | begin | 
|---|
|  | 248 | result := false; | 
|---|
|  | 249 | exit; | 
|---|
|  | 250 | end; | 
|---|
|  | 251 |  | 
|---|
|  | 252 | while tmpMatchPos > 0 do | 
|---|
|  | 253 | begin | 
|---|
|  | 254 | if aReceiver[tmpStringPos] <> anEndString[tmpMatchPos] then | 
|---|
| [35] | 255 | begin | 
|---|
| [43] | 256 | result := false; | 
|---|
|  | 257 | exit; | 
|---|
|  | 258 | end; | 
|---|
|  | 259 | dec(tmpMatchPos); | 
|---|
|  | 260 | dec(tmpStringPos); | 
|---|
|  | 261 | end; | 
|---|
| [35] | 262 |  | 
|---|
| [43] | 263 | result := true; | 
|---|
|  | 264 | end; | 
|---|
|  | 265 |  | 
|---|
|  | 266 |  | 
|---|
|  | 267 | Function StrEndsWithIgnoringCase(const aReceiver: String; const anEndString: String): Boolean; | 
|---|
|  | 268 | Var | 
|---|
|  | 269 | tmpStringPos : Longint; | 
|---|
|  | 270 | tmpMatchPos : Longint; | 
|---|
|  | 271 | Begin | 
|---|
|  | 272 | tmpStringPos := length(aString); | 
|---|
|  | 273 | tmpMatchPos := length(anEndString); | 
|---|
|  | 274 |  | 
|---|
|  | 275 | if tmpMatchPos > tmpStringPos then | 
|---|
|  | 276 | begin | 
|---|
|  | 277 | result := false; | 
|---|
|  | 278 | exit; | 
|---|
|  | 279 | end; | 
|---|
|  | 280 |  | 
|---|
|  | 281 | while tmpMatchPos > 0 do | 
|---|
|  | 282 | begin | 
|---|
|  | 283 | if upcase(aString[tmpStringPos]) <> upcase(anEndString[tmpMatchPos]) then | 
|---|
|  | 284 | begin | 
|---|
|  | 285 | result := false; | 
|---|
|  | 286 | exit; | 
|---|
| [35] | 287 | end; | 
|---|
| [43] | 288 | dec(tmpMatchPos); | 
|---|
|  | 289 | dec(tmpStringPos); | 
|---|
| [35] | 290 | end; | 
|---|
| [43] | 291 |  | 
|---|
|  | 292 | result := true; | 
|---|
| [35] | 293 | end; | 
|---|
|  | 294 |  | 
|---|
| [68] | 295 | Function LongWordToStr(const aLongWord: LongWord) : String; | 
|---|
|  | 296 | Var | 
|---|
|  | 297 | l : LongWord; | 
|---|
|  | 298 | i : Integer; | 
|---|
|  | 299 | Begin | 
|---|
|  | 300 | Result := ''; | 
|---|
|  | 301 | l := aLongWord; | 
|---|
|  | 302 |  | 
|---|
|  | 303 | if l = 0 then | 
|---|
|  | 304 | begin | 
|---|
|  | 305 | result := '0'; | 
|---|
|  | 306 | exit; | 
|---|
|  | 307 | end; | 
|---|
|  | 308 |  | 
|---|
|  | 309 | while l > 0 do | 
|---|
|  | 310 | begin | 
|---|
|  | 311 | i := l mod 10; | 
|---|
|  | 312 | l := l div 10; | 
|---|
|  | 313 | Case i of | 
|---|
|  | 314 | 0 : result := '0' + result; | 
|---|
|  | 315 | 1 : result := '1' + result; | 
|---|
|  | 316 | 2 : result := '2' + result; | 
|---|
|  | 317 | 3 : result := '3' + result; | 
|---|
|  | 318 | 4 : result := '4' + result; | 
|---|
|  | 319 | 5 : result := '5' + result; | 
|---|
|  | 320 | 6 : result := '6' + result; | 
|---|
|  | 321 | 7 : result := '7' + result; | 
|---|
|  | 322 | 8 : result := '8' + result; | 
|---|
|  | 323 | 9 : result := '9' + result; | 
|---|
|  | 324 | end; | 
|---|
|  | 325 | end; | 
|---|
|  | 326 |  | 
|---|
|  | 327 | end; | 
|---|
|  | 328 |  | 
|---|
|  | 329 |  | 
|---|
| [65] | 330 | Function BoolToStr(const aBoolean : boolean ): string; | 
|---|
|  | 331 | begin | 
|---|
|  | 332 | if aBoolean then | 
|---|
|  | 333 | Result := 'True' | 
|---|
|  | 334 | else | 
|---|
|  | 335 | Result := 'False'; | 
|---|
|  | 336 | end; | 
|---|
|  | 337 |  | 
|---|
| [35] | 338 | END. | 
|---|