Recent

Author Topic: Get Actual Char Count from SynEdit  (Read 294 times)

pixelink

  • Sr. Member
  • ****
  • Posts: 488
    • Pixelink Media
Get Actual Char Count from SynEdit
« on: April 16, 2019, 01:05:45 am »
Using SynEdit in LAZ 2.0 on Windows 7

Because there is no real good documentation in English for SynEdit.
I turn to the community

As I get going on porting this application from .NET to LAZ

Using length on synEdit is inaccurate.

Below, there are 24 actual characters in the "editor" window

But the length is pulling in 30, thus adding 6 extra hidden characters.
See below status bar on screen shot... "Editor Characters = 30"

How can I get an accurate LENGTH of actual characters??
Pixelink Media •  My LAZ Forum Page
Lazarus 2.0 w/FPC-3.0.4 • Windows 7 • 10G RAM • NIV-GForce GT 640 Graphics Card
Pascal, VB6, VBA, VB.NET • sql, php, html/css, javascript

ASBzone

  • Full Member
  • ***
  • Posts: 191
  • Automation leads to relaxation...
    • BrainWaveCC Utilities
Re: Get Actual Char Count from SynEdit
« Reply #1 on: April 16, 2019, 01:10:59 am »
Using SynEdit in LAZ 2.0 on Windows 7

Because there is no real good documentation in English for SynEdit.
I turn to the community

As I get going on porting this application from .NET to LAZ

Using length on synEdit is inaccurate.

Below, there are 24 actual characters in the "editor" window

But the length is pulling in 30, thus adding 6 extra hidden characters.
See below status bar on screen shot... "Editor Characters = 30"

How can I get an accurate LENGTH of actual characters??

My suspicion would be that the line endings are also being counted.  And that would be valid, although line endings are invisible.
-ASB: https://www.BrainWaveCC.com

Lazarus v2.0.3 r60988 / FPC v3.2.0-beta-r41873 (via FpcUpDeluxe) -- Windows 64-bit install w/32-bit cross-compile
Primary System: Windows 10 Pro x64, Version 1809 (Build 17763.379)
Other Systems: Windows 10 Pro x64, Version 1803 or greater

pixelink

  • Sr. Member
  • ****
  • Posts: 488
    • Pixelink Media
Re: Get Actual Char Count from SynEdit
« Reply #2 on: April 16, 2019, 01:25:17 am »
All well and good, but how do I get actual characters.
Pixelink Media •  My LAZ Forum Page
Lazarus 2.0 w/FPC-3.0.4 • Windows 7 • 10G RAM • NIV-GForce GT 640 Graphics Card
Pascal, VB6, VBA, VB.NET • sql, php, html/css, javascript

lucamar

  • Hero Member
  • *****
  • Posts: 1254
Re: Get Actual Char Count from SynEdit
« Reply #3 on: April 16, 2019, 01:51:28 am »
Get the text into a string, replace the line endings by nothing and use UTF8Lentgh() on it. You may want to replace TABs also. And page-feeds. And ... well, in fact anything in the range #00..#1F.

Or if it's only the line endings that bother you, get the length as you're doing and substract (number_of_lines * Length(LineEnding)) from it. It'll not always be accurate (p.e. if the last line doesn't end in a line jump) but it'll be better than what you have now.

It rather depends on what you want to do with that "number of characters"
« Last Edit: April 16, 2019, 01:53:53 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Sr. Member
  • ****
  • Posts: 488
    • Pixelink Media
Re: Get Actual Char Count from SynEdit
« Reply #4 on: April 16, 2019, 01:58:07 am »
Good idea... just grad the string of text and count those characters.
I just want to disaplay the actual characters on the status bar.

Thanks
Pixelink Media •  My LAZ Forum Page
Lazarus 2.0 w/FPC-3.0.4 • Windows 7 • 10G RAM • NIV-GForce GT 640 Graphics Card
Pascal, VB6, VBA, VB.NET • sql, php, html/css, javascript

Edson

  • Hero Member
  • *****
  • Posts: 992
Re: Get Actual Char Count from SynEdit
« Reply #5 on: April 16, 2019, 02:54:20 am »
Or just use:

nChars = length - (numberOfLines-1) * 2
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7

lucamar

  • Hero Member
  • *****
  • Posts: 1254
Re: Get Actual Char Count from SynEdit
« Reply #6 on: April 16, 2019, 03:00:57 am »
Or just use:

nChars = length - (numberOfLines-1) * 2

That is not very multi-platform friendly and it will fail when the last line does end in a CRLF (... or not? I've to test that...)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Sr. Member
  • ****
  • Posts: 488
    • Pixelink Media
Re: Get Actual Char Count from SynEdit
« Reply #7 on: April 16, 2019, 02:24:17 pm »
Well, I gave the previous suggestion a try, but it still is not counting the correct characters.
It keeps adding between 2 to 5 characters depending on what you are typing.

Here is what I tried.
So, if you type in the editor "12345678901234567890" it should be 20 char, but it could be 19 or it could be 23.
It's weird. Not reliable.

I even tried to display 0 if I return 2, even though there is no text, just hidden somethings that counts as 2

Code: Pascal  [Select]
  1. var
  2. getTxtStr: String;
  3. cEditC: Integer;
  4. cEditL: Integer;
  5.  
  6. begin
  7.   getTxtStr:=synEdit.text;
  8.   cEditC:=(Length(getTxtStr));
  9.  
  10. if cEditC <=2 Then
  11.      cEditC:=0;
  12.  
  13.   cEditL:=synEdit.Lines.Count;
  14.   lbStats.Caption:='STATS: Chords = ' + IntToStr(lstChords.Items.Count) + ' • Snippets = ' +
  15.   IntToStr(lstSnippets.Items.Count) + ' • Tags = ' + IntToStr(lstChords.Items.Count) +
  16.   ' • Editor Characters = ' + IntToStr(cEditC) + ' • Editor Lines = ' + IntToStr(cEditL);
  17.  
  18.  
« Last Edit: April 16, 2019, 02:31:52 pm by pixelink »
Pixelink Media •  My LAZ Forum Page
Lazarus 2.0 w/FPC-3.0.4 • Windows 7 • 10G RAM • NIV-GForce GT 640 Graphics Card
Pascal, VB6, VBA, VB.NET • sql, php, html/css, javascript

lucamar

  • Hero Member
  • *****
  • Posts: 1254
Re: Get Actual Char Count from SynEdit
« Reply #8 on: April 16, 2019, 02:53:17 pm »
I even tried to display 0 if I return 2, even though there is no text, just hidden somethings that counts as 2

The "hidden somethings" are the carriage return and line feed (#13#10) which end each line. That's why we suggested somthing along the lines of:
Code: [Select]
cEditC:= Length(synEdit.text) - (synEdit.Lines.Count * 2);
That gets all the chars (including the line-ends) and substracts the (teorical) number of chars wasted by those line-ends. You can make it more complete by substracting 2 if the last line doesn't end in a CR-LF pair, or something like:
Code: Pascal  [Select]
  1. if synEdit.Text.EndsWith(LineEnding) then
  2.   cEditC:= Length(synEdit.text) - (synEdit.Lines.Count * Length(LineEnding))
  3. else
  4.   cEditC:= Length(synEdit.text) - ((synEdit.Lines.Count -1) * Length(LineEnding));

Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5240
    • wiki
Re: Get Actual Char Count from SynEdit
« Reply #9 on: April 16, 2019, 03:03:37 pm »
SynEdit currently does not add line endings to SynEdit.Lines[].
It does if you get the entire SynEdit.Text.

However if you do
SynEdit.Lines[n]:='abc'+LineEnding
the lineending may be stored, and will be in the results. (SynEdit will ignore it when working with the text)

So it all depends, if you get any line ends in there (no idea what LoadFormFile does)

- loop over SynEdit.Lines[]
- if need remove line ends
- sum up the Utf8Length

Note, if you switched on remove trailing spaces, the trailing spaces on the current line may not be in the result (may depend on the settings, but usually they are not)


looping over SynEdit.Lines[] is more efficient than using SynEdit.Text.
SynEdit.Text must first build a new string with all the lines in it.

Code: Pascal  [Select]
  1. len:=0;
  2. for i := 0 to syn.lines.count - 1 do begin
  3.   s :=  syn.lines[i];
  4.   len:= len + utf8length(s);  // includes line end
  5.   j := length(s)-1;  // normal byte len
  6.   while (j>0) and s[j] in [#10, #13] do begin
  7.     dec(j);
  8.     dec(len);
  9.   end;
  10. end;
« Last Edit: April 16, 2019, 03:08:34 pm by Martin_fr »