成果物
日本海洋データセンターから提供されている、潮汐データの欠測潮高値はスペースが含まれておらず、 そのまま解析等に使用すると使い勝手が悪いので、欠測値を4桁ごとに区切りスペースを挿入するCLIツールです。
開発意図
兄が通っている大学で、上記のデータ変換を手動で行っていたので、
ツールで自動化し、楽に正規化できるようにしたかったため。
詳細情報
技術選定
- 言語: Rust v1.78.0
- 使用クレート: Clap v4.5.4
変換処理
/// Converts missing digits to normal digits in the given text data. /// /// ## Arguments /// /// * `txt_data` - The text data to convert. /// /// ## Returns /// /// * The converted text data with missing digits replaced by normal digits. pub fn convert_missing_to_normal(&self, txt_data: String) -> String { let mut result = String::new(); for (_index, line) in txt_data.lines().enumerate() { // 行の反転を行い、探索方向を逆順にする let reversed_line: String = line.chars().rev().collect(); // 行の中に偶数桁で連続している9があったら、4桁ごと(前に)にスペースを挿入 let mut converted_line = String::new(); let mut count = 0; let mut chars = reversed_line.chars().peekable(); // 各行の文字を逆順で探索を行う while let Some(c) = chars.next() { if c == '9' { count += 1; } else { count = 0; } converted_line.push(c); if count % 4 == 0 && count > 0 && chars.peek().is_some() { converted_line.push(' '); } } result.push_str(&converted_line.chars().rev().collect::<String>()); result.push('\n'); } result }
実装にあたってつまずいた部分
最初は、正規表現を使用していたのですが、急ぎの開発だった為、
個人的に実装しやすかった欠測値判定の繰り返し処理での実装に切り替えました。