Browse Source

* patch by Bart B. to improve DelSpace1, resolves #39465
+ test

(cherry picked from commit 60c26a11ef481c924f2d52868e51a7a4c05878b7)

Florian Klämpfl 3 years ago
parent
commit
55d25eea07

+ 18 - 4
packages/rtl-objpas/src/inc/strutils.pp

@@ -2037,13 +2037,27 @@ end;
 function DelSpace1(const S: string): string;
 
 var
-  I : SizeInt;
+  I,J: SizeInt;
 
 begin
   Result:=S;
-  for i:=Length(Result) downto 2 do
-    if (Result[i]=' ') and (Result[I-1]=' ') then
-      Delete(Result,I,1);
+  I:=Length(Result);
+  While I>0 do
+    begin
+    if Result[I]=#32 then
+      begin
+      J:=I-1;
+      While (J>0) and (Result[J]=#32) do
+        Dec(j);
+      Inc(J);
+      if I<>J then
+        begin
+        Delete(Result,J+1,I-J);
+        I:=J+1;
+        end;
+      end;
+    dec(I);
+    end;
 end;
 
 function Tab2Space(const S: string; Numb: Byte): string;

+ 24 - 0
tests/test/packages/rtl-objpas/tdelspace1.pp

@@ -0,0 +1,24 @@
+uses
+  strutils;
+
+begin
+  if DelSpace1('asdf')<>'asdf' then
+    halt(1);
+  if DelSpace1(' asdf')<>' asdf' then
+    halt(2);
+  if DelSpace1('asdf ')<>'asdf ' then
+    halt(3);
+  if DelSpace1('  asdf')<>' asdf' then
+    halt(4);
+  if DelSpace1('asdf  ')<>'asdf ' then
+    halt(5);
+  if DelSpace1('as df')<>'as df' then
+    halt(6);
+  if DelSpace1('as  df')<>'as df' then
+    halt(7);
+  if DelSpace1(' a  s   d f')<>' a s d f' then
+    halt(8);
+  if DelSpace1('  a        b     c   ')<>' a b c ' then
+    halt(9);
+  writeln('ok')
+end.