1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! Small fixed-width text parsing helpers.
//!
//! SP3, RINEX, IONEX, and CRINEX are fixed-column text products. Keeping the
//! common field slicing and Fortran-float handling here keeps product parsers
//! focused on record semantics instead of repeating byte-window plumbing.
use crate::astro::time::model::TimeScale;
/// Map a GNSS product time-system label onto the core [`TimeScale`].
///
/// Returns `None` for an empty or unrecognized label so each product parser can
/// apply its own policy for the unknown case (SP3 rejects, RINEX defaults to
/// GPST). Only the labels common to SP3 and RINEX headers are mapped here;
/// product-specific spellings (e.g. the SP3 `BDS` alias) stay with the caller.
pub(crate) fn time_scale_label(label: &str) -> Option<TimeScale> {
match label.trim() {
"GPS" | "QZS" => Some(TimeScale::Gpst),
"GLO" => Some(TimeScale::Utc),
"GAL" => Some(TimeScale::Gst),
"BDT" => Some(TimeScale::Bdt),
"UTC" => Some(TimeScale::Utc),
"TAI" => Some(TimeScale::Tai),
_ => None,
}
}
/// A fixed-column field, trimmed. Returns `None` if the requested range is out
/// of bounds or the trimmed field is blank.
pub(crate) fn field(line: &str, start: usize, end: usize) -> Option<&str> {
let s = line.get(start..end.min(line.len()))?.trim();
if s.is_empty() {
None
} else {
Some(s)
}
}
/// A fixed-column field, untrimmed. Missing ranges are returned as an empty
/// string so callers that historically treated short lines as blank can preserve
/// that behavior.
pub(crate) fn raw_field(line: &str, start: usize, end: usize) -> &str {
let s = floor_char_boundary(line, start);
let e = floor_char_boundary(line, end);
if e <= s {
return "";
}
&line[s..e]
}
/// The remainder of a fixed-column line, untrimmed. Missing ranges are returned
/// as an empty string.
pub(crate) fn raw_field_from(line: &str, start: usize) -> &str {
let s = floor_char_boundary(line, start);
&line[s..]
}
/// Parse a RINEX-style numeric field, accepting Fortran `D`/`d` exponent
/// markers. Returns `None` for missing, blank, or malformed fields.
pub(crate) fn fortran_f64(line: &str, start: usize, end: usize) -> Option<f64> {
let s = field(line, start, end)?;
crate::validate::strict_f64(s, "numeric field").ok()
}
/// Largest char boundary `<= index` and `<= line.len()`.
fn floor_char_boundary(line: &str, index: usize) -> usize {
let mut i = index.min(line.len());
while i > 0 && !line.is_char_boundary(i) {
i -= 1;
}
i
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn fortran_f64_rejects_nonfinite_fields() {
for value in ["NaN", "inf", "-inf"] {
let line = format!("{value:>19}");
assert_eq!(fortran_f64(&line, 0, line.len()), None);
}
}
#[test]
fn fortran_f64_keeps_valid_fortran_exponents() {
assert_eq!(fortran_f64(" 1.250000000000D+03", 0, 20), Some(1250.0));
assert_eq!(fortran_f64("-2.500000000000d-01", 0, 20), Some(-0.25));
}
}