Recent

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

pixelink

  • Hero Member
  • *****
  • Posts: 1260
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??
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
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.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

pixelink

  • Hero Member
  • *****
  • Posts: 1260
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.
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
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
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

Edson

  • Hero Member
  • *****
  • Posts: 1302
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 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 1260
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 »
Can't Type - Forgetful - Had Stroke = Forgive this old man!
LAZ 2.2.0 •  VSSTUDIO(.Net) 2022 • Win10 • 16G RAM • Nvida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 4219
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/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9870
  • Debugger - SynEdit - and more
    • 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 »

 

TinyPortal © 2005-2018