Recent

Author Topic: RawByteString Questions  (Read 3278 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
RawByteString Questions
« on: August 21, 2022, 08:07:54 am »
Is it possible to split the string with out using the period.


Code: Pascal  [Select][+][-]
  1. Var S: String='FutureValue Calculate investment.'#$0D#$0ANumberOfPeriods Calculate number';
  2.  
  3. Call: Prep(S,x,y)
  4.  
  5. procedure Prep(ARawString: RawByteString; out xPos: Integer; out yPos: Integer);
  6. Var RBSs: RawByteString;
  7.  aPos: Integer=0;
  8.  begin                                            //#$0D#$0A
  9.   RBSs:=ARawString;
  10.   xPos :=Pos('#$0D' ,RBSs);               // aPos=0
  11.   yPos :=Pos('$0A' ,RBSs);                  // aPos=0
  12.  
  13.   end;      

Don't know why all the red?
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

bytebites

  • Hero Member
  • *****
  • Posts: 780
Re: RawByteString Questions
« Reply #1 on: August 21, 2022, 08:31:32 am »
Code: Pascal  [Select][+][-]
  1. var S: String='FutureValue Calculate investment.'#$0D#$0A'NumberOfPeriods Calculate number';
  2.   lines: TStringArray;
  3.  
  4. begin
  5.   lines:=s.split(LineEnding);
  6.   writeln(lines[0]);
  7.                      
  8. end;
  9.                    
  10.  

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #2 on: August 21, 2022, 05:53:58 pm »
I get an compiler error.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.PrepSeeAlso(ARawString: RawByteString);
  2.  Var RBSs: RawByteString;
  3.    alines: TStringArray;
  4.   begin
  5.    RBSs:=ARawString;
  6.    aLines:=RBSs.split(LineEnding);       //unit1.pas(110,17) Error: Illegal qualifier
  7.   end;        
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

Handoko

  • Hero Member
  • *****
  • Posts: 5538
  • My goal: build my own game engine using Lazarus
Re: RawByteString Questions
« Reply #3 on: August 21, 2022, 06:28:11 pm »
The documentation says RawByteString actually is AnsiString:
https://www.freepascal.org/docs-html/rtl/system/rawbytestring.html

That means we can safely do the type casting AnsiString(RBSs):

Code: Pascal  [Select][+][-]
  1. procedure PrepSeeAlso(ARawString: RawByteString);
  2. var
  3.   RBSs:   RawByteString;
  4.   aLines: TStringArray;
  5. begin
  6.   RBSs   := ARawString;
  7.   aLines := AnsiString(RBSs).split(LineEnding);
  8. end;

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #4 on: August 21, 2022, 06:42:23 pm »
@Handoko Hi.

Worked Now I need to put the split lines to a TStringList. I guess I can do it in a loop. I don't think there is any statement that will assign the TString Array.
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

Handoko

  • Hero Member
  • *****
  • Posts: 5538
  • My goal: build my own game engine using Lazarus
Re: RawByteString Questions
« Reply #5 on: August 21, 2022, 06:51:38 pm »
You can try TStringList.AddDelimitedtext:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   RBSs:   RawByteString;
  4.   aLines: TStringList;
  5.   i:      Integer;
  6. begin
  7.   RBSs   := 'one' #13#10 'two' #13#10 'three';
  8.   aLines := TStringList.Create;
  9.   aLines.AddDelimitedtext(RBSs);
  10.   for i := 0 to aLines.Count-1 do
  11.     ShowMessage(aLines[i]);
  12.   aLines.Free;
  13. end;
« Last Edit: August 21, 2022, 06:54:52 pm by Handoko »

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #6 on: August 21, 2022, 07:14:46 pm »
I don't understand that at all.
There no loop to go thru the tstringarray.
I guess the line 'aLines.AddDelimitedtext(RBSs);' adds the delimiter to all the lines in the TStringList;

anyway I get this error;

  RBSs := 'one' #13#10 'two' #13#10 'three';     unit1.pas(122,22) Fatal: Syntax error, ";" expected but "const string" found

« Last Edit: August 21, 2022, 07:26:56 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

Handoko

  • Hero Member
  • *****
  • Posts: 5538
  • My goal: build my own game engine using Lazarus
Re: RawByteString Questions
« Reply #7 on: August 21, 2022, 08:02:52 pm »
Oops, I'm sorry. Spaces are not allowed in the line:

Code: Pascal  [Select][+][-]
  1.     RBSs := 'one'#13#10'two'#13#10'three';     // remove the spaces between the words
  2.  
  3.   //RBSs := 'one' #13#10 'two' #13#10 'three'; // this is incorrect


AddDelimitedtext will split the text and put the result in the TStringList. In my test, it works. Unfortunately, it also will split the line if it found a comma.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #8 on: August 21, 2022, 08:27:01 pm »
Thanks I'll try it.

This maybe a little more involved that I thought.
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

paweld

  • Hero Member
  • *****
  • Posts: 1617
Re: RawByteString Questions
« Reply #9 on: August 21, 2022, 08:35:58 pm »
The 'LineEnding'  is a natural line separator for StringList, so it is enough:
Code: Pascal  [Select][+][-]
  1. var
  2.   sl: TStringList;
  3.   s: RawByteString;
  4. begin
  5.   s := 'one'#13#10'two'#13#10'three';
  6.   sl := TStringList.Create;
  7.   sl.Text := s;  //String list has 3 lines
  8.   sl.Free;
  9. end;
« Last Edit: August 21, 2022, 08:37:37 pm by paweld »
Best regards / Pozdrawiam
paweld

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #10 on: August 21, 2022, 10:05:22 pm »
It is more complicated than I first though.

I get the rawbytestring from the clipboard. It could be n strings, but usually 6 or less.
Some of the strings end with a period and some don't, some times none of the string have a period. Seems to be random.

I would like to parse without using the periods, if they exist.

example:

StartClassGroup Start new class group.
GroupDescendentsWith Provided for Delphi compatibility
ClassGroupOf  Returns the class group to which an instance or class belongs
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

paweld

  • Hero Member
  • *****
  • Posts: 1617
Re: RawByteString Questions
« Reply #11 on: August 21, 2022, 10:57:10 pm »
somthing like this:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Clipbrd;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   sl: TStringList;
  7.   i, j: Integer;
  8. begin
  9.   sl := TStringList.Create;
  10.   sl.Text := Clipboard.AsText;  //load string from clipboard
  11.   Memo1.Lines.Assign(sl);
  12.   if sl.Count = 0 then
  13.     Label1.Caption := 'No text in clipboard'
  14.   else
  15.   begin
  16.     j := 0;
  17.     Label1.Caption := 'Text from clipboard has ' + IntToStr(sl.Count) + ' line/s';
  18.     for i := 0 to sl.Count - 1 do //for each line
  19.     begin
  20.       if RightStr(Trim(sl[i]), 1) = '.' then //check that it ends with a period
  21.         Inc(j);
  22.     end;
  23.     if j = 0 then
  24.       Label1.Caption := Label1.Caption + #13#10 + 'None'
  25.     else
  26.       Label1.Caption := Label1.Caption + #13#10 + IntToStr(j);
  27.     Label1.Caption := Label1.Caption + ' of them end with a period';
  28.   end;
  29.   sl.Free;
  30. end;                
« Last Edit: August 21, 2022, 11:01:05 pm by paweld »
Best regards / Pozdrawiam
paweld

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #12 on: August 21, 2022, 11:00:57 pm »
I'll Try it.

It compiled and runs.
I don't really know what my output should look like but I don't think what I get is
what you intneded.
I can't get the debugger to work. On compile it ask for three choices.
« Last Edit: August 21, 2022, 11:26:02 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

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1590
    • Lebeau Software
Re: RawByteString Questions
« Reply #13 on: August 22, 2022, 04:39:03 am »
I get the rawbytestring from the clipboard.

Clipboard text is either ANSI (CF_TEXT) or Unicode (CF_UNICODETEXT).  So, why are you using RawByteString at all?  It is not suited for this task.  You should be using AnsiString (when requesting CF_TEXT) or UnicodeString/WideString (when requesting CF_UNICODETEXT) instead.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: RawByteString Questions
« Reply #14 on: August 22, 2022, 05:28:14 am »
@Remy Lebeau

 The short answer is:

   Because I'm posting on the beginners board.

  The long answer is:

 I can't figure out how to parse this stuff correctly and it's left over fittings from two days and half the night of trying this that and I no longer know what.

Thanks
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