Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve array formatting #6448

Open
enitrat opened this issue Oct 2, 2024 · 3 comments
Open

feat: improve array formatting #6448

enitrat opened this issue Oct 2, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@enitrat
Copy link
Contributor

enitrat commented Oct 2, 2024

It would be nice to format arrays enabling multiple values per line.

Current:

       #[test]
        fn test_u256_to_be_bytes_full() {
            let input: u256 = 0xf432156278901234deadbeefcafebabe0123456789abcdef0fedcba987654321;
            let res: Span<u8> = input.to_be_bytes();

            assert_eq!(
                res,
                [
                    0xf4,
                    0x32,
                    0x15,
                    0x62,
                    0x78,
                    0x90,
                    0x12,
                    0x34,
                    0xde,
                    0xad,
                    0xbe,
                    0xef,
                    0xca,
                    0xfe,
                    0xba,
                    0xbe,
                    0x01,
                    0x23,
                    0x45,
                    0x67,
                    0x89,
                    0xab,
                    0xcd,
                    0xef,
                    0x0f,
                    0xed,
                    0xcb,
                    0xa9,
                    0x87,
                    0x65,
                    0x43,
                    0x21
                ].span()
            );
        }

Desired:

        #[test]
        fn test_u256_to_be_bytes_full() {
            let input: u256 = 0xf432156278901234deadbeefcafebabe0123456789abcdef0fedcba987654321;
            let res: Span<u8> = input.to_be_bytes();

            assert_eq!(res, [
                0xf4, 0x32, 0x15, 0x62, 0x78, 0x90, 0x12, 0x34,
                0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe,
                0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
                0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21
            ].span());
        }
@enitrat enitrat added the enhancement New feature or request label Oct 2, 2024
@feltroidprime
Copy link
Contributor

+1

@gilbens-starkware
Copy link
Contributor

gilbens-starkware commented Oct 22, 2024

I feel like your desired output as it is, is not well defined, as each row in the array does not span the entire line. If we'll break each line once it reached the full length of the we'll have something like:

#[test]
fn test_u256_to_be_bytes_full() {
    let input: u256 = 0xf432156278901234deadbeefcafebabe0123456789abcdef0fedcba987654321;
    let res: Span<u8> = input.to_be_bytes();

    assert_eq!(
        res,
        [
            0xf4, 0x32, 0x15, 0x62, 0x78, 0x90, 0x12, 0x34, 0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe,
            0xba, 0xbe, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x0f, 0xed, 0xcb, 0xa9,
            0x87, 0x65, 0x43, 0x21
        ]
        .span()
    );
}

Which is probably what we'll end up doing.

Unrelated to this change, if you still wish to organise the array in a matrix like shape, you can make the formatter skip this statement using #[cairofmt::skip], however this means that the whole statement will have to be manually formatted.

#[test]
fn test_u256_to_be_bytes_full() {
    let input: u256 = 0xf432156278901234deadbeefcafebabe0123456789abcdef0fedcba987654321;
    let res: Span<u8> = input.to_be_bytes();
    
    #[cairofmt::skip]
    assert_eq!(
        res,
        [
            0xf4, 0x32, 0x15, 0x62, 0x78, 0x90, 0x12, 0x34,
            0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xba, 0xbe,
            0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
            0x0f, 0xed, 0xcb, 0xa9, 0x87, 0x65, 0x43, 0x21
        ]
        .span()
    );
}

@enitrat
Copy link
Contributor Author

enitrat commented Oct 22, 2024

Sure, that's fine for me, I just wanted something that was not one element per line 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants