Giter Club home page Giter Club logo

Comments (4)

19Rustam80 avatar 19Rustam80 commented on June 11, 2024

There is "if Right>0 then" on line 718.

from fibplus.

OLMIG62 avatar OLMIG62 commented on June 11, 2024

@19Rustam80 There is "if Right>0 then" on line 718.
Yes, but Right is decremented in the loop line 721 and could fall below 0....

from fibplus.

duilioisola avatar duilioisola commented on June 11, 2024

Tested an had this result:

No spaces
'AAAA' -> Left : 1; Right: 4
'AAA' -> Left : 1; Right: 3
'AA' -> Left : 1; Right: 2
'A' -> Left : 1; Right: 1
2 spaces
'  BBBB  ' -> Left : 3; Right: 6
'  BBB  ' -> Left : 3; Right: 5
'  BB  ' -> Left : 3; Right: 4
'  B  ' -> Left : 3; Right: 3
1 space
' CCCC ' -> Left : 2; Right: 5
' CCC ' -> Left : 2; Right: 4
' CC ' -> Left : 2; Right: 3
' C ' -> Left : 2; Right: 2
Space right
'DDDD ' -> Left : 1; Right: 4
'DDD ' -> Left : 1; Right: 3
'DD ' -> Left : 1; Right: 2
'D ' -> Left : 1; Right: 1
Space left
' EEEE' -> Left : 2; Right: 5
' EEE' -> Left : 2; Right: 4
' EE' -> Left : 2; Right: 3
' E' -> Left : 2; Right: 2
Just space
'    ' -> Left : 1; Right: 0
'   ' -> Left : 1; Right: 0
'  ' -> Left : 1; Right: 0
' ' -> Left : 1; Right: 0
'' -> Left : 0; Right: 0

Used this code:

procedure TFMMain.Button1Click(Sender: TObject);
  procedure TrimPositions(const Str:string; var Left,Right:integer);
  begin
    Right:=Length(Str);
    if Right>0 then
    begin
      Left :=1;
      while (Right > 0) and (Str[Right] <= ' ') do Dec(Right);
      while (Left <= Right) and (Str[Left] <= ' ') do Inc(Left);
    end
    else
    Left:=0;
  end;

var
  s : string;
  Left,Right : integer;
begin
  inherited;
  // MDebug is a TMemo
  MDebug.Lines.Clear;

  MDebug.Lines.Add('No spaces');
  s := 'AAAA';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'AAA';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'AA';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'A';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  MDebug.Lines.Add('2 spaces');
  s := '  BBBB  ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '  BBB  ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '  BB  ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '  B  ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  MDebug.Lines.Add('1 space');
  s := ' CCCC ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' CCC ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' CC ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' C ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  MDebug.Lines.Add('Space right');
  s := 'DDDD ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'DDD ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'DD ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := 'D ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  MDebug.Lines.Add('Space left');
  s := ' EEEE';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' EEE';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' EE';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' E';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  MDebug.Lines.Add('Just space');
  s := '    ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '   ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '  ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := ' ';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));

  s := '';
  TrimPositions(s, Left, Right);
  MDebug.Lines.Add(format('''%s'' -> Left : %d; Right: %d', [s, Left, Right]));
end;

from fibplus.

madorin avatar madorin commented on June 11, 2024

@shavluk , thanks, applied the patch

from fibplus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.