MCPcopy Index your code
hub / github.com/endbasic/endbasic / parse_numeric_prefix

Function parse_numeric_prefix

std/src/strings.rs:99–143  ·  view source on GitHub ↗

Parses the numeric prefix in `s` according to BASIC's `VAL` rules.

(s: &str)

Source from the content-addressed store, hash-verified

97
98/// Parses the numeric prefix in `s` according to BASIC's `VAL` rules.
99fn parse_numeric_prefix(s: &str) -> f64 {
100 let s = s.trim_start();
101 let bytes = s.as_bytes();
102
103 let mut i = 0;
104 if let Some(b'+' | b'-') = bytes.first().copied() {
105 i += 1;
106 }
107
108 let int_start = i;
109 while i < bytes.len() && bytes[i].is_ascii_digit() {
110 i += 1;
111 }
112 let have_int = i > int_start;
113
114 if i < bytes.len() && bytes[i] == b'.' {
115 i += 1;
116 let frac_start = i;
117 while i < bytes.len() && bytes[i].is_ascii_digit() {
118 i += 1;
119 }
120 if !have_int && i == frac_start {
121 return 0.0;
122 }
123 } else if !have_int {
124 return 0.0;
125 }
126
127 let mut end = i;
128 if i < bytes.len() && matches!(bytes[i], b'e' | b'E') {
129 let mut j = i + 1;
130 if j < bytes.len() && matches!(bytes[j], b'+' | b'-') {
131 j += 1;
132 }
133 let exp_start = j;
134 while j < bytes.len() && bytes[j].is_ascii_digit() {
135 j += 1;
136 }
137 if j > exp_start {
138 end = j;
139 }
140 }
141
142 s[..end].parse().expect("numeric prefix must parse")
143}
144
145/// The `ASC` function.
146pub struct AscFunction {

Callers 1

execMethod · 0.85

Calls 4

as_bytesMethod · 0.80
firstMethod · 0.80
lenMethod · 0.45
parseMethod · 0.45

Tested by

no test coverage detected