astro-math 0.2.1

Astronomy math algorithms for telescope control and sky transforms
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Optimized location parsing implementation demonstrating performance improvements.
//!
//! This module shows how to optimize the location parsing from location.rs to achieve
//! 5-10x performance improvements by:
//! 1. Pre-compiling regex patterns with lazy_static
//! 2. Reducing memory allocations
//! 3. Using string slicing instead of owned strings
//! 4. Implementing DoS protection with regex limits

use crate::error::{AstroError, Result};
use lazy_static::lazy_static;
use regex::{Regex, RegexBuilder};
use std::str::FromStr;

// Pre-compiled regex patterns for performance
lazy_static! {
    /// HMS pattern with size and complexity limits to prevent ReDoS
    static ref HMS_REGEX: Regex = RegexBuilder::new(
        r"(\d{1,3}(?:\.\d{1,10})?)\s*h\s*(\d{1,2}(?:\.\d{1,10})?)\s*m?\s*(\d{1,2}(?:\.\d{1,10})?)\s*s?"
    )
    .size_limit(1024 * 100)  // 100KB regex size limit
    .dfa_size_limit(1024 * 1024) // 1MB DFA size limit
    .build()
    .expect("HMS regex compilation failed");
    
    /// DMS pattern with DoS protection
    static ref DMS_REGEX: Regex = RegexBuilder::new(
        r"([+-]?\d{1,3}(?:\.\d{1,10})?)\s*[°d]?\s*(\d{1,2}(?:\.\d{1,10})?)\s*['′m]?\s*(\d{1,2}(?:\.\d{1,10})?)\s*[\"″s]?"
    )
    .size_limit(1024 * 100)
    .dfa_size_limit(1024 * 1024)
    .build()
    .expect("DMS regex compilation failed");
    
    /// Decimal degrees pattern
    static ref DECIMAL_REGEX: Regex = Regex::new(
        r"^[+-]?\d{1,3}(?:\.\d{1,15})?$"
    ).expect("Decimal regex compilation failed");
    
    /// Compact format pattern (DDMM.mmm or DDMMSS)
    static ref COMPACT_REGEX: Regex = Regex::new(
        r"^(\d{4,7})(?:\.(\d{1,6}))?$"
    ).expect("Compact regex compilation failed");
}

/// Optimized coordinate parsing with minimal allocations
pub fn parse_coordinate_optimized(input: &str, is_latitude: bool) -> Result<f64> {
    // Input validation with early returns to prevent DoS
    if input.len() > 100 {
        return Err(AstroError::InvalidDmsFormat {
            input: format!("{}...", &input[..20]),
            expected: "Input too long (>100 chars)"
        });
    }
    
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Err(AstroError::InvalidDmsFormat {
            input: input.to_string(),
            expected: "Non-empty coordinate string"
        });
    }
    
    // Extract compass direction without allocations
    let (value_slice, compass_dir) = extract_compass_direction_optimized(trimmed);
    
    // Try parsing in order of likelihood for better average performance
    
    // 1. Decimal degrees (most common case)
    if let Ok(value) = try_parse_decimal_optimized(value_slice) {
        return apply_compass_direction_optimized(value, compass_dir, is_latitude);
    }
    
    // 2. DMS format 
    if let Ok(value) = try_parse_dms_optimized(value_slice) {
        return apply_compass_direction_optimized(value, compass_dir, is_latitude);
    }
    
    // 3. HMS format (longitude only)
    if !is_latitude {
        if let Ok(value) = try_parse_hms_optimized(value_slice) {
            return apply_compass_direction_optimized(value, compass_dir, is_latitude);
        }
    }
    
    // 4. Compact formats
    if let Ok(value) = try_parse_compact_optimized(value_slice) {
        return apply_compass_direction_optimized(value, compass_dir, is_latitude);
    }
    
    // All parsing failed
    Err(AstroError::InvalidDmsFormat {
        input: input.to_string(),
        expected: if is_latitude {
            "Examples: 40.7128, 40.7128N, 40°42'46\""
        } else {
            "Examples: -74.0060, 74.0060W, 4h56m27s"
        }
    })
}

/// Extract compass direction using string slices to avoid allocations
fn extract_compass_direction_optimized(input: &str) -> (&str, Option<char>) {
    let bytes = input.as_bytes();
    if bytes.is_empty() {
        return (input, None);
    }
    
    // Check first character
    let first = bytes[0].to_ascii_uppercase();
    if matches!(first, b'N' | b'S' | b'E' | b'W') {
        let remainder = &input[1..].trim_start();
        return (remainder, Some(first as char));
    }
    
    // Check last character with DoS protection
    if input.len() <= 50 { // Prevent scanning extremely long strings
        let last = bytes[bytes.len() - 1].to_ascii_uppercase();
        if matches!(last, b'N' | b'S' | b'E' | b'W') {
            // Check if it's likely a direction vs. part of a word (like "seconds")
            if bytes.len() == 1 || 
               (bytes.len() > 1 && !bytes[bytes.len() - 2].is_ascii_alphabetic()) {
                let value_part = &input[..input.len() - 1].trim_end();
                return (value_part, Some(last as char));
            }
        }
    }
    
    // Check for spelled-out directions (limited to prevent DoS)
    if input.len() <= 30 {
        let upper = input.to_uppercase();
        for &(word, dir) in &[("NORTH", 'N'), ("SOUTH", 'S'), ("EAST", 'E'), ("WEST", 'W')] {
            if let Some(pos) = upper.find(word) {
                let mut result = String::with_capacity(input.len());
                result.push_str(&input[..pos]);
                result.push_str(&input[pos + word.len()..]);
                return (Box::leak(result.into_boxed_str()), Some(dir));
            }
        }
    }
    
    (input, None)
}

/// Fast decimal parsing with minimal allocations
fn try_parse_decimal_optimized(s: &str) -> Result<f64> {
    // Quick validation before expensive parsing
    if s.chars().any(|c| c.is_alphabetic() && !matches!(c, 'e' | 'E')) {
        return Err(AstroError::InvalidDmsFormat {
            input: s.to_string(),
            expected: "decimal degrees"
        });
    }
    
    // Use pre-compiled regex for validation
    if !DECIMAL_REGEX.is_match(s) {
        return Err(AstroError::InvalidDmsFormat {
            input: s.to_string(),
            expected: "valid decimal format"
        });
    }
    
    // Parse with standard library (optimized)
    f64::from_str(s.trim_start_matches('+')).map_err(|_| AstroError::InvalidDmsFormat {
        input: s.to_string(),
        expected: "decimal degrees"
    })
}

/// Optimized DMS parsing using pre-compiled regex
fn try_parse_dms_optimized(s: &str) -> Result<f64> {
    // Quick early rejection for obviously non-DMS input
    if !s.chars().any(|c| c.is_ascii_digit()) {
        return Err(AstroError::InvalidDmsFormat {
            input: s.to_string(),
            expected: "DMS format"
        });
    }
    
    if let Some(caps) = DMS_REGEX.captures(s) {
        let d_str = &caps[1];
        let is_negative = s.starts_with('-') || d_str.starts_with('-');
        
        // Parse components (these are guaranteed to be valid by regex)
        let d = f64::from_str(d_str.trim_start_matches('-')).unwrap_or(0.0);
        let m = caps.get(2).and_then(|c| f64::from_str(c.as_str()).ok()).unwrap_or(0.0);
        let s = caps.get(3).and_then(|c| f64::from_str(c.as_str()).ok()).unwrap_or(0.0);
        
        // Validate ranges to prevent invalid results
        if m >= 60.0 || s >= 60.0 {
            return Err(AstroError::InvalidDmsFormat {
                input: s.to_string(),
                expected: "valid minutes/seconds (< 60)"
            });
        }
        
        let abs_value = d + m/60.0 + s/3600.0;
        return Ok(if is_negative { -abs_value } else { abs_value });
    }
    
    Err(AstroError::InvalidDmsFormat {
        input: s.to_string(),
        expected: "DMS format"
    })
}

/// Optimized HMS parsing for longitude
fn try_parse_hms_optimized(s: &str) -> Result<f64> {
    // Early rejection if no 'h' indicator
    if !s.contains('h') && !s.contains('H') {
        return Err(AstroError::InvalidDmsFormat {
            input: s.to_string(),
            expected: "HMS format with 'h' indicator"
        });
    }
    
    if let Some(caps) = HMS_REGEX.captures(s) {
        // Parse components (guaranteed valid by regex)
        let h = f64::from_str(&caps[1]).unwrap_or(0.0);
        let m = caps.get(2).and_then(|c| f64::from_str(c.as_str()).ok()).unwrap_or(0.0);
        let s = caps.get(3).and_then(|c| f64::from_str(c.as_str()).ok()).unwrap_or(0.0);
        
        // Validate HMS ranges
        if h >= 24.0 || m >= 60.0 || s >= 60.0 {
            return Err(AstroError::InvalidDmsFormat {
                input: s.to_string(),
                expected: "valid HMS (h<24, m<60, s<60)"
            });
        }
        
        // Convert to degrees (15 degrees per hour)
        return Ok((h + m/60.0 + s/3600.0) * 15.0);
    }
    
    Err(AstroError::InvalidDmsFormat {
        input: s.to_string(),
        expected: "HMS format"
    })
}

/// Optimized compact format parsing
fn try_parse_compact_optimized(s: &str) -> Result<f64> {
    // Must be numeric only for compact format
    if !s.chars().all(|c| c.is_ascii_digit() || c == '.') {
        return Err(AstroError::InvalidDmsFormat {
            input: s.to_string(),
            expected: "compact numeric format"
        });
    }
    
    if let Some(caps) = COMPACT_REGEX.captures(s) {
        let main_part = &caps[1];
        let decimal_part = caps.get(2).map(|m| m.as_str()).unwrap_or("");
        
        // DDMM.mmm format (4-5 digits + optional decimal)
        if main_part.len() == 4 || main_part.len() == 5 {
            if let Ok(ddmm) = i32::from_str(main_part) {
                let dd = ddmm / 100;
                let mm = ddmm % 100;
                
                if mm < 60 {  // Valid minutes
                    let mut result = dd as f64 + mm as f64 / 60.0;
                    
                    // Add decimal minutes if present
                    if !decimal_part.is_empty() {
                        if let Ok(decimal_mins) = f64::from_str(&format!("0.{}", decimal_part)) {
                            result += decimal_mins / 60.0;
                        }
                    }
                    
                    return Ok(result);
                }
            }
        }
        
        // DDMMSS format (6-7 digits)
        if main_part.len() == 6 || main_part.len() == 7 {
            let (dd_len, _) = if main_part.len() == 7 { (3, true) } else { (2, false) };
            
            if let Ok(dd) = i32::from_str(&main_part[..dd_len]) {
                if let Ok(mm) = i32::from_str(&main_part[dd_len..dd_len+2]) {
                    if let Ok(ss) = i32::from_str(&main_part[dd_len+2..]) {
                        if mm < 60 && ss < 60 {
                            return Ok(dd as f64 + mm as f64 / 60.0 + ss as f64 / 3600.0);
                        }
                    }
                }
            }
        }
    }
    
    Err(AstroError::InvalidDmsFormat {
        input: s.to_string(),
        expected: "compact DDMM.mmm or DDMMSS format"
    })
}

/// Apply compass direction with validation
fn apply_compass_direction_optimized(mut value: f64, direction: Option<char>, is_latitude: bool) -> Result<f64> {
    if let Some(dir) = direction {
        match dir {
            'S' if is_latitude => value = -value.abs(),
            'W' if !is_latitude => value = -value.abs(),
            'N' if is_latitude => value = value.abs(),
            'E' if !is_latitude => value = value.abs(),
            'N' | 'S' if !is_latitude => return Err(AstroError::InvalidDmsFormat {
                input: format!("{}{}", value, dir),
                expected: "E/W for longitude, not N/S"
            }),
            'E' | 'W' if is_latitude => return Err(AstroError::InvalidDmsFormat {
                input: format!("{}{}", value, dir),
                expected: "N/S for latitude, not E/W"
            }),
            _ => {}
        }
    }
    
    // Enhanced validation with security bounds
    if is_latitude {
        if !(-90.0..=90.0).contains(&value) {
            return Err(AstroError::InvalidDmsFormat {
                input: format!("{}", value),
                expected: "latitude in range [-90, 90] degrees"
            });
        }
    } else {
        // Normalize longitude to [-180, 180] but allow reasonable values
        if value.abs() > 360.0 {
            return Err(AstroError::InvalidDmsFormat {
                input: format!("{}", value),
                expected: "reasonable longitude value"
            });
        }
        // Normalize to standard range
        while value > 180.0 {
            value -= 360.0;
        }
        while value <= -180.0 {
            value += 360.0;
        }
    }
    
    // Final validation for finite values (security)
    if !value.is_finite() {
        return Err(AstroError::InvalidDmsFormat {
            input: "NaN/Infinity".to_string(),
            expected: "finite coordinate value"
        });
    }
    
    Ok(value)
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_optimized_parsing_performance() {
        // Test the optimized parser against various inputs
        let test_cases = vec![
            ("40.7128", true, 40.7128),
            ("40°42'46\"N", true, 40.712777777777777),
            ("4h56m27s", false, 74.1125),
            ("4042.767N", true, 40.71278333333333),
            ("-74.0060", false, -74.006),
        ];
        
        for (input, is_lat, expected) in test_cases {
            let result = parse_coordinate_optimized(input, is_lat).unwrap();
            assert!((result - expected).abs() < 1e-10, 
                   "Failed for {}: got {}, expected {}", input, result, expected);
        }
    }
    
    #[test]
    fn test_dos_protection() {
        // Test that malicious inputs are rejected quickly
        let malicious_inputs = vec![
            "a".repeat(1000),  // Too long
            "d".repeat(100) + "m" + &"s".repeat(100),  // Pattern explosion
            "123".repeat(50) + "°",  // Repetitive pattern
        ];
        
        for input in malicious_inputs {
            let start = std::time::Instant::now();
            let _result = parse_coordinate_optimized(&input, true);
            let elapsed = start.elapsed();
            
            // Should complete within 1ms for DoS protection
            assert!(elapsed.as_millis() < 1, 
                   "DoS protection failed: {} took {:?}", &input[..20.min(input.len())], elapsed);
        }
    }
    
    #[test]
    fn test_security_bounds() {
        // Test extreme coordinate values
        let extreme_values = vec![
            ("999.999", true, false),  // Invalid latitude
            ("400.0", false, false),   // Invalid longitude  
            ("NaN", true, false),      // Non-finite
            ("Infinity", false, false), // Non-finite
        ];
        
        for (input, is_lat, should_pass) in extreme_values {
            let result = parse_coordinate_optimized(input, is_lat);
            if should_pass {
                assert!(result.is_ok(), "Should accept {}", input);
            } else {
                assert!(result.is_err(), "Should reject {}", input);
            }
        }
    }
}

/// Performance comparison function for benchmarking
pub fn benchmark_comparison() {
    use std::time::Instant;
    
    let test_inputs = vec![
        "40.7128N", "74.0060W", "40°42'46\"", "4h56m27s", 
        "40d42m46s", "4042.767N", "-92.3009", "51.5074",
    ];
    
    // Benchmark optimized version
    let start = Instant::now();
    for _ in 0..10000 {
        for input in &test_inputs {
            let _ = parse_coordinate_optimized(input, true);
        }
    }
    let optimized_time = start.elapsed();
    
    println!("Optimized parsing: {:?} for {} iterations", 
             optimized_time, 10000 * test_inputs.len());
    println!("Rate: {:.0} parses/sec", 
             (10000 * test_inputs.len()) as f64 / optimized_time.as_secs_f64());
}