| 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 | TYPE
|
|---|
| 15 | TSerializableStringList = class
|
|---|
| 16 | private
|
|---|
| 17 | stringList : TStringList;
|
|---|
| 18 |
|
|---|
| 19 | public
|
|---|
| 20 | CONSTRUCTOR Create;
|
|---|
| 21 | DESTRUCTOR Destroy; override;
|
|---|
| 22 | FUNCTION getCount : LongInt;
|
|---|
| 23 | PROCEDURE add(const aString : String);
|
|---|
| 24 | FUNCTION get(const anIndex : LongInt) : String;
|
|---|
| 25 | FUNCTION getSerializedString : String;
|
|---|
| 26 | PROCEDURE readValuesFromSerializedString(const aSerializedString : String);
|
|---|
| 27 | end;
|
|---|
| 28 |
|
|---|
| 29 | // prefices all occurences of one of the chars in aStringWithChars with anEscape char
|
|---|
| 30 | // if the escapeChar itself is found, then it is doubled
|
|---|
| 31 | Function escapeAllCharsBy(Const aReceiver: String; const aStringWithChars: String; const anEscapeChar: char): String;
|
|---|
| 32 |
|
|---|
| 33 | Implementation
|
|---|
| 34 |
|
|---|
| 35 | constructor TSerializableStringList.Create;
|
|---|
| 36 | begin
|
|---|
| 37 | inherited Create;
|
|---|
| 38 | stringList := TStringList.Create;
|
|---|
| 39 | end;
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | destructor TSerializableStringList.Destroy;
|
|---|
| 43 | begin
|
|---|
| 44 | stringList.Destroy;
|
|---|
| 45 | inherited Destroy;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | FUNCTION TSerializableStringList.getCount : LongInt;
|
|---|
| 50 | begin
|
|---|
| 51 | result := stringList.count;
|
|---|
| 52 | end;
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | PROCEDURE TSerializableStringList.add(const aString : String);
|
|---|
| 56 | begin
|
|---|
| 57 | stringList.add(aString);
|
|---|
| 58 | end;
|
|---|
| 59 |
|
|---|
| 60 | FUNCTION TSerializableStringList.get(const anIndex : LongInt) : String;
|
|---|
| 61 | begin
|
|---|
| 62 | result := stringList[anIndex];
|
|---|
| 63 | end;
|
|---|
| 64 |
|
|---|
| 65 | FUNCTION TSerializableStringList.getSerializedString : String;
|
|---|
| 66 | Var
|
|---|
| 67 | i : Integer;
|
|---|
| 68 | begin
|
|---|
| 69 | result := '';
|
|---|
| 70 | for i := 0 To stringList.count-1 do
|
|---|
| 71 | begin
|
|---|
| 72 | if (i > 0) then result := result + '&';
|
|---|
| 73 | result := result + escapeAllCharsBy(stringList[i], '&', '\');
|
|---|
| 74 | end;
|
|---|
| 75 | end;
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | PROCEDURE TSerializableStringList.readValuesFromSerializedString(const aSerializedString : String);
|
|---|
| 79 | Var
|
|---|
| 80 | i : Integer;
|
|---|
| 81 | tmpChar,tmpNextChar : Char;
|
|---|
| 82 | tmpPart: String;
|
|---|
| 83 | begin
|
|---|
| 84 | if (length(aSerializedString) < 1) then exit;
|
|---|
| 85 |
|
|---|
| 86 | stringList.destroy;
|
|---|
| 87 | stringList := TStringList.Create;
|
|---|
| 88 | tmpPart := '';
|
|---|
| 89 |
|
|---|
| 90 | i := 1;
|
|---|
| 91 | while i <= length(aSerializedString) do
|
|---|
| 92 | begin
|
|---|
| 93 | tmpChar := aSerializedString[i];
|
|---|
| 94 | if i < length(aSerializedString) then
|
|---|
| 95 | tmpNextChar := aSerializedString[i+1]
|
|---|
| 96 | else
|
|---|
| 97 | tmpNextChar := #0;
|
|---|
| 98 |
|
|---|
| 99 | if (tmpChar = '\') and (tmpNextChar = '\') then
|
|---|
| 100 | begin
|
|---|
| 101 | tmpPart := tmpPart + '\';
|
|---|
| 102 | i := i + 2;
|
|---|
| 103 | end
|
|---|
| 104 | else
|
|---|
| 105 | if (tmpChar = '\') and (tmpNextChar = '&') then
|
|---|
| 106 | begin
|
|---|
| 107 | tmpPart := tmpPart + '&';
|
|---|
| 108 | i := i + 2;
|
|---|
| 109 | end
|
|---|
| 110 | else
|
|---|
| 111 | if (tmpChar = '&') then
|
|---|
| 112 | begin
|
|---|
| 113 | stringList.add(tmpPart);
|
|---|
| 114 | tmpPart := '';
|
|---|
| 115 | i := i + 1;
|
|---|
| 116 | end
|
|---|
| 117 | else
|
|---|
| 118 | begin
|
|---|
| 119 | tmpPart := tmpPart + tmpChar;
|
|---|
| 120 | i := i + 1;
|
|---|
| 121 | end;
|
|---|
| 122 | end;
|
|---|
| 123 | stringList.add(tmpPart);
|
|---|
| 124 | end;
|
|---|
| 125 |
|
|---|
| 126 | // ----------------------------------------------------------
|
|---|
| 127 |
|
|---|
| 128 | Function escapeAllCharsBy(Const aReceiver: String; const aStringWithChars: String; const anEscapeChar: char): String;
|
|---|
| 129 | Var
|
|---|
| 130 | i : Integer;
|
|---|
| 131 | tmpChar : Char;
|
|---|
| 132 | Begin
|
|---|
| 133 | Result := '';
|
|---|
| 134 |
|
|---|
| 135 | if (length(aStringWithChars) > 0) then
|
|---|
| 136 | begin
|
|---|
| 137 | for i := 1 To length(aReceiver) do
|
|---|
| 138 | begin
|
|---|
| 139 | tmpChar := aReceiver[i];
|
|---|
| 140 |
|
|---|
| 141 | if ((tmpChar = anEscapeChar) or (pos(tmpChar, aStringWithChars) > 0)) then
|
|---|
| 142 | result := result + anEscapeChar + tmpChar
|
|---|
| 143 | else
|
|---|
| 144 | result := result + tmpChar;
|
|---|
| 145 | end;
|
|---|
| 146 | end;
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | END.
|
|---|