iced_x86/formatter/
num_fmt_opts.rs

1// SPDX-License-Identifier: MIT
2// Copyright (C) 2018-present iced project and contributors
3
4use crate::formatter::*;
5use core::cmp;
6
7/// Gets initialized with the default options and can be overridden by a [`FormatterOptionsProvider`]
8///
9/// [`FormatterOptionsProvider`]: trait.FormatterOptionsProvider.html
10#[derive(Debug, Default, Copy, Clone)]
11pub struct NumberFormattingOptions<'a> {
12	/// Number prefix or an empty string
13	pub prefix: &'a str,
14	/// Number suffix or an empty string
15	pub suffix: &'a str,
16	/// Digit separator or an empty string to not use a digit separator
17	pub digit_separator: &'a str,
18	/// Size of a digit group or 0 to not use a digit separator
19	pub digit_group_size: u8,
20	/// Number base
21	pub number_base: NumberBase,
22	/// Use uppercase hex digits
23	pub uppercase_hex: bool,
24	/// Small hex numbers (-9 .. 9) are shown in decimal
25	pub small_hex_numbers_in_decimal: bool,
26	/// Add a leading zero to hex numbers if there's no prefix and the number starts with hex digits `A-F`
27	pub add_leading_zero_to_hex_numbers: bool,
28	/// If `true`, add leading zeros to numbers, eg. `1h` vs `00000001h`
29	pub leading_zeros: bool,
30	/// If `true`, the number is signed, and if `false` it's an unsigned number
31	pub signed_number: bool,
32	/// Add leading zeros to displacements
33	pub displacement_leading_zeros: bool,
34}
35
36impl<'a> NumberFormattingOptions<'a> {
37	/// Creates options used when formatting immediate values
38	///
39	/// # Arguments
40	///
41	/// * `options`: Formatter options to use
42	#[inline]
43	#[must_use]
44	pub fn with_immediate(options: &'a FormatterOptions) -> Self {
45		NumberFormattingOptions::new(options, options.leading_zeros(), options.signed_immediate_operands(), false)
46	}
47
48	/// Creates options used when formatting displacements
49	///
50	/// # Arguments
51	///
52	/// * `options`: Formatter options to use
53	#[inline]
54	#[must_use]
55	pub fn with_displacement(options: &'a FormatterOptions) -> Self {
56		NumberFormattingOptions::new(options, options.leading_zeros(), options.signed_memory_displacements(), options.displacement_leading_zeros())
57	}
58
59	/// Creates options used when formatting branch operands
60	///
61	/// # Arguments
62	///
63	/// * `options`: Formatter options to use
64	#[inline]
65	#[must_use]
66	pub fn with_branch(options: &'a FormatterOptions) -> Self {
67		NumberFormattingOptions::new(options, options.branch_leading_zeros(), false, false)
68	}
69
70	/// Constructor
71	///
72	/// # Arguments
73	///
74	/// * `options`: Formatter options to use
75	/// * `leading_zeros`: Add leading zeros to numbers, eg. `1h` vs `00000001h`
76	/// * `signed_number`: Signed numbers if `true`, and unsigned numbers if `false`
77	/// * `displacement_leading_zeros`: Add leading zeros to displacements
78	#[inline]
79	#[must_use]
80	#[allow(clippy::missing_inline_in_public_items)]
81	pub fn new(options: &'a FormatterOptions, leading_zeros: bool, signed_number: bool, displacement_leading_zeros: bool) -> Self {
82		let (digit_group_size, prefix, suffix) = match options.number_base() {
83			NumberBase::Hexadecimal => (options.hex_digit_group_size(), options.hex_prefix(), options.hex_suffix()),
84			NumberBase::Decimal => (options.decimal_digit_group_size(), options.decimal_prefix(), options.decimal_suffix()),
85			NumberBase::Octal => (options.octal_digit_group_size(), options.octal_prefix(), options.octal_suffix()),
86			NumberBase::Binary => (options.binary_digit_group_size(), options.binary_prefix(), options.binary_suffix()),
87		};
88		Self {
89			prefix,
90			suffix,
91			digit_separator: options.digit_separator(),
92			digit_group_size: cmp::min(u8::MAX as u32, digit_group_size) as u8,
93			number_base: options.number_base(),
94			uppercase_hex: options.uppercase_hex(),
95			small_hex_numbers_in_decimal: options.small_hex_numbers_in_decimal(),
96			add_leading_zero_to_hex_numbers: options.add_leading_zero_to_hex_numbers(),
97			leading_zeros,
98			signed_number,
99			displacement_leading_zeros,
100		}
101	}
102}