|
| 1 | +use super::*; |
| 2 | + |
| 3 | +// Helper to negate a Rain Float hex string while keeping full precision by |
| 4 | +// operating on the binary representation directly. |
| 5 | +fn float_negate_hex_to_hex(input_hex: &str) -> Result<String, String> { |
| 6 | + let trimmed = input_hex.trim(); |
| 7 | + |
| 8 | + if trimmed.is_empty() { |
| 9 | + return Err("Empty string is not a valid hex number".to_string()); |
| 10 | + } |
| 11 | + |
| 12 | + // Parse the input hex into a Float |
| 13 | + let float_val = |
| 14 | + Float::from_hex(trimmed).map_err(|e| format!("Failed to parse Float hex: {e}"))?; |
| 15 | + |
| 16 | + // Negate the float directly to avoid any formatting or precision loss. |
| 17 | + let neg_float = (-float_val).map_err(|e| format!("Failed to negate Float value: {e}"))?; |
| 18 | + |
| 19 | + // Return as hex string |
| 20 | + Ok(neg_float.as_hex()) |
| 21 | +} |
| 22 | + |
| 23 | +// SQLite scalar function wrapper: FLOAT_NEGATE(hex_text) |
| 24 | +pub unsafe extern "C" fn float_negate( |
| 25 | + context: *mut sqlite3_context, |
| 26 | + argc: c_int, |
| 27 | + argv: *mut *mut sqlite3_value, |
| 28 | +) { |
| 29 | + if argc != 1 { |
| 30 | + sqlite3_result_error( |
| 31 | + context, |
| 32 | + c"FLOAT_NEGATE() requires exactly 1 argument".as_ptr(), |
| 33 | + -1, |
| 34 | + ); |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + // Return early for NULL inputs using the documented type check. |
| 39 | + if sqlite3_value_type(*argv) == SQLITE_NULL { |
| 40 | + sqlite3_result_null(context); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + // Get the text value (now known to be non-NULL). |
| 45 | + let value_ptr = sqlite3_value_text(*argv); |
| 46 | + |
| 47 | + let value_cstr = CStr::from_ptr(value_ptr as *const c_char); |
| 48 | + let value_str = match value_cstr.to_str() { |
| 49 | + Ok(value_str) => value_str, |
| 50 | + Err(_) => { |
| 51 | + sqlite3_result_error(context, c"invalid UTF-8".as_ptr(), -1); |
| 52 | + return; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + match float_negate_hex_to_hex(value_str) { |
| 57 | + Ok(result_hex) => { |
| 58 | + if let Ok(result_cstr) = CString::new(result_hex) { |
| 59 | + sqlite3_result_text( |
| 60 | + context, |
| 61 | + result_cstr.as_ptr(), |
| 62 | + result_cstr.as_bytes().len() as c_int, |
| 63 | + SQLITE_TRANSIENT(), |
| 64 | + ); |
| 65 | + } else { |
| 66 | + sqlite3_result_error(context, c"Failed to create result string".as_ptr(), -1); |
| 67 | + } |
| 68 | + } |
| 69 | + Err(e) => match CString::new(e) { |
| 70 | + Ok(error_msg) => { |
| 71 | + sqlite3_result_error(context, error_msg.as_ptr(), -1); |
| 72 | + } |
| 73 | + Err(_) => { |
| 74 | + sqlite3_result_error( |
| 75 | + context, |
| 76 | + c"Error message contained interior NUL".as_ptr(), |
| 77 | + -1, |
| 78 | + ); |
| 79 | + } |
| 80 | + }, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(test)] |
| 85 | +mod tests { |
| 86 | + use super::*; |
| 87 | + use wasm_bindgen_test::*; |
| 88 | + |
| 89 | + #[wasm_bindgen_test] |
| 90 | + fn test_float_negate_hex_to_hex_pos_to_neg() { |
| 91 | + let pos_hex = Float::parse("1.5".to_string()).unwrap().as_hex(); |
| 92 | + let expected_neg_hex = Float::parse("-1.5".to_string()).unwrap().as_hex(); |
| 93 | + let out = float_negate_hex_to_hex(&pos_hex).unwrap(); |
| 94 | + assert_eq!(out, expected_neg_hex); |
| 95 | + } |
| 96 | + |
| 97 | + #[wasm_bindgen_test] |
| 98 | + fn test_float_negate_hex_to_hex_neg_to_pos() { |
| 99 | + let neg_hex = Float::parse("-2.25".to_string()).unwrap().as_hex(); |
| 100 | + let expected_pos_hex = Float::parse("2.25".to_string()).unwrap().as_hex(); |
| 101 | + let out = float_negate_hex_to_hex(&neg_hex).unwrap(); |
| 102 | + assert_eq!(out, expected_pos_hex); |
| 103 | + } |
| 104 | + |
| 105 | + #[wasm_bindgen_test] |
| 106 | + fn test_float_negate_hex_to_hex_zero() { |
| 107 | + let zero_hex = Float::parse("0".to_string()).unwrap().as_hex(); |
| 108 | + let expected_zero_hex = Float::parse("0".to_string()).unwrap().as_hex(); |
| 109 | + let out = float_negate_hex_to_hex(&zero_hex).unwrap(); |
| 110 | + assert_eq!(out, expected_zero_hex); |
| 111 | + } |
| 112 | + |
| 113 | + #[wasm_bindgen_test] |
| 114 | + fn test_float_negate_hex_to_hex_high_precision() { |
| 115 | + let input = "300.123456789012345678"; |
| 116 | + let in_hex = Float::parse(input.to_string()).unwrap().as_hex(); |
| 117 | + let expected_hex = Float::parse(format!("-{input}")).unwrap().as_hex(); |
| 118 | + let out = float_negate_hex_to_hex(&in_hex).unwrap(); |
| 119 | + assert_eq!(out, expected_hex); |
| 120 | + } |
| 121 | + |
| 122 | + #[wasm_bindgen_test] |
| 123 | + fn test_float_negate_hex_to_hex_whitespace() { |
| 124 | + let in_hex = Float::parse("10".to_string()).unwrap().as_hex(); |
| 125 | + let wrapped = format!(" {in_hex} "); |
| 126 | + let expected_hex = Float::parse("-10".to_string()).unwrap().as_hex(); |
| 127 | + let out = float_negate_hex_to_hex(&wrapped).unwrap(); |
| 128 | + assert_eq!(out, expected_hex); |
| 129 | + } |
| 130 | + |
| 131 | + #[wasm_bindgen_test] |
| 132 | + fn test_float_negate_hex_to_hex_invalid() { |
| 133 | + assert!(float_negate_hex_to_hex("0XBADHEX").is_err()); |
| 134 | + assert!(float_negate_hex_to_hex("").is_err()); |
| 135 | + assert!(float_negate_hex_to_hex("not_hex").is_err()); |
| 136 | + } |
| 137 | +} |
0 commit comments