| 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 |
|
|---|
| 14 | const
|
|---|
| 15 | StrCR = chr(13);
|
|---|
| 16 | StrLF = chr(10);
|
|---|
| 17 | StrCRLF = StrCR + StrLF;
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | TYPE
|
|---|
| 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 |
|
|---|
| 35 | TYPE
|
|---|
| 36 | TSetOfChars = set of char;
|
|---|
| 37 |
|
|---|
| 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
|
|---|
| 40 | Function StrEscapeAllCharsBy(Const aReceiver: String; const aSetOfChars: TSetOfChars; const anEscapeChar: char): String;
|
|---|
| 41 |
|
|---|
| 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 |
|
|---|
| 64 | Function BoolToStr(const aBoolean : boolean ): string;
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | Implementation
|
|---|
| 68 |
|
|---|
| 69 | uses
|
|---|
| 70 | DebugUnit;
|
|---|
| 71 |
|
|---|
| 72 | constructor TSerializableStringList.Create;
|
|---|
| 73 | begin
|
|---|
| 74 | LogEvent(LogObjConstDest, 'TSerializableStringList createdestroy');
|
|---|
| 75 |
|
|---|
| 76 | inherited Create;
|
|---|
| 77 | stringList := TStringList.Create;
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | destructor TSerializableStringList.Destroy;
|
|---|
| 82 | begin
|
|---|
| 83 | LogEvent(LogObjConstDest, 'TSerializableStringList destroy');
|
|---|
| 84 | if Nil <> stringList then stringList.Destroy;
|
|---|
| 85 |
|
|---|
| 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 + '&';
|
|---|
| 114 | result := result + StrEscapeAllCharsBy(stringList[i], ['&'], '\');
|
|---|
| 115 | end;
|
|---|
| 116 | end;
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | PROCEDURE TSerializableStringList.readValuesFromSerializedString(const aSerializedString : String);
|
|---|
| 120 | Begin
|
|---|
| 121 | if (length(aSerializedString) < 1) then exit;
|
|---|
| 122 |
|
|---|
| 123 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString');
|
|---|
| 124 | stringList.Destroy;
|
|---|
| 125 | LogEvent(LogObjConstDest, 'readValuesFromSerializedString destroy done');
|
|---|
| 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;
|
|---|
| 135 | Var
|
|---|
| 136 | i : Integer;
|
|---|
| 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;
|
|---|
| 156 | tmpChar,tmpNextChar : Char;
|
|---|
| 157 | tmpPart: String;
|
|---|
| 158 | Begin
|
|---|
| 159 | if (length(aReceiver) < 1) then exit;
|
|---|
| 160 |
|
|---|
| 161 | tmpPart := '';
|
|---|
| 162 |
|
|---|
| 163 | i := 1;
|
|---|
| 164 | while i <= length(aReceiver) do
|
|---|
| 165 | begin
|
|---|
| 166 | tmpChar := aReceiver[i];
|
|---|
| 167 | if i < length(aReceiver) then
|
|---|
| 168 | tmpNextChar := aReceiver[i+1]
|
|---|
| 169 | else
|
|---|
| 170 | tmpNextChar := #0;
|
|---|
| 171 |
|
|---|
| 172 | if (tmpChar = anEscapeChar) and (tmpNextChar = anEscapeChar) then
|
|---|
| 173 | begin
|
|---|
| 174 | tmpPart := tmpPart + anEscapeChar;
|
|---|
| 175 | i := i + 2;
|
|---|
| 176 | end
|
|---|
| 177 | else
|
|---|
| 178 | if (tmpChar = anEscapeChar) and (tmpNextChar IN aSetOfChars) then
|
|---|
| 179 | begin
|
|---|
| 180 | tmpPart := tmpPart + tmpNextChar;
|
|---|
| 181 | i := i + 2;
|
|---|
| 182 | end
|
|---|
| 183 | else
|
|---|
| 184 | if (tmpChar IN aSetOfChars) then
|
|---|
| 185 | begin
|
|---|
| 186 | aResult.add(tmpPart);
|
|---|
| 187 | tmpPart := '';
|
|---|
| 188 | i := i + 1;
|
|---|
| 189 | end
|
|---|
| 190 | else
|
|---|
| 191 | begin
|
|---|
| 192 | tmpPart := tmpPart + tmpChar;
|
|---|
| 193 | i := i + 1;
|
|---|
| 194 | end;
|
|---|
| 195 | end;
|
|---|
| 196 | aResult.add(tmpPart);
|
|---|
| 197 | end;
|
|---|
| 198 |
|
|---|
| 199 |
|
|---|
| 200 | Function StrTrimChars(const aReceiver: String; const aSetOfChars: TSetOfChars): String;
|
|---|
| 201 | Var
|
|---|
| 202 | i : Longint;
|
|---|
| 203 | j : Longint;
|
|---|
| 204 | Begin
|
|---|
| 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;
|
|---|
| 213 |
|
|---|
| 214 | j := Length(aReceiver);
|
|---|
| 215 | while j >= i do
|
|---|
| 216 | begin
|
|---|
| 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
|
|---|
| 250 | begin
|
|---|
| 251 | result := false;
|
|---|
| 252 | exit;
|
|---|
| 253 | end;
|
|---|
| 254 | dec(tmpMatchPos);
|
|---|
| 255 | dec(tmpStringPos);
|
|---|
| 256 | end;
|
|---|
| 257 |
|
|---|
| 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;
|
|---|
| 282 | end;
|
|---|
| 283 | dec(tmpMatchPos);
|
|---|
| 284 | dec(tmpStringPos);
|
|---|
| 285 | end;
|
|---|
| 286 |
|
|---|
| 287 | result := true;
|
|---|
| 288 | end;
|
|---|
| 289 |
|
|---|
| 290 | Function BoolToStr(const aBoolean : boolean ): string;
|
|---|
| 291 | begin
|
|---|
| 292 | if aBoolean then
|
|---|
| 293 | Result := 'True'
|
|---|
| 294 | else
|
|---|
| 295 | Result := 'False';
|
|---|
| 296 | end;
|
|---|
| 297 |
|
|---|
| 298 | END.
|
|---|