增加了示例fiber_id 修改了rust中关于字符串的处理

This commit is contained in:
xin
2025-06-12 09:54:15 +08:00
parent 1aba741f67
commit a500813e45
3 changed files with 19 additions and 1 deletions

View File

@ -105,6 +105,7 @@ pub fn read_iris_file_example() {
// Write to a temporary file
let path = "iris_data_example.iris";
// let path = "output_iris_data.iris";
// {
// let file = std::fs::File::create(path).unwrap();
// let mut writer = std::io::BufWriter::new(file);

View File

@ -26,6 +26,7 @@ pub fn read_spectral_info<R: Read>(reader: &mut R) -> Result<SpectralInfo> {
let mut sensor_buf = [0u8; 50];
reader.read_exact(&mut sensor_buf)?;
info.sensor_id = String::from_utf8_lossy(&sensor_buf).trim_end_matches('\0').to_string();
info.sensor_id = remove_after_null_split_once( info.sensor_id);
// Read wave coefficients
for i in 0..4 {
@ -49,6 +50,7 @@ pub fn read_image_info<R: Read>(reader: &mut R) -> Result<ImageInfo> {
let mut name_buf = [0u8; 100];
reader.read_exact(&mut name_buf)?;
info.name = String::from_utf8_lossy(&name_buf).trim_end_matches('\0').to_string();
info.name = remove_after_null_split_once(info.name);
// Read collection time
info.collection_time = read_time(reader)?;
@ -202,7 +204,15 @@ pub fn read_other_info<R: Read>(reader: &mut R) -> Result<OtherInfo> {
Ok(info)
}
fn remove_after_null_split_once(s: String) -> String {
if let Some((before_null, _after_null)) = s.split_once('\0') {
// 返回 \0 之前的部分
before_null.to_string()
} else {
// 如果没有找到 \0就返回原始 String
s
}
}
pub fn read_spectral_data<R: Read>(reader: &mut R) -> Result<SpectralData> {
let mut data = SpectralData::new();
@ -213,10 +223,12 @@ pub fn read_spectral_data<R: Read>(reader: &mut R) -> Result<SpectralData> {
name_buf[99] = 0; // Ensure null termination
let temp= String::from_utf8_lossy(&name_buf);
data.name = temp.trim_end_matches('\0').to_string();
data.name = remove_after_null_split_once(data.name);
let mut sensor_buf = [0u8; 50];
reader.read_exact(&mut sensor_buf)?;
data.sensor_id = String::from_utf8_lossy(&sensor_buf).trim_end_matches('\0').to_string();
data.sensor_id = remove_after_null_split_once(data.sensor_id);
let mut uint8_buf = [0u8; 1];
reader.read_exact(&mut uint8_buf)?;