Skip to content

Commit ff5076b

Browse files
committed
rustpython-literal
1 parent cea23d2 commit ff5076b

File tree

23 files changed

+419
-407
lines changed

23 files changed

+419
-407
lines changed

Cargo.lock

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
1212
[workspace]
1313
resolver = "2"
1414
members = [
15-
"compiler", "compiler/ast", "compiler/core", "compiler/codegen", "compiler/parser",
15+
"compiler", "compiler/ast", "compiler/core", "compiler/literal", "compiler/codegen", "compiler/parser",
1616
".", "common", "derive", "jit", "vm", "pylib", "stdlib", "wasm/lib", "derive-impl",
1717
]
1818

common/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ license = "MIT"
1111
threading = ["parking_lot"]
1212

1313
[dependencies]
14+
rustpython-literal = { path = "../compiler/literal" }
15+
1416
ascii = { workspace = true }
1517
bitflags = { workspace = true }
1618
bstr = { workspace = true }
@@ -24,12 +26,9 @@ once_cell = { workspace = true }
2426
parking_lot = { workspace = true, optional = true }
2527
rand = { workspace = true }
2628

27-
hexf-parse = "0.2.1"
28-
lexical-parse-float = { version = "0.8.0", features = ["format"] }
2929
lock_api = "0.4"
3030
radium = "0.7"
3131
siphasher = "0.3"
32-
unic-ucd-category = "0.9"
3332
volatile = "0.3"
3433

3534
[target.'cfg(windows)'.dependencies]

common/src/cformat.rs

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Implementation of Printf-Style string formatting
22
//! as per the [Python Docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting).
3-
use crate::float_ops;
43
use bitflags::bitflags;
54
use num_bigint::{BigInt, Sign};
65
use num_traits::Signed;
6+
use rustpython_literal::{float, format::Case};
77
use std::{
88
cmp, fmt,
99
iter::{Enumerate, Peekable},
@@ -48,24 +48,18 @@ impl fmt::Display for CFormatError {
4848

4949
pub type CFormatConversion = super::format::FormatConversion;
5050

51-
#[derive(Debug, PartialEq)]
52-
pub enum CFormatCase {
53-
Lowercase,
54-
Uppercase,
55-
}
56-
5751
#[derive(Debug, PartialEq)]
5852
pub enum CNumberType {
5953
Decimal,
6054
Octal,
61-
Hex(CFormatCase),
55+
Hex(Case),
6256
}
6357

6458
#[derive(Debug, PartialEq)]
6559
pub enum CFloatType {
66-
Exponent(CFormatCase),
67-
PointDecimal(CFormatCase),
68-
General(CFormatCase),
60+
Exponent(Case),
61+
PointDecimal(Case),
62+
General(Case),
6963
}
7064

7165
#[derive(Debug, PartialEq)]
@@ -283,14 +277,13 @@ impl CFormatSpec {
283277
}
284278

285279
pub fn format_number(&self, num: &BigInt) -> String {
286-
use CFormatCase::{Lowercase, Uppercase};
287280
use CNumberType::*;
288281
let magnitude = num.abs();
289282
let prefix = if self.flags.contains(CConversionFlags::ALTERNATE_FORM) {
290283
match self.format_type {
291284
CFormatType::Number(Octal) => "0o",
292-
CFormatType::Number(Hex(Lowercase)) => "0x",
293-
CFormatType::Number(Hex(Uppercase)) => "0X",
285+
CFormatType::Number(Hex(Case::Lower)) => "0x",
286+
CFormatType::Number(Hex(Case::Upper)) => "0X",
294287
_ => "",
295288
}
296289
} else {
@@ -300,8 +293,8 @@ impl CFormatSpec {
300293
let magnitude_string: String = match self.format_type {
301294
CFormatType::Number(Decimal) => magnitude.to_str_radix(10),
302295
CFormatType::Number(Octal) => magnitude.to_str_radix(8),
303-
CFormatType::Number(Hex(Lowercase)) => magnitude.to_str_radix(16),
304-
CFormatType::Number(Hex(Uppercase)) => {
296+
CFormatType::Number(Hex(Case::Lower)) => magnitude.to_str_radix(16),
297+
CFormatType::Number(Hex(Case::Upper)) => {
305298
let mut result = magnitude.to_str_radix(16);
306299
result.make_ascii_uppercase();
307300
result
@@ -359,42 +352,30 @@ impl CFormatSpec {
359352

360353
let magnitude_string = match &self.format_type {
361354
CFormatType::Float(CFloatType::PointDecimal(case)) => {
362-
let case = match case {
363-
CFormatCase::Lowercase => float_ops::Case::Lower,
364-
CFormatCase::Uppercase => float_ops::Case::Upper,
365-
};
366355
let magnitude = num.abs();
367-
float_ops::format_fixed(
356+
float::format_fixed(
368357
precision,
369358
magnitude,
370-
case,
359+
*case,
371360
self.flags.contains(CConversionFlags::ALTERNATE_FORM),
372361
)
373362
}
374363
CFormatType::Float(CFloatType::Exponent(case)) => {
375-
let case = match case {
376-
CFormatCase::Lowercase => float_ops::Case::Lower,
377-
CFormatCase::Uppercase => float_ops::Case::Upper,
378-
};
379364
let magnitude = num.abs();
380-
float_ops::format_exponent(
365+
float::format_exponent(
381366
precision,
382367
magnitude,
383-
case,
368+
*case,
384369
self.flags.contains(CConversionFlags::ALTERNATE_FORM),
385370
)
386371
}
387372
CFormatType::Float(CFloatType::General(case)) => {
388373
let precision = if precision == 0 { 1 } else { precision };
389-
let case = match case {
390-
CFormatCase::Lowercase => float_ops::Case::Lower,
391-
CFormatCase::Uppercase => float_ops::Case::Upper,
392-
};
393374
let magnitude = num.abs();
394-
float_ops::format_general(
375+
float::format_general(
395376
precision,
396377
magnitude,
397-
case,
378+
*case,
398379
self.flags.contains(CConversionFlags::ALTERNATE_FORM),
399380
false,
400381
)
@@ -480,7 +461,6 @@ where
480461
I: Iterator<Item = T>,
481462
{
482463
use CFloatType::*;
483-
use CFormatCase::{Lowercase, Uppercase};
484464
use CNumberType::*;
485465
let (index, c) = match iter.next() {
486466
Some((index, c)) => (index, c.into()),
@@ -494,14 +474,14 @@ where
494474
let format_type = match c {
495475
'd' | 'i' | 'u' => CFormatType::Number(Decimal),
496476
'o' => CFormatType::Number(Octal),
497-
'x' => CFormatType::Number(Hex(Lowercase)),
498-
'X' => CFormatType::Number(Hex(Uppercase)),
499-
'e' => CFormatType::Float(Exponent(Lowercase)),
500-
'E' => CFormatType::Float(Exponent(Uppercase)),
501-
'f' => CFormatType::Float(PointDecimal(Lowercase)),
502-
'F' => CFormatType::Float(PointDecimal(Uppercase)),
503-
'g' => CFormatType::Float(General(Lowercase)),
504-
'G' => CFormatType::Float(General(Uppercase)),
477+
'x' => CFormatType::Number(Hex(Case::Lower)),
478+
'X' => CFormatType::Number(Hex(Case::Upper)),
479+
'e' => CFormatType::Float(Exponent(Case::Lower)),
480+
'E' => CFormatType::Float(Exponent(Case::Upper)),
481+
'f' => CFormatType::Float(PointDecimal(Case::Lower)),
482+
'F' => CFormatType::Float(PointDecimal(Case::Upper)),
483+
'g' => CFormatType::Float(General(Case::Lower)),
484+
'G' => CFormatType::Float(General(Case::Upper)),
505485
'c' => CFormatType::Character,
506486
'r' => CFormatType::String(CFormatConversion::Repr),
507487
's' => CFormatType::String(CFormatConversion::Str),

0 commit comments

Comments
 (0)