comparison libinterp/corefcn/pr-output.cc @ 27500:1ef42010c53b

Allow multiple arguments to format(). * pr-output.cc (set_format_style): Wrap if/else if decoding tree in while (--argc > 0) loop to process all arguments to function. Place special case of zero arguments (argc == 0) first.
author Rik <rik@octave.org>
date Tue, 15 Oct 2019 21:29:41 -0700
parents 2545345f8bd9
children 25479159213b
comparison
equal deleted inserted replaced
27499:2545345f8bd9 27500:1ef42010c53b
3570 set_format_style (int argc, const string_vector& argv) 3570 set_format_style (int argc, const string_vector& argv)
3571 { 3571 {
3572 int idx = 1; 3572 int idx = 1;
3573 std::string format; 3573 std::string format;
3574 3574
3575 if (--argc > 0) 3575 argc--;
3576 { 3576 if (argc == 0)
3577 std::string arg = argv[idx++];
3578 std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
3579 format = arg;
3580
3581 if (arg == "short")
3582 {
3583 if (--argc > 0)
3584 {
3585 arg = argv[idx++];
3586 format.append (arg);
3587
3588 if (arg == "e")
3589 {
3590 init_format_state ();
3591 print_e = true;
3592 }
3593 else if (arg == "g")
3594 {
3595 init_format_state ();
3596 print_g = true;
3597 }
3598 else if (arg == "eng")
3599 {
3600 init_format_state ();
3601 print_eng = true;
3602 }
3603 else
3604 error ("format: unrecognized option 'short %s'", arg.c_str ());
3605 }
3606 else
3607 init_format_state ();
3608
3609 set_output_prec (5);
3610 }
3611 else if (arg == "shorte")
3612 {
3613 init_format_state ();
3614 print_e = true;
3615 set_output_prec (5);
3616 }
3617 else if (arg == "shortg")
3618 {
3619 init_format_state ();
3620 print_g = true;
3621 set_output_prec (5);
3622 }
3623 else if (arg == "shorteng")
3624 {
3625 init_format_state ();
3626 print_eng = true;
3627 set_output_prec (5);
3628 }
3629 else if (arg == "long")
3630 {
3631 if (--argc > 0)
3632 {
3633 arg = argv[idx++];
3634 format.append (arg);
3635
3636 if (arg == "e")
3637 {
3638 init_format_state ();
3639 print_e = true;
3640 }
3641 else if (arg == "g")
3642 {
3643 init_format_state ();
3644 print_g = true;
3645 }
3646 else if (arg == "eng")
3647 {
3648 init_format_state ();
3649 print_eng = true;
3650 }
3651 else
3652 error ("format: unrecognized option 'long %s'", arg.c_str ());
3653 }
3654 else
3655 init_format_state ();
3656
3657 set_output_prec (16);
3658 }
3659 else if (arg == "longe")
3660 {
3661 init_format_state ();
3662 print_e = true;
3663 set_output_prec (16);
3664 }
3665 else if (arg == "longg")
3666 {
3667 init_format_state ();
3668 print_g = true;
3669 set_output_prec (16);
3670 }
3671 else if (arg == "longeng")
3672 {
3673 init_format_state ();
3674 print_eng = true;
3675 set_output_prec (16);
3676 }
3677 else if (arg == "hex")
3678 {
3679 init_format_state ();
3680 hex_format = 1;
3681 }
3682 else if (arg == "native-hex")
3683 {
3684 init_format_state ();
3685 hex_format = 2;
3686 }
3687 else if (arg == "bit")
3688 {
3689 init_format_state ();
3690 bit_format = 1;
3691 }
3692 else if (arg == "native-bit")
3693 {
3694 init_format_state ();
3695 bit_format = 2;
3696 }
3697 else if (arg == "+" || arg == "plus")
3698 {
3699 if (--argc > 0)
3700 {
3701 arg = argv[idx++];
3702 format.append (arg);
3703
3704 if (arg.length () == 3)
3705 plus_format_chars = arg;
3706 else
3707 error ("format: invalid option for plus format");
3708 }
3709 else
3710 plus_format_chars = "+- ";
3711
3712 init_format_state ();
3713 plus_format = true;
3714 }
3715 else if (arg == "rat")
3716 {
3717 init_format_state ();
3718 rat_format = true;
3719 }
3720 else if (arg == "bank")
3721 {
3722 init_format_state ();
3723 bank_format = true;
3724 }
3725 else if (arg == "free")
3726 {
3727 init_format_state ();
3728 free_format = true;
3729 }
3730 else if (arg == "none")
3731 {
3732 init_format_state ();
3733 free_format = true;
3734 }
3735 else if (arg == "compact")
3736 {
3737 Vcompact_format = true;
3738 return;
3739 }
3740 else if (arg == "loose")
3741 {
3742 Vcompact_format = false;
3743 return;
3744 }
3745 else if (arg == "lowercase")
3746 {
3747 uppercase_format = false;
3748 return;
3749 }
3750 else if (arg == "uppercase")
3751 {
3752 uppercase_format = true;
3753 return;
3754 }
3755 else
3756 error ("format: unrecognized format state '%s'", arg.c_str ());
3757 }
3758 else
3759 { 3577 {
3760 init_format_state (); 3578 init_format_state ();
3761 set_output_prec (5); 3579 set_output_prec (5);
3762 format = "short"; 3580 format = "short";
3763 Vcompact_format = false; 3581 Vcompact_format = false;
3764 uppercase_format = false; 3582 uppercase_format = false;
3583 }
3584 else
3585 {
3586 format = format_string; // Initialize with existing value
3587 while (argc-- > 0)
3588 {
3589 std::string arg = argv[idx++];
3590 std::transform (arg.begin (), arg.end (), arg.begin (), tolower);
3591
3592 if (arg == "short")
3593 {
3594 format = arg;
3595 if (argc > 0)
3596 {
3597 arg = argv[idx];
3598 if (arg == "e")
3599 {
3600 init_format_state ();
3601 print_e = true;
3602 format.append (arg);
3603 argc--;
3604 idx++;
3605 }
3606 else if (arg == "g")
3607 {
3608 init_format_state ();
3609 print_g = true;
3610 format.append (arg);
3611 argc--;
3612 idx++;
3613 }
3614 else if (arg == "eng")
3615 {
3616 init_format_state ();
3617 print_eng = true;
3618 format.append (arg);
3619 argc--;
3620 idx++;
3621 }
3622 else
3623 init_format_state ();
3624 }
3625 else
3626 init_format_state ();
3627
3628 set_output_prec (5);
3629 }
3630 else if (arg == "shorte")
3631 {
3632 format = arg;
3633 init_format_state ();
3634 print_e = true;
3635 set_output_prec (5);
3636 }
3637 else if (arg == "shortg")
3638 {
3639 format = arg;
3640 init_format_state ();
3641 print_g = true;
3642 set_output_prec (5);
3643 }
3644 else if (arg == "shorteng")
3645 {
3646 format = arg;
3647 init_format_state ();
3648 print_eng = true;
3649 set_output_prec (5);
3650 }
3651 else if (arg == "long")
3652 {
3653 format = arg;
3654 if (argc > 0)
3655 {
3656 arg = argv[idx];
3657
3658 if (arg == "e")
3659 {
3660 init_format_state ();
3661 print_e = true;
3662 format.append (arg);
3663 argc--;
3664 idx++;
3665 }
3666 else if (arg == "g")
3667 {
3668 init_format_state ();
3669 print_g = true;
3670 format.append (arg);
3671 argc--;
3672 idx++;
3673 }
3674 else if (arg == "eng")
3675 {
3676 init_format_state ();
3677 print_eng = true;
3678 format.append (arg);
3679 argc--;
3680 idx++;
3681 }
3682 else
3683 init_format_state ();
3684 }
3685 else
3686 init_format_state ();
3687
3688 set_output_prec (16);
3689 }
3690 else if (arg == "longe")
3691 {
3692 format = arg;
3693 init_format_state ();
3694 print_e = true;
3695 set_output_prec (16);
3696 }
3697 else if (arg == "longg")
3698 {
3699 format = arg;
3700 init_format_state ();
3701 print_g = true;
3702 set_output_prec (16);
3703 }
3704 else if (arg == "longeng")
3705 {
3706 format = arg;
3707 init_format_state ();
3708 print_eng = true;
3709 set_output_prec (16);
3710 }
3711 else if (arg == "hex")
3712 {
3713 format = arg;
3714 init_format_state ();
3715 hex_format = 1;
3716 }
3717 else if (arg == "native-hex")
3718 {
3719 format = arg;
3720 init_format_state ();
3721 hex_format = 2;
3722 }
3723 else if (arg == "bit")
3724 {
3725 format = arg;
3726 init_format_state ();
3727 bit_format = 1;
3728 }
3729 else if (arg == "native-bit")
3730 {
3731 format = arg;
3732 init_format_state ();
3733 bit_format = 2;
3734 }
3735 else if (arg == "+" || arg == "plus")
3736 {
3737 format = arg;
3738 if (argc > 0)
3739 {
3740 arg = argv[idx];
3741
3742 if (arg.length () == 3)
3743 {
3744 plus_format_chars = arg;
3745 format.append (arg);
3746 argc--;
3747 idx++;
3748 }
3749 else
3750 plus_format_chars = "+- ";
3751 }
3752 else
3753 plus_format_chars = "+- ";
3754
3755 init_format_state ();
3756 plus_format = true;
3757 }
3758 else if (arg == "rat")
3759 {
3760 format = arg;
3761 init_format_state ();
3762 rat_format = true;
3763 }
3764 else if (arg == "bank")
3765 {
3766 format = arg;
3767 init_format_state ();
3768 bank_format = true;
3769 }
3770 else if (arg == "free")
3771 {
3772 format = arg;
3773 init_format_state ();
3774 free_format = true;
3775 }
3776 else if (arg == "none")
3777 {
3778 format = arg;
3779 init_format_state ();
3780 free_format = true;
3781 }
3782 else if (arg == "compact")
3783 Vcompact_format = true;
3784 else if (arg == "loose")
3785 Vcompact_format = false;
3786 else if (arg == "lowercase")
3787 uppercase_format = false;
3788 else if (arg == "uppercase")
3789 uppercase_format = true;
3790 else
3791 error ("format: unrecognized format state '%s'", arg.c_str ());
3792 }
3765 } 3793 }
3766 3794
3767 format_string = format; 3795 format_string = format;
3768 } 3796 }
3769 3797