Natools

Check-in [da55fecd97]
Login
Overview
Comment:s_expressions-printers-pretty: fix width overflow in quoted string escapes
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: da55fecd9728138d7e0a8744b461bbada065a788
User & Date: nat on 2014-02-01 13:27:22
Other Links: manifest | tags
Context
2014-02-02
12:58
s_expressions-printers-pretty-tests: new test for indentation mechanisms check-in: b48757a3eb user: nat tags: trunk
2014-02-01
13:27
s_expressions-printers-pretty: fix width overflow in quoted string escapes check-in: da55fecd97 user: nat tags: trunk
2014-01-31
10:40
s_expressions-printers-pretty: fix unwanted escape of the second byte of two-byte newlines check-in: e4eacf664f user: nat tags: trunk
Changes

Modified src/natools-s_expressions-printers-pretty.adb from [978118703a] to [42192fffa4].

321
322
323
324
325
326
327




328
329
330
331
332
333
334
335
336
337
338


339
340
341
342
343
344
345
346
347
348
349
350
351

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392



393
394
395




396
397
398
399
400
401
402
403
404
405
406



407
408
409
410
411
412
413
      Single_Line : in Boolean;
      Size        : out Count;
      Cursor      : in out Screen_Offset)
   is
      C : Count;
      I : Offset := Data'First;
      Last_Non_NL : Offset := Data'Last;




   begin
      while Last_Non_NL in Data'Range
        and then (Data (Last_Non_NL) = Encodings.CR
                  or Data (Last_Non_NL) = Encodings.LF)
      loop
         Last_Non_NL := Last_Non_NL - 1;
      end loop;

      Size := 2;
      Cursor := Cursor + 1;
      while I in Data'Range loop


         case Data (I) is
            when 8 | 9 | 11 | 12
              | Encodings.Quoted_Atom_End | Encodings.Escape =>
               Size := Size + 2;
               Cursor := Cursor + 2;
            when 10 | 13 =>
               if Single_Line
                 or else I > Last_Non_NL
                 or else not Is_Newline (Data, I, Newline)
               then
                  Size := Size + 2;
                  Cursor := Cursor + 2;
               else

                  case Newline is
                     when LF | CR =>
                        Size := Size + 1;
                     when CR_LF | LF_CR =>
                        Size := Size + 2;
                        I := I + 1;
                  end case;
                  Cursor := 1;
               end if;
            when 0 .. 7 | 14 .. 31 =>
               Size := Size + 4;
               Cursor := Cursor + 4;
            when 16#80# .. 16#FF# =>
               case Encoding is
                  when ASCII =>
                     Size := Size + 4;
                     Cursor := Cursor + 4;
                  when Latin =>
                     if Data (I) in 16#80# .. 16#9F# then
                        Size := Size + 4;
                        Cursor := Cursor + 4;
                     else
                        Size := Size + 1;
                        Cursor := Cursor + 1;
                     end if;
                  when UTF_8 =>
                     C := UTF_Character_Size (Data, I);
                     if C = 0 then
                        Size := Size + 4;
                        Cursor := Cursor + 4;
                     else
                        Size := Size + C;
                        Cursor := Cursor + 1;
                        I := I + C - 1;
                     end if;
               end case;
            when others =>
               Size := Size + 1;
               Cursor := Cursor + 1;
         end case;




         if not Single_Line
           and then Width > 0
           and then Cursor >= Width




         then
            case Newline is
               when CR | LF =>
                  Size := Size + 2;
               when CR_LF | LF_CR =>
                  Size := Size + 3;
            end case;
            Cursor := 1;
         end if;

         I := I + 1;



      end loop;

      Cursor := Cursor + 1;
   end Quoted_Lengths;


   function Multi_Line_Quoted_Size







>
>
>
>











>
>



|
<





|
<

>


|

|
|

<


|
<



|
<


|
<

|
<




|
<

|
|
|



|
<


>
>
>


|
>
>
>
>








<
|
|
>
>
>







321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

349
350
351
352
353
354

355
356
357
358
359
360
361
362
363

364
365
366

367
368
369
370

371
372
373

374
375

376
377
378
379
380

381
382
383
384
385
386
387
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

409
410
411
412
413
414
415
416
417
418
419
420
      Single_Line : in Boolean;
      Size        : out Count;
      Cursor      : in out Screen_Offset)
   is
      C : Count;
      I : Offset := Data'First;
      Last_Non_NL : Offset := Data'Last;
      Input_Delta : Count;
      Output_Delta : Count;
      Width_Adjust : Offset;
      New_Cursor : Screen_Column;
   begin
      while Last_Non_NL in Data'Range
        and then (Data (Last_Non_NL) = Encodings.CR
                  or Data (Last_Non_NL) = Encodings.LF)
      loop
         Last_Non_NL := Last_Non_NL - 1;
      end loop;

      Size := 2;
      Cursor := Cursor + 1;
      while I in Data'Range loop
         Input_Delta := 1;
         Width_Adjust := 0;
         case Data (I) is
            when 8 | 9 | 11 | 12
              | Encodings.Quoted_Atom_End | Encodings.Escape =>
               Output_Delta := 2;

            when 10 | 13 =>
               if Single_Line
                 or else I > Last_Non_NL
                 or else not Is_Newline (Data, I, Newline)
               then
                  Output_Delta := 2;

               else
                  Width_Adjust := -Offset (Cursor);
                  case Newline is
                     when LF | CR =>
                        Output_Delta := 1;
                     when CR_LF | LF_CR =>
                        Output_Delta := 2;
                        Input_Delta := 2;
                  end case;

               end if;
            when 0 .. 7 | 14 .. 31 =>
               Output_Delta := 4;

            when 16#80# .. 16#FF# =>
               case Encoding is
                  when ASCII =>
                     Output_Delta := 4;

                  when Latin =>
                     if Data (I) in 16#80# .. 16#9F# then
                        Output_Delta := 4;

                     else
                        Output_Delta := 1;

                     end if;
                  when UTF_8 =>
                     C := UTF_Character_Size (Data, I);
                     if C = 0 then
                        Output_Delta := 4;

                     else
                        Output_Delta := C;
                        Input_Delta := C;
                        Width_Adjust := 1 - C;
                     end if;
               end case;
            when others =>
               Output_Delta := 1;

         end case;

         New_Cursor := Screen_Column
           (Offset (Cursor) + Output_Delta + Width_Adjust);

         if not Single_Line
           and then Width > 0
           and then Cursor > 1
           and then (New_Cursor > Width + 1
             or else (New_Cursor = Width + 1
               and then I + 1 in Data'Range
               and then not Is_Newline (Data, I, Newline)))
         then
            case Newline is
               when CR | LF =>
                  Size := Size + 2;
               when CR_LF | LF_CR =>
                  Size := Size + 3;
            end case;
            Cursor := 1;

         else
            I := I + Input_Delta;
            Size := Size + Output_Delta;
            Cursor := New_Cursor;
         end if;
      end loop;

      Cursor := Cursor + 1;
   end Quoted_Lengths;


   function Multi_Line_Quoted_Size
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
   procedure Write_Quoted
     (Output : in out Printer;
      Data : in Atom;
      Single_Line : in Boolean)
   is
      procedure Escape
        (Value : in Octet;
         Result : out Atom;
         Pos : in out Offset);

      Size : Count;
      Last_Non_NL : Offset := Data'Last;

      procedure Escape
        (Value : in Octet;
         Result : out Atom;
         Pos : in out Offset) is
      begin
         Result (Pos) := Encodings.Escape;

         case Output.Param.Quoted_Escape is
            when Octal_Escape =>
               Result (Pos + 1) := Encodings.Digit_0 + (Value / 2**6);
               Result (Pos + 2) := Encodings.Digit_0 + (Value / 2**3) mod 2**3;
               Result (Pos + 3) := Encodings.Digit_0 + (Value mod 2**3);
            when Hex_Escape =>
               Result (Pos + 1) := Character'Pos ('x');
               Encodings.Encode_Hex
                 (Value,
                  Output.Param.Hex_Casing,
                  Result (Pos + 2),
                  Result (Pos + 3));
         end case;
         Pos := Pos + 4;
      end Escape;
   begin
      declare
         Discarded_Cursor : Screen_Offset := Output.Cursor;
      begin
         Quoted_Lengths
           (Data,







|
|






|
|
















<







556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588

589
590
591
592
593
594
595
   procedure Write_Quoted
     (Output : in out Printer;
      Data : in Atom;
      Single_Line : in Boolean)
   is
      procedure Escape
        (Value : in Octet;
         Result : in out Atom;
         Pos : in Offset);

      Size : Count;
      Last_Non_NL : Offset := Data'Last;

      procedure Escape
        (Value : in Octet;
         Result : in out Atom;
         Pos : in Offset) is
      begin
         Result (Pos) := Encodings.Escape;

         case Output.Param.Quoted_Escape is
            when Octal_Escape =>
               Result (Pos + 1) := Encodings.Digit_0 + (Value / 2**6);
               Result (Pos + 2) := Encodings.Digit_0 + (Value / 2**3) mod 2**3;
               Result (Pos + 3) := Encodings.Digit_0 + (Value mod 2**3);
            when Hex_Escape =>
               Result (Pos + 1) := Character'Pos ('x');
               Encodings.Encode_Hex
                 (Value,
                  Output.Param.Hex_Casing,
                  Result (Pos + 2),
                  Result (Pos + 3));
         end case;

      end Escape;
   begin
      declare
         Discarded_Cursor : Screen_Offset := Output.Cursor;
      begin
         Quoted_Lengths
           (Data,
603
604
605
606
607
608
609




610
611
612
613
614




615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717



718
719
720




721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742



743
744
745
746
747
748
749
      end loop;

      declare
         Result : Atom (0 .. Size - 1);
         I : Offset := Data'First;
         O : Offset := Result'First + 1;
         C : Count;




      begin
         Result (0) := Encodings.Quoted_Atom_Begin;
         Output.Cursor := Output.Cursor + 1;

         while I in Data'Range loop




            case Data (I) is
               when 8 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('b');
                  O := O + 2;
                  Output.Cursor := Output.Cursor + 2;
               when 9 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('t');
                  O := O + 2;
                  Output.Cursor := Output.Cursor + 2;
               when 10 =>
                  if Single_Line
                    or else I > Last_Non_NL
                    or else not Is_Newline (Data, I, Output.Param.Newline)
                  then
                     Result (O) := Encodings.Escape;
                     Result (O + 1) := Character'Pos ('n');
                     O := O + 2;
                     Output.Cursor := Output.Cursor + 2;
                  else
                     Result (O) := Data (I);
                     O := O + 1;
                     Output.Cursor := 1;
                     if Output.Param.Newline = CR_LF
                       or Output.Param.Newline = LF_CR
                     then
                        I := I + 1;
                        Result (O) := Data (I);
                        O := O + 1;
                     end if;
                  end if;
               when 11 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('v');
                  O := O + 2;
                  Output.Cursor := Output.Cursor + 2;
               when 12 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('f');
                  O := O + 2;
                  Output.Cursor := Output.Cursor + 2;
               when 13 =>
                  if Single_Line
                    or else I > Last_Non_NL
                    or else not Is_Newline (Data, I, Output.Param.Newline)
                  then
                     Result (O) := Encodings.Escape;
                     Result (O + 1) := Character'Pos ('r');
                     O := O + 2;
                     Output.Cursor := Output.Cursor + 2;
                  else
                     Result (O) := Data (I);
                     O := O + 1;
                     Output.Cursor := 1;
                     if Output.Param.Newline = CR_LF
                       or Output.Param.Newline = LF_CR
                     then
                        I := I + 1;
                        Result (O) := Data (I);
                        O := O + 1;
                     end if;
                  end if;
               when Encodings.Quoted_Atom_End | Encodings.Escape =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Data (I);
                  O := O + 2;
                  Output.Cursor := Output.Cursor + 2;
               when 0 .. 7 | 14 .. 31 =>
                  Escape (Data (I), Result, O);
                  Output.Cursor := Output.Cursor + 4;
               when 16#80# .. 16#FF# =>
                  case Output.Param.Char_Encoding is
                     when ASCII =>
                        Escape (Data (I), Result, O);
                        Output.Cursor := Output.Cursor + 4;
                     when Latin =>
                        if Data (I) in 16#80# .. 16#9F# then
                           Escape (Data (I), Result, O);
                           Output.Cursor := Output.Cursor + 4;
                        else
                           Result (O) := Data (I);
                           O := O + 1;
                           Output.Cursor := Output.Cursor + 1;
                        end if;
                     when UTF_8 =>
                        C := UTF_Character_Size (Data, I);
                        if C = 0 then
                           Escape (Data (I), Result, O);
                           Output.Cursor := Output.Cursor + 4;
                        else
                           Result (O .. O + C - 1) := Data (I .. I + C - 1);
                           O := O + C;
                           I := I + C - 1;
                           Output.Cursor := Output.Cursor + 1;
                        end if;
                  end case;
               when others =>
                  Result (O) := Data (I);
                  O := O + 1;
                  Output.Cursor := Output.Cursor + 1;
            end case;




            if not Single_Line
              and then Output.Param.Width > 0
              and then Output.Cursor >= Output.Param.Width




            then
               Result (O) := Encodings.Escape;
               case Output.Param.Newline is
                  when CR =>
                     Result (O + 1) := Encodings.CR;
                     O := O + 2;
                  when LF =>
                     Result (O + 1) := Encodings.LF;
                     O := O + 2;
                  when CR_LF =>
                     Result (O + 1) := Encodings.CR;
                     Result (O + 2) := Encodings.LF;
                     O := O + 3;
                  when LF_CR =>
                     Result (O + 1) := Encodings.LF;
                     Result (O + 2) := Encodings.CR;
                     O := O + 3;
               end case;
               Output.Cursor := 1;
            end if;

            I := I + 1;



         end loop;

         pragma Assert (O = Result'Last);
         Result (O) := Encodings.Quoted_Atom_End;

         Output.Stream.Write (Result);
      end;







>
>
>
>





>
>
>
>




|
<



|
<







|
<


<
|



|
|
|





|
<



|
<







|
<


<
|



|
|
|





|
<


|




|



|


<
<





|


|
|
|




<
<


>
>
>


|
>
>
>
>



















<
|
|
>
>
>







609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633

634
635
636
637

638
639
640
641
642
643
644
645

646
647

648
649
650
651
652
653
654
655
656
657
658
659
660

661
662
663
664

665
666
667
668
669
670
671
672

673
674

675
676
677
678
679
680
681
682
683
684
685
686
687

688
689
690
691
692
693
694
695
696
697
698
699
700
701


702
703
704
705
706
707
708
709
710
711
712
713
714
715
716


717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747

748
749
750
751
752
753
754
755
756
757
758
759
      end loop;

      declare
         Result : Atom (0 .. Size - 1);
         I : Offset := Data'First;
         O : Offset := Result'First + 1;
         C : Count;
         Input_Delta : Count;
         Output_Delta : Count;
         Width_Adjust : Offset;
         New_Cursor : Screen_Column;
      begin
         Result (0) := Encodings.Quoted_Atom_Begin;
         Output.Cursor := Output.Cursor + 1;

         while I in Data'Range loop
            Output_Delta := 1;
            Width_Adjust := 0;
            Input_Delta := 1;

            case Data (I) is
               when 8 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('b');
                  Output_Delta := 2;

               when 9 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('t');
                  Output_Delta := 2;

               when 10 =>
                  if Single_Line
                    or else I > Last_Non_NL
                    or else not Is_Newline (Data, I, Output.Param.Newline)
                  then
                     Result (O) := Encodings.Escape;
                     Result (O + 1) := Character'Pos ('n');
                     Output_Delta := 2;

                  else
                     Result (O) := Data (I);

                     Width_Adjust := -Offset (Output.Cursor);
                     if Output.Param.Newline = CR_LF
                       or Output.Param.Newline = LF_CR
                     then
                        Input_Delta := 2;
                        Result (O + 1) := Data (I + 1);
                        Output_Delta := 2;
                     end if;
                  end if;
               when 11 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('v');
                  Output_Delta := 2;

               when 12 =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Character'Pos ('f');
                  Output_Delta := 2;

               when 13 =>
                  if Single_Line
                    or else I > Last_Non_NL
                    or else not Is_Newline (Data, I, Output.Param.Newline)
                  then
                     Result (O) := Encodings.Escape;
                     Result (O + 1) := Character'Pos ('r');
                     Output_Delta := 2;

                  else
                     Result (O) := Data (I);

                     Width_Adjust := -Offset (Output.Cursor);
                     if Output.Param.Newline = CR_LF
                       or Output.Param.Newline = LF_CR
                     then
                        Input_Delta := 2;
                        Result (O + 1) := Data (I + 1);
                        Output_Delta := 2;
                     end if;
                  end if;
               when Encodings.Quoted_Atom_End | Encodings.Escape =>
                  Result (O) := Encodings.Escape;
                  Result (O + 1) := Data (I);
                  Output_Delta := 2;

               when 0 .. 7 | 14 .. 31 =>
                  Escape (Data (I), Result, O);
                  Output_Delta := 4;
               when 16#80# .. 16#FF# =>
                  case Output.Param.Char_Encoding is
                     when ASCII =>
                        Escape (Data (I), Result, O);
                        Output_Delta := 4;
                     when Latin =>
                        if Data (I) in 16#80# .. 16#9F# then
                           Escape (Data (I), Result, O);
                           Output_Delta := 4;
                        else
                           Result (O) := Data (I);


                        end if;
                     when UTF_8 =>
                        C := UTF_Character_Size (Data, I);
                        if C = 0 then
                           Escape (Data (I), Result, O);
                           Output_Delta := 4;
                        else
                           Result (O .. O + C - 1) := Data (I .. I + C - 1);
                           Input_Delta := C;
                           Output_Delta := C;
                           Width_Adjust := 1 - C;
                        end if;
                  end case;
               when others =>
                  Result (O) := Data (I);


            end case;

            New_Cursor := Screen_Column
              (Offset (Output.Cursor) + Output_Delta + Width_Adjust);

            if not Single_Line
              and then Output.Param.Width > 0
              and then Output.Cursor > 1
              and then (New_Cursor > Output.Param.Width + 1
                or else (New_Cursor = Output.Param.Width + 1
                  and then I + 1 in Data'Range
                  and then not Is_Newline (Data, I + 1, Output.Param.Newline)))
            then
               Result (O) := Encodings.Escape;
               case Output.Param.Newline is
                  when CR =>
                     Result (O + 1) := Encodings.CR;
                     O := O + 2;
                  when LF =>
                     Result (O + 1) := Encodings.LF;
                     O := O + 2;
                  when CR_LF =>
                     Result (O + 1) := Encodings.CR;
                     Result (O + 2) := Encodings.LF;
                     O := O + 3;
                  when LF_CR =>
                     Result (O + 1) := Encodings.LF;
                     Result (O + 2) := Encodings.CR;
                     O := O + 3;
               end case;
               Output.Cursor := 1;

            else
               I := I + Input_Delta;
               O := O + Output_Delta;
               Output.Cursor := New_Cursor;
            end if;
         end loop;

         pragma Assert (O = Result'Last);
         Result (O) := Encodings.Quoted_Atom_End;

         Output.Stream.Write (Result);
      end;