Recent

Author Topic: Trouble with DelSpace1 (Solved)  (Read 903 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Trouble with DelSpace1 (Solved)
« on: November 18, 2022, 01:32:26 am »
s2String =type TShiftState = set of (  ssShift      ssAlt      ssCtrl      ssLeft      ssRight
          ssMiddle      ssDouble      ssMeta      ssHyper   ssAltGr   ssCaps    ssNum
            ssScroll    ssTriple     ssQuad  ssExtra1    ssExtra2    );
 
  Notes: This type is used when describing a shortcut key or when describing what special keys are pressed on a keyboard when a key event is generated. The set contains the special keys that can be used in combination                    with a 'normal' key.



sCOPY ='*M1 uses ... classes; type TShiftState = set of (  ssShift      ssAlt      ssCtrl      ssLeft      ssRight    ssMiddle      ssDouble      ssMeta      ssHyper   ssAltGr   ssCaps    ssNum      ssScroll    ssTriple     ssQuad  ssExtra1    ssExtra2    ); Notes: This type is used when describing a shortcut key or when describing what special keys are pressed on a keyboard when a key event is generated. The set contains the special keys that can be used in combination with a ''normal'' key.'

The spaces inside the left '(' and right ')' are not set to one 1 space.

Code: Pascal  [Select][+][-]
  1.    s2String:=sCopy;
  2.    sCopy:=DelSpace1(s2String);
  3.  
« Last Edit: December 09, 2022, 08:29:14 pm by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 933
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Trouble with DelSpace1
« Reply #1 on: November 18, 2022, 04:02:54 am »
The spaces inside the left '(' and right ')' are not set to one 1 space.
Is it this you are asking for? Since I do not know what DelSpace1 method is, I wrote that:
Code: Pascal  [Select][+][-]
  1. function DelDoubleSpace(const AString: string): string;
  2. var
  3.   s: string;
  4.   p: Integer;
  5. begin
  6.   Result := AString;
  7.   if (Length(AString) < 1) then
  8.     Exit;
  9.   s := AString;
  10.   repeat
  11.     p := Pos('  ', s);
  12.     if (p > 0) then
  13.       Delete(s, p, 1);
  14.   until p = 0;
  15.   Result := s;
  16. end;
Hope that was it... untested but from theory it should eliminate all double spaces...
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

dje

  • Full Member
  • ***
  • Posts: 133
Re: Trouble with DelSpace1
« Reply #2 on: November 18, 2022, 04:16:25 am »
Maybe simplify your question. The following code removes all double spaces as described in the doc:
https://www.freepascal.org/docs-html/rtl/strutils/delspace1.html

Code: Pascal  [Select][+][-]
  1.   WriteLn('"', DelSpace1('    apple  orange    (     pear          )  '), '"');
The output is:
Code: Text  [Select][+][-]
  1. " apple orange ( pear ) "

Which looks correct to me. Are you expecting a different result?
« Last Edit: November 18, 2022, 04:18:53 am by dje »

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #3 on: November 18, 2022, 04:22:08 am »
function DelSpace1(  const S: string ): string;
 
  Notes: DelSpace1 returns a copy of S with all sequences of spaces reduced to 1 space.

I doubt DelSpace1 has a bug. After posting I now believe the blanks aren't spaces but tabs or something.

I'll try you're function bit suspect it won't work.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #4 on: November 18, 2022, 04:55:58 am »
Yea, they weren't spaces but control characters.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 933
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Trouble with DelSpace1(Solved)
« Reply #5 on: November 18, 2022, 12:32:21 pm »
Code: Pascal  [Select][+][-]
  1. function FilterAscii(const AString: string): string;
  2. var
  3.   i: Integer;
  4.   s: string;
  5. begin
  6.   s := '';
  7.   for i := 1 to Pred(Length(AString)) do
  8.     if  ((Ord(AString[i]) >= 32) and (Ord(AString[i]) < 127)) then
  9.       s := s + AString[i];
  10.   Result := s;
  11. end;
  12.  
  13. function DelDoubleSpace(const AString: string): string;
  14. var
  15.   s: string;
  16.   p: Integer;
  17. begin
  18.   Result := AString;
  19.   if (Length(AString) < 1) then
  20.     Exit;
  21.   s := AString;
  22.   repeat
  23.     p := Pos('  ', s);
  24.     if (p > 0) then
  25.       Delete(s, p, 1);
  26.   until p = 0;
  27.   Result := s;
  28. end;
  29.  
  30. procedure TForm1.Button1Click(Sender: TObject);
  31. begin
  32.   Memo1.Text := DelDoubleSpace(FilterAscii('insert your text here'));
  33. end;
Above would result in what you try to achieve.

Warning: Limited to ASCII
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

jamie

  • Hero Member
  • *****
  • Posts: 5069
Re: Trouble with DelSpace1(Solved)
« Reply #6 on: November 18, 2022, 01:46:05 pm »
I believe a function exists in the RTL to remove the tabs from a string and properly
replace them with spaces.

 I would need to look that up but I do remember seeing it somewhere.

EDIT
  Yes there is, Tab2Space but I noticed that it does not calculate the group segment of tabs verses the chars up to that point. It simple replaces the tab char with the number of spaces you indicate.
  Maybe a proper function should be added to the strutils ?

« Last Edit: November 18, 2022, 02:07:13 pm by jamie »
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #7 on: November 18, 2022, 08:39:23 pm »
I'm Not sure there tabs. I run the string thru a CleanString function and then DelSpace1 function and then  DelDoubleSpace (written by KodeZwerg) and still having the problem of double spaces remaining in the string.

Will write a demo and post.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 933
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Trouble with DelSpace1
« Reply #8 on: November 18, 2022, 09:50:47 pm »
I'm Not sure there tabs. I run the string thru a CleanString function and then DelSpace1 function and then  DelDoubleSpace (written by KodeZwerg) and still having the problem of double spaces remaining in the string.

Will write a demo and post.
Try with my DelDoubleSpace(FilterAscii('text')) combo?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #9 on: November 18, 2022, 10:02:25 pm »
Line 1 is the string copied from hovering over the variable.

Line 2 is a copy and paste of the same string. (Notice double space between Integer and read)

Line 3 after FilterAscii function

Line 4 after DelDoubleSpace function

So I ran a DelSpace1 in lieu of DelDoubleSpace and it worked.

So it's the FilterAscii that is removing something in the string. I can live with that.

Still, wonder what it is in the string that stops DelSpace1 from working.

thanks

Code: Text  [Select][+][-]
  1. Line 1: *M1 #$0D#$0A#$0D#$0A  uses ... avglvltree; #$0D#$0A#$0D#$0A   public property TCustomStringMap.CompareItemsFunc: TListSortCompare   read GetCompareItemsFunc; #$0D#$0A #$0D#$0A
  2. Line 2: *M1 uses ... actnlist; public property TCustomActionList.ActionCount: Integer   read GetActionCount;
  3. Line 3: *M1 uses ... actnlist; public property TCustomActionList.ActionCount: Integer   read GetActionCount
  4. Line 4: *M1 uses ... actnlist; public property TCustomActionList.ActionCount: Integer read GetActionCount
  5.  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4104
Re: Trouble with DelSpace1
« Reply #10 on: November 18, 2022, 10:21:58 pm »
Try a simple parsing function like this (ASCII text only - full UTF8 support is a bit more complex):
Code: Pascal  [Select][+][-]
  1. function RemoveControlCharsDoubleSpacesTrimInsideParentheses(const aText: String): String;
  2.   var
  3.     i: SizeInt;
  4.   begin
  5.     Result := aText;
  6.     if Result = '' then
  7.       Exit;
  8.  
  9.     for i := Length(Result) downto 1 do
  10.       if Result[i] in [#0..#31] then
  11.         Delete(Result, i, 1);
  12.  
  13.     for i := Length(Result) downto 2 do
  14.       if (Result[i] = #32) and (Result[i-1] = #32) then
  15.         Delete(Result, i, 1);
  16.  
  17.     i := 1;
  18.     while (i < Pred(Length(Result))) do
  19.       begin
  20.         if (Result[i] = '(') and (Result[i+1] = #32) then
  21.           Delete(Result, i+1, 1);
  22.         if (Result[i] = #32) and (Result[i+1] = ')') then
  23.           Delete(Result, i, 1);
  24.         Inc(i);
  25.       end;
  26.  
  27.   end;
                   

KodeZwerg

  • Hero Member
  • *****
  • Posts: 933
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Trouble with DelSpace1
« Reply #11 on: November 18, 2022, 10:39:44 pm »
..snip..
you forgot in your code to filter out #127/$7F (DEL control)
(just for completition of ascii aspect)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #12 on: November 18, 2022, 10:45:37 pm »
Thanks All

Using the FilterAscii with DelSpace1 and I think it works.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 933
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Trouble with DelSpace1 (Solved)
« Reply #13 on: November 18, 2022, 10:53:40 pm »
Code: Pascal  [Select][+][-]
  1. function FilterAscii(const AString: string): string;
  2. var
  3.   i: Integer;
  4.   s: string;
  5. begin
  6.   Result := AString;
  7.   if (Length(AString) < 1) then
  8.     Exit;
  9.   s := '';
  10.   for i := 1 to Length(AString) do
  11.     if  ((Ord(AString[i]) > 31) and (Ord(AString[i]) < 127)) then
  12.       s := s + AString[i];
  13.   Result := s;
  14. end;
  15.  
  16. function DelDoubleSpace(const AString: string): string;
  17. var
  18.   s: string;
  19.   p: Integer;
  20. begin
  21.   Result := AString;
  22.   if (Length(AString) < 2) then
  23.     Exit;
  24.   s := AString;
  25.   repeat
  26.     p := Pos('  ', s);
  27.     if (p > 0) then
  28.       Delete(s, p, 1);
  29.   until p = 0;
  30.   Result := s;
  31. end;
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. const
  37.   s = String('*M1 uses ... actnlist; public property TCustomActionList.ActionCount: Integer   read GetActionCount;');
  38. begin
  39.   Memo1.Text := DelDoubleSpace(FilterAscii(s));
  40. end;
output: *M1 uses ... actnlist; public property TCustomActionList.ActionCount: Integer read GetActionCount;

FilterAscii had a minor bug with the lenght that i used, now all fluffy and shiny.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1259
Re: Trouble with DelSpace1
« Reply #14 on: December 09, 2022, 04:56:15 am »
I still have double spaces problens. Now using a CleanString function, DelSpace1 and a routine written by howardpc -  "function RemoveControlCharsDoubleSpacesTrimInsideParentheses(const aText: String): String;"

However; there are still double spaces in the string;



Code: Pascal  [Select][+][-]
  1. Before
  2. s2String = $00000000015ADAE8^: '*M1'#$0D#$0A#$0D#$0A'  uses ... controls;'#$0D#$0A#$0D#$0A'  protected procedure TControlBorderSpacing.Change(  InnerSpaceChanged: Boolean); virtual;'#$0D#$0A'                              Arguments:InnerSpaceChanged    currently ignored (distinction no more'#$0D#$0A'             required).'#$0D#$0A' '#$0D#$0A anything is done.
  3.  
  4. *M1  uses ... controls;  protected procedure TControlBorderSpacing.Change(  InnerSpaceChanged: Boolean); virtual;
  5.   Arguments: InnerSpaceChanged    currently ignored (distinction no more required).
« Last Edit: December 09, 2022, 05:02:48 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018