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: support format!("{:x}") notation for hexadecimal formatting #6297

Merged
merged 5 commits into from
Aug 29, 2024

Conversation

remybar
Copy link
Contributor

@remybar remybar commented Aug 27, 2024

Proposal for issue #6261.

This PR adds a LowerHex trait as existing in Rust, to be able to format integer values to hexadecimal using the {:x} notation. Rust also provides a UpperHex trait to format the string in upper case but I don't know if it's really useful.

This PR also implements this trait for:

  • unsigned integers (from u8 to u256),
  • felt252
  • NonZero
  • common Starknet types such as ContractAddress and ClassHash using the existing into_felt252_based module.

This change is Reviewable

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 8 files at r1.
Reviewable status: 3 of 8 files reviewed, 4 unresolved discussions (waiting on @remybar)


Cargo.lock line 241 at r1 (raw file):

]

[[package]]

revert file.


corelib/src/fmt.cairo line 320 at r1 (raw file):

> of LowerHex<T> {
    fn fmt(self: @T, ref f: Formatter) -> Result<(), Error> {
        f.buffer.append(@"0x");

shouldn't be part of it. same as rust.

Code quote:

        f.buffer.append(@"0x");

crates/cairo-lang-semantic/src/inline_macros/write.rs line 142 at r1 (raw file):

        WriteMacro::NAME
    }
}

run the same rust fmt as the CI.
(./script/rust_fmt.sh)

Code quote:

fn get_macro_name(with_newline: bool) -> &'static str {
    if with_newline {
        WritelnMacro::NAME
    } else {
        WriteMacro::NAME
    }
}

crates/cairo-lang-semantic/src/inline_macros/write.rs line 527 at r1 (raw file):

    } else {
        return Err("Unsupported formatting trait: only `Display` and `Debug` are supported");
    };

Suggestion:

    let fmt_type = match formatting_spec.as_str() {
        "" => FormattingTrait::Display,
        "?" => FormattingTrait::Debug,
        "x" => FormattingTrait::LowerHex,
        _ => return Err("Unsupported formatting trait: only `Display`, `Debug` and `LowerHex` are supported"),
    };

@remybar remybar force-pushed the feat/hex_print branch 2 times, most recently from bc28a98 to c257cb4 Compare August 28, 2024 10:52
@remybar
Copy link
Contributor Author

remybar commented Aug 28, 2024

Hi @orizi !

Thanks for the review. I did the required changes.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 4 files at r2.
Reviewable status: 4 of 8 files reviewed, 1 unresolved discussion (waiting on @remybar)


crates/cairo-lang-semantic/src/inline_macros/write.rs line 527 at r1 (raw file):

    } else {
        return Err("Unsupported formatting trait: only `Display` and `Debug` are supported");
    };

why not match?

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please view at reviewable: https://reviewable.io/reviews/starkware-libs/cairo/6297#-

Reviewable status: 4 of 8 files reviewed, 1 unresolved discussion (waiting on @remybar)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 4 of 8 files reviewed, 1 unresolved discussion (waiting on @remybar)

a discussion (no related file):
@gilbens-starkware for 2nd eye.


@remybar
Copy link
Contributor Author

remybar commented Aug 28, 2024

why not match?

Because I didn't noticed the match but just the change related to the error message... 🫣
Sorry, I also replaced the if/else by a match as suggested ;-)

Copy link
Contributor

@gilbens-starkware gilbens-starkware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewable status: 4 of 8 files reviewed, 1 unresolved discussion (waiting on @orizi and @remybar)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 8 files at r1, 1 of 4 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @remybar)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @remybar)

@orizi orizi enabled auto-merge August 29, 2024 11:47
auto-merge was automatically disabled August 29, 2024 12:23

Head branch was pushed to by a user without write access

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 6 of 6 files at r4, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @remybar)

@orizi orizi enabled auto-merge August 29, 2024 12:35
auto-merge was automatically disabled August 29, 2024 13:15

Head branch was pushed to by a user without write access

Copy link
Collaborator

@mkaput mkaput left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 1 files at r5.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on @Arcticae, @Draggu, and @piotmag769)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @remybar)

@orizi orizi enabled auto-merge August 29, 2024 18:22
@orizi orizi added this pull request to the merge queue Aug 29, 2024
Merged via the queue into starkware-libs:main with commit f9f2823 Aug 29, 2024
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants