Lazarus

Free Pascal => Beginners => Topic started by: JLWest on November 18, 2022, 01:32:26 am

Title: Trouble with DelSpace1 (Solved)
Post by: JLWest 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.  
Title: Re: Trouble with DelSpace1
Post by: KodeZwerg 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...
Title: Re: Trouble with DelSpace1
Post by: dje 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?
Title: Re: Trouble with DelSpace1
Post by: JLWest 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.
Title: Re: Trouble with DelSpace1
Post by: JLWest on November 18, 2022, 04:55:58 am
Yea, they weren't spaces but control characters.
Title: Re: Trouble with DelSpace1(Solved)
Post by: KodeZwerg 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
Title: Re: Trouble with DelSpace1(Solved)
Post by: jamie 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 ?

Title: Re: Trouble with DelSpace1
Post by: JLWest 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.
Title: Re: Trouble with DelSpace1
Post by: KodeZwerg 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?
Title: Re: Trouble with DelSpace1
Post by: JLWest 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.  
Title: Re: Trouble with DelSpace1
Post by: howardpc 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;
                   
Title: Re: Trouble with DelSpace1
Post by: KodeZwerg 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)
Title: Re: Trouble with DelSpace1
Post by: JLWest on November 18, 2022, 10:45:37 pm
Thanks All

Using the FilterAscii with DelSpace1 and I think it works.
Title: Re: Trouble with DelSpace1 (Solved)
Post by: KodeZwerg 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.
Title: Re: Trouble with DelSpace1
Post by: JLWest 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).
Title: Re: Trouble with DelSpace1
Post by: bytebites on December 09, 2022, 07:55:18 am
Code: Pascal  [Select][+][-]
  1.   writeln(RemoveControlCharsDoubleSpacesTrimInsideParentheses('*M1'#$0D#$0A#$0D#$0A'  uses ... controls;'#$0D#$0A#$0D#$0A'  protected procedure TControlBorderSpacing.Change(  InnerSpaceChanged: Boolean); virtual;'#$0D#$0A ));
Yields:
*M1 uses ... controls; protected procedure TControlBorderSpacing.Change(InnerSpaceChanged: Boolean); virtual;
Title: Re: Trouble with DelSpace1
Post by: JLWest on December 09, 2022, 08:56:11 am
No they still have double Spaces. Willing to build and post a Demo but will take a couple of  days.

Code: Pascal  [Select][+][-]
  1. sSTR := Trim( ASTR );
  2. S:=RemoveControlCharsDoubleSpacesTrimInsideParentheses(sSTR);
  3. S = $00000000000DC3B8^: '*M1 uses ... controls; protected procedure TControlBorderSpacing.Change(  InnerSpaceChanged: Boolean); virtual; Arguments: InnerSpaceChanged    currently ignored (distinction no more required).'
Title: Re: Trouble with DelSpace1
Post by: bytebites on December 09, 2022, 09:27:57 am
Code: Pascal  [Select][+][-]
  1. for c in sSTR do write(ord(c));
What does this show?
Title: Re: Trouble with DelSpace1
Post by: jamie on December 09, 2022, 03:53:26 pm
Have you tried using Tab2Space function first on the string?
Title: Re: Trouble with DelSpace1
Post by: JLWest on December 09, 2022, 04:55:21 pm
@bytebites I'll try it.
@Jamie  Yes I did Tab2Space. I also tried:
 S:=StringReplace(sCopy,'  ',' ',[ rfReplaceAll, rfIgnoreCase]x) ;
Title: Re: Trouble with DelSpace1
Post by: JLWest on December 09, 2022, 05:03:45 pm
That Worked. I don't understand how.
Thanks

Code: Pascal  [Select][+][-]
  1. S ='*M1 uses ... controls; public property TCustomControl.Canvas: TCanvas read FCanvas write FCanvas; See: TCanvas TCanvas represents a drawing surface which might be attached to a visual control which might be displayed on the screen or to an offscreen bitmap. It contains a number of drawing functions.'
  2.                
Title: Re: Trouble with DelSpace1
Post by: JLWest on December 09, 2022, 05:10:12 pm
@bitebites It works but I'm getting sn error. Something about not able to open the file.
TinyPortal © 2005-2018