diff --git a/source/text/implementation/vss-implementation-line_iterators.adb b/source/text/implementation/vss-implementation-line_iterators.adb index 384e60a..4a881b8 100644 --- a/source/text/implementation/vss-implementation-line_iterators.adb +++ b/source/text/implementation/vss-implementation-line_iterators.adb @@ -1,5 +1,5 @@ -- --- Copyright (C) 2021-2024, AdaCore +-- Copyright (C) 2021-2025, AdaCore -- -- SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -- @@ -45,8 +45,10 @@ package body VSS.Implementation.Line_Iterators is if not Handler.Backward (Current_Position) then -- There is no any characters before initial position. - First_Position := (others => <>); - Last_Position := (others => <>); + First_Position := + VSS.Implementation.Strings.Position_Before_First_Character; + Last_Position := + VSS.Implementation.Strings.Position_Before_First_Character; Terminator_Position := (others => <>); return False; @@ -154,8 +156,6 @@ package body VSS.Implementation.Line_Iterators is end if; end if; - LF_Found := False; - while Handler.Backward (Current_Position) loop declare C : constant VSS.Unicode.Code_Point := @@ -166,136 +166,49 @@ package body VSS.Implementation.Line_Iterators is case C is when Line_Feed => if Terminators (VSS.Strings.LF) then - -- ??? Commented-out code - -- LF_Found := True; - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - exit; elsif Terminators (VSS.Strings.CRLF) then - raise Program_Error; + Aux_Position := Current_Position; + + if Handler.Backward (Aux_Position) then + if Handler.Element (Aux_Position) + = Carriage_Return + then + exit; + end if; + end if; else raise Program_Error; end if; - -- ??? Commented-out code - - -- if Terminators (VSS.Strings.CRLF) then - -- LF_Found := True; - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - -- elsif Terminators (VSS.Strings.LF) then - -- raise Program_Error; - -- else - -- raise Program_Error; - -- end if; - when Line_Tabulation => - if Terminators (VSS.Strings.VT) then - -- ??? Commented-out code - - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - -- return True; - exit; - - else - -- ??? Commented-out code - -- LF_Found := False; - - -- CodePeer reports here that LF_Found is always false - -- (same for the 4 other occurrences of LF_Found below) - if LF_Found then - raise Program_Error; - end if; - end if; + exit when Terminators (VSS.Strings.VT); when Form_Feed => - if Terminators (VSS.Strings.FF) then - -- ??? Commented-out code - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - -- -- return True; - exit; - else - if LF_Found then - raise Program_Error; - end if; - end if; + exit when Terminators (VSS.Strings.FF); when Carriage_Return => - if Terminators (VSS.Strings.CR) then - -- ??? Commented-out code - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - -- return True; - exit; - - else - raise Program_Error; - end if; + exit when Terminators (VSS.Strings.CR); when Next_Line => - if Terminators (VSS.Strings.NEL) then - First_Position := Current_Position; - D := Handler.Forward (First_Position); - - exit; - - else - raise Program_Error; - end if; + exit when Terminators (VSS.Strings.NEL); when Line_Separator => - if Terminators (VSS.Strings.LS) then - -- ??? Commented-out code - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - exit; - - else - if LF_Found then - raise Program_Error; - end if; - end if; + exit when Terminators (VSS.Strings.LS); when Paragraph_Separator => - if Terminators (VSS.Strings.PS) then - -- ??? Commented-out code - -- First_Position := Current_Position; - -- D := Handler.Forward (Data, First_Position); - -- - exit; - - else - raise Program_Error; - end if; + exit when Terminators (VSS.Strings.PS); when others => - if LF_Found then - raise Program_Error; - end if; - -- ??? Commented-out code - -- if LF_Found then - -- return True; - -- end if; + null; end case; First_Position := Current_Position; end; end loop; - if LF_Found then - raise Program_Error; - end if; - return True; end Backward; diff --git a/source/text/implementation/vss-strings-cursors-iterators-lines.adb b/source/text/implementation/vss-strings-cursors-iterators-lines.adb index 819d90e..ba548c2 100644 --- a/source/text/implementation/vss-strings-cursors-iterators-lines.adb +++ b/source/text/implementation/vss-strings-cursors-iterators-lines.adb @@ -41,6 +41,17 @@ package body VSS.Strings.Cursors.Iterators.Lines is -- Lookup for previous line. Position points to the first character of -- the line of the current line. + -------------- + -- Backward -- + -------------- + + overriding function Backward (Self : in out Line_Iterator) return Boolean is + begin + Lookup_Previous_Line (Self, Self.First_Position); + + return VSS.Strings.Character_Count (Self.First_Position.Index) /= 0; + end Backward; + ------------------------ -- Element_Terminator -- ------------------------ @@ -101,15 +112,20 @@ package body VSS.Strings.Cursors.Iterators.Lines is ----------------- overriding function Has_Element (Self : Line_Iterator) return Boolean is - use type VSS.Implementation.Strings.Character_Count; + Data : VSS.Implementation.Strings.String_Data; + Text : VSS.Implementation.Strings.Constant_Text_Handler_Access; begin - return - not VSS.Implementation.Strings.Is_Invalid (Self.First_Position) - and then Self.First_Position.Index - <= VSS.Implementation.Strings.Character_Count - (VSS.Strings.Magic_String_Access - (Self.Owner).Character_Length); + if Self.Owner = null then + -- Uninitialized iterator. + + return False; + end if; + + Data := VSS.Strings.Magic_String_Access (Self.Owner).Data; + Text := VSS.Implementation.Strings.Constant_Handler (Data); + + return Self.First_Position.Index in 1 .. Text.Length; end Has_Element; ------------------------- @@ -157,7 +173,17 @@ package body VSS.Strings.Cursors.Iterators.Lines is Self.Terminators := Terminators; Self.Keep_Terminator := Keep_Terminator; - if Current_Position.Index /= 1 then + if Position.Index = 0 then + -- Before first character + + Self.First_Position := + VSS.Implementation.Strings.Position_Before_First_Character; + Self.Last_Position := + VSS.Implementation.Strings.Position_Before_First_Character; + + return; + + elsif Current_Position.Index /= 1 then -- Going backward till previous line terminator has been found. Dummy := Handler.Forward (Current_Position); @@ -249,7 +275,7 @@ package body VSS.Strings.Cursors.Iterators.Lines is Last_Position, Terminator_Position) then - raise Program_Error; + null; end if; if VSS.Implementation.Strings.Is_Invalid (Terminator_Position) then