1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
{ * -----------------------------------------------------------------------------
la fonction Explode retourne un tableau de chaînes. Ce sont les sous-chaînes, extraites de S, en utilisant le séparateur Separator.
@param S Chaine à découper
@param A Tableau de Chaine qui recevra la découpe
@param Separators Caractères qui délimitent une chaine pour la découpe
@param ExcludeEmpty Si True, les Chaines vides ne sont pas insérés dans le Tableau
@param Quotes Caractères qui délimitent une chaine pour la découpe contenant des Separators, n'importe quel séparateur peut commencer et terminé une chaine, une quote doublée est considéré comme valeur un quote dans la chaine
@param KeepSeparators Si True, A contient les chaines et les séparateurs mais pas les Quotes, sinon (par défaut) A ne contient que les Chaines.
@param KeepTextAfterQuote Si True, le Texte entre une quote terminale d'un texte et un séparateur est conservée, la Quote comprise, sinon (par défaut) le Texte entre une quote terminale d'un texte et un séparateur est ignorées
@return Nombre de Séparateur Trouvé (peut-être différent du nombre de chaine dans A !)
------------------------------------------------------------------------------ }
function Explode(const S: string; out A: Types.TStringDynArray;
const Separators: string; ExcludeEmpty: Boolean = False;
const Quotes: string = ''; KeepSeparators: Boolean = False;
KeepTextAfterQuote: Boolean = False): Integer;
var
iLesSep: Integer;
iLesQuote: Integer;
function IsSeparator(c: Char): Integer;
begin
for Result := 1 to iLesSep do
if c = Separators[Result] then
Exit;
Result := -1;
end;
function IsQuote(c: Char): Integer;
begin
for Result := 1 to iLesQuote do
if c = Quotes[Result] then
Exit;
Result := -1;
end;
var
iStr: Integer;
iQuote: Integer;
iLenS: Integer;
iLenSS: Integer;
iLenA: Integer;
iAdded: Integer;
iBegin: Integer;
Quoted: Boolean;
DoubleQuoted: Boolean;
AlreadyDQ: Boolean;
QuoteConcat: string;
iOffQuote: Integer;
LastIsSep: Boolean;
LastIsQ: Boolean;
ContinueToNextSeparator: Boolean;
begin
iLenS := Length(S);
iLesSep := Length(Separators);
if (iLenS = 0) or (iLesSep = 0) then
begin
SetLength(A, 1);
Result := 0;
A[Result] := '';
Exit;
end;
iLesQuote := Length(Quotes);
for iQuote := 1 to iLesQuote do
if IsSeparator(Quotes[iQuote]) > 0 then
raise EParserError.CreateFmt
('le Délimiteur "%s" ne peut pas être un Séparateur !',
[Quotes[iQuote]]);
Result := 0;
iQuote := 0;
for iStr := 1 to Length(S) do
begin
if IsSeparator(S[iStr]) > 0 then
inc(Result)
else if IsQuote(S[iStr]) > 0 then
inc(iQuote);
end;
if Odd(iQuote) then
raise EParserError.CreateFmt('Nombre de Délimiteur Incorrect : "%d" !',
[iQuote]);
LastIsSep := IsSeparator(S[iLenS]) > 0;
LastIsQ := IsQuote(S[iLenS]) > 0;
if KeepSeparators then
iLenA := Result * 2 + 1
else
iLenA := Result + 1;
SetLength(A, iLenA);
iLenSS := 0;
iAdded := 0;
Quoted := False;
iOffQuote := 0;
QuoteConcat := '';
AlreadyDQ := False;
iBegin := 1;
if IsSeparator(S[1]) > 0 then
begin
if KeepSeparators then
begin
iBegin := 2;
A[iAdded] := S[1];
inc(iAdded);
end;
end;
ContinueToNextSeparator := False;
for iStr := iBegin to iLenS do
begin
if ContinueToNextSeparator and (IsSeparator(S[iStr]) <= 0) then
Continue;
ContinueToNextSeparator := False;
if not Quoted and (IsSeparator(S[iStr]) > 0) then
begin
if ExcludeEmpty and (iLenSS = 0) then
begin
if KeepSeparators then
begin
A[iAdded] := S[iStr];
inc(iAdded);
end;
iBegin := iStr + 1;
end
else
begin
if AlreadyDQ then
A[iAdded] := QuoteConcat
else
A[iAdded] := Copy(S, iBegin, iLenSS);
AlreadyDQ := False;
inc(iAdded);
if KeepSeparators and (iBegin > 0) then
begin
A[iAdded] := S[iStr];
inc(iAdded);
end
else
begin
if LastIsSep and KeepSeparators and (iStr = iLenS) then
begin
A[iAdded] := S[iStr];
inc(iAdded);
end;
end;
iBegin := iStr + 1;
iLenSS := 0;
end;
end
else
begin
if IsQuote(S[iStr]) > 0 then
begin
if Quoted then
begin
Quoted := False;
if iStr < iLenS then
begin
DoubleQuoted := IsQuote(S[iStr + 1]) > 0;
if not KeepTextAfterQuote and not DoubleQuoted and
(IsSeparator(S[iStr + 1]) <= 0) then
begin
ContinueToNextSeparator := True;
iQuote := iStr;
end;
if AlreadyDQ then
QuoteConcat := QuoteConcat + Copy(S, iBegin, iLenSS) +
IfThen(DoubleQuoted, S[iStr + 1], '')
else
QuoteConcat := Copy(S, iBegin, iLenSS) +
IfThen(DoubleQuoted, S[iStr + 1], '');
AlreadyDQ := AlreadyDQ or DoubleQuoted;
end;
end
else
begin
Quoted := True;
iBegin := iStr + 1;
iLenSS := 0;
end;
end
else
begin
if Quoted and (IsSeparator(S[iStr]) > 0) then
inc(iOffQuote);
inc(iLenSS);
end;
end;
end;
if iBegin <= iLenS then
begin
if ContinueToNextSeparator then
A[iAdded] := Copy(S, iBegin, iQuote - iBegin)
else if LastIsQ then
A[iAdded] := Copy(S, iBegin, iLenS - iBegin)
else
A[iAdded] := Copy(S, iBegin, MaxInt);
inc(iAdded);
if LastIsSep and KeepSeparators then
begin
A[iAdded] := S[iLenS];
inc(iAdded);
end;
end;
if LastIsSep and not ExcludeEmpty then
inc(iAdded);
if iAdded < iLenA then
A := Copy(A, 0, iAdded);
Result := Result - iOffQuote;
end; |
Partager