@@ -7,102 +7,30 @@ pub fn name(lex: &mut Lexer<Token>) -> SmolStr {
77 SmolStr :: from ( lex. slice ( ) )
88}
99
10- pub fn string ( lex : & mut Lexer < Token > ) -> SmolStr {
11- SmolStr :: from ( serde_json:: from_str :: < ' _ , String > ( lex. slice ( ) ) . unwrap ( ) )
10+ pub fn string ( lex : & mut Lexer < Token > ) -> Option < SmolStr > {
11+ serde_json:: from_str :: < String > ( lex. slice ( ) ) . ok ( ) . map ( SmolStr :: from )
1212}
1313
1414pub fn arg ( lex : & mut Lexer < Token > ) -> SmolStr {
1515 SmolStr :: from ( & lex. slice ( ) [ 1 ..] )
1616}
1717
18- pub fn bin ( lex : & mut Lexer < Token > ) -> i64 {
19- let mut neg = false ;
20- let mut value = 0 ;
21- for & c in lex. slice ( ) . as_bytes ( ) [ 2 ..] . iter ( ) {
22- if c == b'-' {
23- neg = true ;
24- continue ;
25- }
26- if c == b'_' {
27- continue ;
28- }
29- value *= 2 ;
30- value += ( c - b'0' ) as i64 ;
31- }
32- if neg {
33- -value
34- } else {
35- value
36- }
18+ pub fn bin ( lex : & mut Lexer < Token > ) -> Option < i64 > {
19+ i64:: from_str_radix ( & lex. slice ( ) [ 2 ..] . replace ( '_' , "" ) , 2 ) . ok ( )
3720}
3821
39- pub fn oct ( lex : & mut Lexer < Token > ) -> i64 {
40- let mut neg = false ;
41- let mut value = 0 ;
42- for & c in lex. slice ( ) . as_bytes ( ) [ 2 ..] . iter ( ) {
43- if c == b'-' {
44- neg = true ;
45- continue ;
46- }
47- if c == b'_' {
48- continue ;
49- }
50- value *= 8 ;
51- value += ( c - b'0' ) as i64 ;
52- }
53- if neg {
54- -value
55- } else {
56- value
57- }
22+ pub fn oct ( lex : & mut Lexer < Token > ) -> Option < i64 > {
23+ i64:: from_str_radix ( & lex. slice ( ) [ 2 ..] . replace ( '_' , "" ) , 8 ) . ok ( )
5824}
5925
60- pub fn int ( lex : & mut Lexer < Token > ) -> i64 {
61- let mut neg = false ;
62- let mut value = 0 ;
63- for & c in lex. slice ( ) . as_bytes ( ) . iter ( ) {
64- if c == b'-' {
65- neg = true ;
66- continue ;
67- }
68- if c == b'_' {
69- continue ;
70- }
71- value *= 10 ;
72- value += ( c - b'0' ) as i64 ;
73- }
74- if neg {
75- -value
76- } else {
77- value
78- }
26+ pub fn int ( lex : & mut Lexer < Token > ) -> Option < i64 > {
27+ lex. slice ( ) . replace ( '_' , "" ) . parse ( ) . ok ( )
7928}
8029
81- pub fn hex ( lex : & mut Lexer < Token > ) -> i64 {
82- let mut neg = false ;
83- let mut value = 0 ;
84- for & c in lex. slice ( ) . as_bytes ( ) [ 2 ..] . iter ( ) {
85- if c == b'-' {
86- neg = true ;
87- continue ;
88- }
89- if c == b'_' {
90- continue ;
91- }
92- value *= 16 ;
93- value += match c {
94- 0 ..=b'9' => ( c - b'0' ) as i64 ,
95- b'a' ..=b'f' => ( c - b'a' + 10 ) as i64 ,
96- _ => ( c - b'A' + 10 ) as i64 ,
97- } ;
98- }
99- if neg {
100- -value
101- } else {
102- value
103- }
30+ pub fn hex ( lex : & mut Lexer < Token > ) -> Option < i64 > {
31+ i64:: from_str_radix ( & lex. slice ( ) [ 2 ..] . replace ( '_' , "" ) , 16 ) . ok ( )
10432}
10533
106- pub fn float ( lex : & mut Lexer < Token > ) -> f64 {
107- serde_json :: from_str ( lex. slice ( ) ) . unwrap ( )
34+ pub fn float ( lex : & mut Lexer < Token > ) -> Option < f64 > {
35+ lex. slice ( ) . replace ( '_' , "" ) . parse ( ) . ok ( )
10836}
0 commit comments