[35] | 1 | Unit StringUtilsUnit;
|
---|
| 2 |
|
---|
| 3 | // NewView - a new OS/2 Help Viewer
|
---|
| 4 | // Copyright 2006 Ronald Brill (rbri at rbri dot de)
|
---|
| 5 | // This software is released under the Gnu Public License - see readme.txt
|
---|
| 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 |
|
---|
[65] | 64 | Function BoolToStr(const aBoolean : boolean ): string;
|
---|
[43] | 65 |
|
---|
[65] | 66 |
|
---|
[35] | 67 | Implementation
|
---|
| 68 |
|
---|
[65] | 69 | uses
|
---|
| 70 | DebugUnit;
|
---|
| 71 |
|
---|
[35] | 72 | constructor TSerializableStringList.Create;
|
---|
| 73 | begin
|
---|
[65] | 74 | LogEvent(LogObjConstDest, 'TSerializableStringList createdestroy');
|
---|
| 75 |
|
---|
[35] | 76 | inherited Create;
|
---|
| 77 | stringList := TStringList.Create;
|
---|
| 78 | end;
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | destructor TSerializableStringList.Destroy;
|
---|
| 82 | begin
|
---|
[65] | 83 | LogEvent(LogObjConstDest, 'TSerializableStringList destroy');
|
---|
| 84 | if Nil <> stringList then stringList.Destroy;
|
---|
| 85 |
|
---|
[35] | 86 | inherited Destroy;
|
---|
| 87 | end;
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | FUNCTION TSerializableStringList.getCount : LongInt;
|
---|
| 91 | begin
|
---|
| 92 | result := stringList.count;
|
---|
| 93 | end;
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | PROCEDURE TSerializableStringList.add(const aString : String);
|
---|
| 97 | begin
|
---|
| 98 | stringList.add(aString);
|
---|
| 99 | end;
|
---|
| 100 |
|
---|
| 101 | FUNCTION TSerializableStringList.get(const anIndex : LongInt) : String;
|
---|
| 102 | begin
|
---|
| 103 | result := stringList[anIndex];
|
---|
| 104 | end;
|
---|
| 105 |
|
---|
| 106 | FUNCTION TSerializableStringList.getSerializedString : String;
|
---|
| 107 | Var
|
---|
| 108 | i : Integer;
|
---|
| 109 | begin
|
---|
| 110 | result := '';
|
---|
| 111 | for i := 0 To stringList.count-1 do
|
---|
| 112 | begin
|
---|
| 113 | if (i > 0) then result := result + '&';
|
---|
[43] | 114 | result := result + StrEscapeAllCharsBy(stringList[i], ['&'], '\');
|
---|
[35] | 115 | end;
|
---|
| 116 | end;
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | PROCEDURE TSerializableStringList.readValuesFromSerializedString(const aSerializedString : String);
|
---|
[43] | 120 | Begin
|
---|
| 121 | if (length(aSerializedString) < 1) then exit;
|
---|
| 122 |
|
---|
[65] | 123 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString');
|
---|
| 124 | stringList.Destroy;
|
---|
| 125 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString destroy done');
|
---|
[43] | 126 | stringList := TStringList.Create;
|
---|
| 127 | StrExtractStrings(stringList, aSerializedString, ['&'], '\');
|
---|
| 128 | end;
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | // ----------------------------------------------------------
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String;
|
---|
[35] | 135 | Var
|
---|
| 136 | i : Integer;
|
---|
[43] | 137 | tmpChar : Char;
|
---|
| 138 | Begin
|
---|
| 139 | Result := '';
|
---|
| 140 |
|
---|
| 141 | for i := 1 To length(aReceiver) do
|
---|
| 142 | begin
|
---|
| 143 | tmpChar := aReceiver[i];
|
---|
| 144 |
|
---|
| 145 | if (tmpChar = anEscapeChar) or (tmpChar IN aSetOfChars) then
|
---|
| 146 | result := result + anEscapeChar + tmpChar
|
---|
| 147 | else
|
---|
| 148 | result := result + tmpChar;
|
---|
| 149 | end;
|
---|
| 150 | end;
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | Procedure StrExtractStrings(Var aResult: TStringList; Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char);
|
---|
| 154 | Var
|
---|
| 155 | i : Integer;
|
---|
[35] | 156 | tmpChar,tmpNextChar : Char;
|
---|
| 157 | tmpPart: String;
|
---|
[43] | 158 | Begin
|
---|
| 159 | if (length(aReceiver) < 1) then exit;
|
---|
[35] | 160 |
|
---|
| 161 | tmpPart := '';
|
---|
| 162 |
|
---|
| 163 | i := 1;
|
---|
[43] | 164 | while i <= length(aReceiver) do
|
---|
[35] | 165 | begin
|
---|
[43] | 166 | tmpChar := aReceiver[i];
|
---|
| 167 | if i < length(aReceiver) then
|
---|
| 168 | tmpNextChar := aReceiver[i+1]
|
---|
[35] | 169 | else
|
---|
| 170 | tmpNextChar := #0;
|
---|
| 171 |
|
---|
[43] | 172 | if (tmpChar = anEscapeChar) and (tmpNextChar = anEscapeChar) then
|
---|
[35] | 173 | begin
|
---|
[43] | 174 | tmpPart := tmpPart + anEscapeChar;
|
---|
[35] | 175 | i := i + 2;
|
---|
| 176 | end
|
---|
| 177 | else
|
---|
[43] | 178 | if (tmpChar = anEscapeChar) and (tmpNextChar IN aSetOfChars) then
|
---|
[35] | 179 | begin
|
---|
[43] | 180 | tmpPart := tmpPart + tmpNextChar;
|
---|
[35] | 181 | i := i + 2;
|
---|
| 182 | end
|
---|
| 183 | else
|
---|
[43] | 184 | if (tmpChar IN aSetOfChars) then
|
---|
[35] | 185 | begin
|
---|
[43] | 186 | aResult.add(tmpPart);
|
---|
[35] | 187 | tmpPart := '';
|
---|
| 188 | i := i + 1;
|
---|
| 189 | end
|
---|
| 190 | else
|
---|
| 191 | begin
|
---|
| 192 | tmpPart := tmpPart + tmpChar;
|
---|
| 193 | i := i + 1;
|
---|
| 194 | end;
|
---|
| 195 | end;
|
---|
[43] | 196 | aResult.add(tmpPart);
|
---|
[35] | 197 | end;
|
---|
| 198 |
|
---|
| 199 |
|
---|
[43] | 200 | Function StrTrimChars(const aReceiver: String; const aSetOfChars: TSetOfChars): String;
|
---|
[35] | 201 | Var
|
---|
[43] | 202 | i : Longint;
|
---|
| 203 | j : Longint;
|
---|
[35] | 204 | Begin
|
---|
[43] | 205 | i := 1;
|
---|
| 206 | while i < Length(aReceiver) do
|
---|
| 207 | begin
|
---|
| 208 | if aReceiver[i] in aSetOfChars then
|
---|
| 209 | inc(i)
|
---|
| 210 | else
|
---|
| 211 | break;
|
---|
| 212 | end;
|
---|
[35] | 213 |
|
---|
[43] | 214 | j := Length(aReceiver);
|
---|
| 215 | while j >= i do
|
---|
[35] | 216 | begin
|
---|
[43] | 217 | if aReceiver[j] in aSetOfChars then
|
---|
| 218 | dec(j)
|
---|
| 219 | else
|
---|
| 220 | break;
|
---|
| 221 | end;
|
---|
| 222 |
|
---|
| 223 | result := Copy(aReceiver, i, j-i+1);
|
---|
| 224 | end;
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | Function StrTrim(const aReceiver: String): String;
|
---|
| 228 | Begin
|
---|
| 229 | result := StrTrimChars(aReceiver, [' ']);
|
---|
| 230 | end;
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | Function StrEndsWith(const aReceiver: String; const anEndString: String): Boolean;
|
---|
| 234 | Var
|
---|
| 235 | tmpStringPos : Longint;
|
---|
| 236 | tmpMatchPos : Longint;
|
---|
| 237 | Begin
|
---|
| 238 | tmpStringPos := length(aReceiver);
|
---|
| 239 | tmpMatchPos := length(anEndString);
|
---|
| 240 |
|
---|
| 241 | if tmpMatchPos > tmpStringPos then
|
---|
| 242 | begin
|
---|
| 243 | result := false;
|
---|
| 244 | exit;
|
---|
| 245 | end;
|
---|
| 246 |
|
---|
| 247 | while tmpMatchPos > 0 do
|
---|
| 248 | begin
|
---|
| 249 | if aReceiver[tmpStringPos] <> anEndString[tmpMatchPos] then
|
---|
[35] | 250 | begin
|
---|
[43] | 251 | result := false;
|
---|
| 252 | exit;
|
---|
| 253 | end;
|
---|
| 254 | dec(tmpMatchPos);
|
---|
| 255 | dec(tmpStringPos);
|
---|
| 256 | end;
|
---|
[35] | 257 |
|
---|
[43] | 258 | result := true;
|
---|
| 259 | end;
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 | Function StrEndsWithIgnoringCase(const aReceiver: String; const anEndString: String): Boolean;
|
---|
| 263 | Var
|
---|
| 264 | tmpStringPos : Longint;
|
---|
| 265 | tmpMatchPos : Longint;
|
---|
| 266 | Begin
|
---|
| 267 | tmpStringPos := length(aString);
|
---|
| 268 | tmpMatchPos := length(anEndString);
|
---|
| 269 |
|
---|
| 270 | if tmpMatchPos > tmpStringPos then
|
---|
| 271 | begin
|
---|
| 272 | result := false;
|
---|
| 273 | exit;
|
---|
| 274 | end;
|
---|
| 275 |
|
---|
| 276 | while tmpMatchPos > 0 do
|
---|
| 277 | begin
|
---|
| 278 | if upcase(aString[tmpStringPos]) <> upcase(anEndString[tmpMatchPos]) then
|
---|
| 279 | begin
|
---|
| 280 | result := false;
|
---|
| 281 | exit;
|
---|
[35] | 282 | end;
|
---|
[43] | 283 | dec(tmpMatchPos);
|
---|
| 284 | dec(tmpStringPos);
|
---|
[35] | 285 | end;
|
---|
[43] | 286 |
|
---|
| 287 | result := true;
|
---|
[35] | 288 | end;
|
---|
| 289 |
|
---|
[65] | 290 | Function BoolToStr(const aBoolean : boolean ): string;
|
---|
| 291 | begin
|
---|
| 292 | if aBoolean then
|
---|
| 293 | Result := 'True'
|
---|
| 294 | else
|
---|
| 295 | Result := 'False';
|
---|
| 296 | end;
|
---|
| 297 |
|
---|
[35] | 298 | END.
|
---|