Recent

Author Topic: How to display a TAB (hex 09) character  (Read 1191 times)

440bx

  • Hero Member
  • *****
  • Posts: 5820
Re: How to display a TAB (hex 09) character
« Reply #15 on: September 09, 2025, 02:40:00 am »
Hello Gary,

You may want to try @Thausand's suggestion. 

May be I mistake but was not work LB_SETTABSTOPS and LBS_USETABSTOPS for windows ?

it will take a little more work than simply substituting the TAB character for a space but the result should be more as intended (and likely more visually pleasant.)

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 18363
  • Here stood a man who saw the Elbe and jumped it.
Re: How to display a TAB (hex 09) character
« Reply #16 on: September 12, 2025, 08:36:42 am »
This is my take:
Code: Pascal  [Select][+][-]
  1. program tabexpander;
  2. {$ifdef fpc}{$mode objfpc}{$endif}{$H+}
  3. uses sysutils;
  4.  
  5. operator in (v:char;s:string):Boolean;inline;
  6. begin
  7.   result := pos(v, S) <> 0;
  8. end;
  9.  
  10. function expandtabs(const s:ansistring;tabsize:byte = 8):ansistring;inline;
  11. begin
  12.   Result := s;
  13.   while #9 in result do
  14.     result := stringreplace(Result,#9,stringofchar(#32,tabsize - (pos(#9,result)-1) mod tabsize),[]);
  15. end;
  16.  
  17. begin
  18.   WriteLn('"', ExpandTabs('1234567'#9'abc'), '"');
  19.   WriteLn('"', ExpandTabs('123456'#9'abc'), '"');
  20.   WriteLn('"', ExpandTabs('123'#9'abc'), '"');
  21.   WriteLn('"', ExpandTabs('1'#9'abc'), '"');
  22.   WriteLn('"', ExpandTabs(#9'abc'), '"');
  23.   WriteLn('"', ExpandTabs('123456781234567812345678'), '"');
  24.   WriteLn('"', ExpandTabs('1234'#9'abcd'#9'xyz'), '"');
  25.   WriteLn('"', ExpandTabs('abc'#9#9'abc'), '"');
  26.   WriteLn('"', ExpandTabs(#9#9'abc'), '"');
  27.   WriteLn('"', ExpandTabs('abc'#9), '"');
  28.   WriteLn('"', ExpandTabs('abc'#9#9), '"');
  29.   WriteLn('"', ExpandTabs('abc'#9'abc'#9), '"');
  30.   ReadLn;
  31. end.
Identical output as @wp
There is a bug in my code: tabsize can not be 0. Some other solutions - e.g. Jamie's - have the same issue: modulo 0 = division by zero.
To correct that:
Code: Pascal  [Select][+][-]
  1. function expandtabs(const s:ansistring;tabsize:byte = 8):ansistring;inline;
  2. begin
  3.   Result := s;
  4.   case tabsize of
  5.   0:result := stringreplace(result,#9,'',[rfReplaceAll]);
  6.   1:result := stringreplace(result,#9,#32,[rfReplaceAll]);
  7.   2..255:while #9 in result do
  8.            result := stringreplace(Result,#9,stringofchar(#32,tabsize - (pos(#9,result)-1) mod tabsize),[]);
  9.   end;
  10. end;

« Last Edit: September 12, 2025, 10:27:15 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018