@@ -122,6 +122,22 @@ fn clean_int_str(mut s: &str) -> Option<Cow<str>> {
122
122
s = suffix;
123
123
}
124
124
125
+ // Remember if the number is negative
126
+ // the `strip_leading_zeros` function will not strip leading zeros for negative numbers
127
+ // therefore we simply "take away" the unary minus sign temporarily and add it back before
128
+ // returning. This allows consistent handling of leading zeros for both positive and negative numbers.
129
+ let mut is_negative = false ;
130
+ if let Some ( suffix) = s. strip_prefix ( '-' ) {
131
+ // Invalidate "--" and "-+" as an integer prefix by returning None
132
+ if suffix. starts_with ( '-' ) | suffix. starts_with ( '+' ) {
133
+ return None ;
134
+ }
135
+
136
+ is_negative = true ;
137
+ // Continue as usual without the unary minus sign
138
+ s = suffix;
139
+ }
140
+
125
141
// strip loading zeros
126
142
s = strip_leading_zeros ( s) ?;
127
143
@@ -136,13 +152,20 @@ fn clean_int_str(mut s: &str) -> Option<Cow<str>> {
136
152
137
153
// remove underscores
138
154
if let Some ( str_stripped) = strip_underscores ( s) {
139
- Some ( str_stripped. into ( ) )
140
- } else {
141
- match len_before == s. len ( ) {
142
- true => None ,
143
- false => Some ( s. into ( ) ) ,
155
+ match is_negative {
156
+ true => return Some ( ( "-" . to_string ( ) + & str_stripped) . into ( ) ) ,
157
+ false => return Some ( str_stripped. into ( ) ) ,
144
158
}
145
159
}
160
+
161
+ if len_before == s. len ( ) {
162
+ return None ;
163
+ }
164
+
165
+ match is_negative {
166
+ true => Some ( ( "-" . to_string ( ) + s) . into ( ) ) ,
167
+ false => Some ( s. into ( ) ) ,
168
+ }
146
169
}
147
170
148
171
/// strip leading zeros from a string, we can't simple use `s.trim_start_matches('0')`, because:
0 commit comments