| 1 |
Index: jsonschema/src/keywords/format.rs |
| 2 |
=================================================================== |
| 3 |
--- jsonschema.orig/src/keywords/format.rs 2006-07-23 21:21:28.000000000 -0400 |
| 4 |
+++ jsonschema/src/keywords/format.rs 2026-06-30 00:20:57.827771190 -0400 |
| 5 |
@@ -150,27 +150,27 @@ |
| 6 |
|
| 7 |
#[inline] |
| 8 |
fn parse_four_digits(bytes: &[u8]) -> Option<u16> { |
| 9 |
- let value = u32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]); |
| 10 |
- |
| 11 |
+ // Little-endian layout: bytes[0] lands in the lowest byte of the u32. |
| 12 |
// Check if all bytes are ASCII digits |
| 13 |
- if value.wrapping_sub(0x3030_3030) & 0xF0F0_F0F0 == 0 { |
| 14 |
- let val = (value & 0x0F0F_0F0F).wrapping_mul(2561) >> 8; |
| 15 |
- Some(((val & 0x00FF_00FF).wrapping_mul(6_553_601) >> 16) as u16) |
| 16 |
- } else { |
| 17 |
- None |
| 18 |
+ let value = u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]); |
| 19 |
+ let sub = value.wrapping_sub(0x3030_3030); |
| 20 |
+ if sub.wrapping_add(0x0606_0606) & 0xF0F0_F0F0 != 0 { |
| 21 |
+ return None; |
| 22 |
} |
| 23 |
+ let val = (sub & 0x0F0F_0F0F).wrapping_mul(2561) >> 8; |
| 24 |
+ Some(((val & 0x00FF_00FF).wrapping_mul(6_553_601) >> 16) as u16) |
| 25 |
} |
| 26 |
|
| 27 |
#[inline] |
| 28 |
fn parse_two_digits(bytes: &[u8]) -> Option<u8> { |
| 29 |
- let value = u16::from_ne_bytes([bytes[0], bytes[1]]); |
| 30 |
- |
| 31 |
- // Check if both bytes are ASCII digits |
| 32 |
- if value.wrapping_sub(0x3030) & 0xF0F0 == 0 { |
| 33 |
- Some(((value & 0x0F0F).wrapping_mul(2561) >> 8) as u8) |
| 34 |
- } else { |
| 35 |
- None |
| 36 |
+ // Little-endian layout: bytes[0] lands in the low byte of the u16. |
| 37 |
+ // Check if all bytes are ASCII digits |
| 38 |
+ let value = u16::from_le_bytes([bytes[0], bytes[1]]); |
| 39 |
+ let sub = value.wrapping_sub(0x3030); |
| 40 |
+ if sub.wrapping_add(0x0606) & 0xF0F0 != 0 { |
| 41 |
+ return None; |
| 42 |
} |
| 43 |
+ Some(((sub & 0x0F0F).wrapping_mul(2561) >> 8) as u8) |
| 44 |
} |
| 45 |
|
| 46 |
macro_rules! handle_offset { |
| 47 |
@@ -912,6 +912,30 @@ |
| 48 |
|
| 49 |
use super::*; |
| 50 |
|
| 51 |
+ #[test_case(b"00" => Some(0); "min")] |
| 52 |
+ #[test_case(b"09" => Some(9); "nine")] |
| 53 |
+ #[test_case(b"10" => Some(10); "ten")] |
| 54 |
+ #[test_case(b"59" => Some(59); "fifty-nine")] |
| 55 |
+ #[test_case(b"99" => Some(99); "max")] |
| 56 |
+ #[test_case(b"1a" => None; "alpha")] |
| 57 |
+ #[test_case(b" 5" => None; "leading space")] |
| 58 |
+ #[test_case(b":0" => None; "colon is not a digit")] |
| 59 |
+ #[test_case(b";9" => None; "semicolon is not a digit")] |
| 60 |
+ fn test_parse_two_digits(bytes: &[u8]) -> Option<u8> { |
| 61 |
+ parse_two_digits(bytes) |
| 62 |
+ } |
| 63 |
+ |
| 64 |
+ #[test_case(b"0000" => Some(0); "zero")] |
| 65 |
+ #[test_case(b"1970" => Some(1970); "epoch year")] |
| 66 |
+ #[test_case(b"2023" => Some(2023); "recent year")] |
| 67 |
+ #[test_case(b"9999" => Some(9999); "max")] |
| 68 |
+ #[test_case(b"199x" => None; "trailing alpha")] |
| 69 |
+ #[test_case(b" 999" => None; "leading space")] |
| 70 |
+ #[test_case(b"20:3" => None; "colon is not a digit")] |
| 71 |
+ fn test_parse_four_digits(bytes: &[u8]) -> Option<u16> { |
| 72 |
+ parse_four_digits(bytes) |
| 73 |
+ } |
| 74 |
+ |
| 75 |
#[test] |
| 76 |
fn ignored_format() { |
| 77 |
let schema = json!({"format": "custom", "type": "string"}); |
| 78 |
@@ -1030,6 +1054,8 @@ |
| 79 |
#[test_case("aaaa-01-12", false; "Malformed (letters in year)")] |
| 80 |
#[test_case("2000-bb-12", false; "Malformed (letters in month)")] |
| 81 |
#[test_case("2000-01-cc", false; "Malformed (letters in day)")] |
| 82 |
+ #[test_case("20:3-01-15", false; "colon in year")] |
| 83 |
+ #[test_case("20;3-01-15", false; "semicolon in year")] |
| 84 |
fn test_is_valid_date(input: &str, expected: bool) { |
| 85 |
assert_eq!(is_valid_date(input), expected); |
| 86 |
} |