CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Tools/langtool/src/section.rs
Views: 1401
// Super simplified ini file processor.1// Doesn't even bother with understanding comments.2// Just understands section headings and3// keys and values, split by ' = '.45#[derive(Debug, Clone)]6pub struct Section {7pub name: String,8pub title_line: String,9pub lines: Vec<String>,10}1112impl Section {13pub fn remove_line(&mut self, key: &str) -> Option<String> {14let mut remove_index = None;15for (index, line) in self.lines.iter().enumerate() {16let prefix = if let Some(pos) = line.find(" =") {17&line[0..pos]18} else {19continue;20};2122if prefix.eq_ignore_ascii_case(key) {23remove_index = Some(index);24break;25}26}2728if let Some(remove_index) = remove_index {29Some(self.lines.remove(remove_index))30} else {31None32}33}3435pub fn get_line(&mut self, key: &str) -> Option<String> {36for line in self.lines.iter() {37let prefix = if let Some(pos) = line.find(" =") {38&line[0..pos]39} else {40continue;41};4243if prefix.eq_ignore_ascii_case(key) {44return Some(line.clone());45}46}47None48}4950pub fn insert_line_if_missing(&mut self, line: &str) -> bool {51let prefix = if let Some(pos) = line.find(" =") {52&line[0..pos + 2]53} else {54return false;55};5657// Ignore comments when copying lines.58if prefix.starts_with('#') {59return false;60}61// Need to decide a policy for these.62if prefix.starts_with("translators") {63return false;64}65let prefix = prefix.to_owned();6667for iter_line in &self.lines {68if iter_line.starts_with(&prefix) {69// Already have it70return false;71}72}7374// Now try to insert it at an alphabetic-ish location.75let prefix = prefix.to_ascii_lowercase();7677// Then, find a suitable insertion spot78for (i, iter_line) in self.lines.iter().enumerate() {79if iter_line.to_ascii_lowercase() > prefix {80println!("Inserting line {} into {}", line, self.name);81self.lines.insert(i, line.to_owned());82return true;83}84}8586for i in (0..self.lines.len()).rev() {87if self.lines[i].is_empty() {88continue;89}90println!("Inserting line {} into {}", line, self.name);91self.lines.insert(i + 1, line.to_owned());92return true;93}9495println!("failed to insert {}", line);96true97}9899pub fn rename_key(&mut self, old: &str, new: &str) {100let prefix = old.to_owned() + " =";101let mut found_index = None;102for (index, line) in self.lines.iter().enumerate() {103if line.starts_with(&prefix) {104found_index = Some(index);105}106}107if let Some(index) = found_index {108let line = self.lines.remove(index);109let mut right_part = line.strip_prefix(&prefix).unwrap().to_string();110if right_part.trim() == old.trim() {111// Was still untranslated - replace the translation too.112right_part = format!(" {}", new);113}114let line = new.to_owned() + " =" + &right_part;115self.insert_line_if_missing(&line);116} else {117let name = &self.name;118println!("didn't find a line starting with {prefix} in section {name}");119}120}121122pub fn sort(&mut self) {123self.lines.sort();124}125126pub fn comment_out_lines_if_not_in(&mut self, other: &Section) {127// Brute force (O(n^2)). Bad but not a problem.128129for line in &mut self.lines {130let prefix = if let Some(pos) = line.find(" =") {131&line[0..pos + 2]132} else {133// Keep non-key lines.134continue;135};136if prefix.starts_with("Font") || prefix.starts_with('#') {137continue;138}139if !other.lines.iter().any(|line| line.starts_with(prefix)) && !prefix.contains("URL") {140println!("Commenting out from {}: {line}", other.name);141// Comment out the line.142*line = "#".to_owned() + line;143}144}145}146147pub fn remove_lines_if_not_in(&mut self, other: &Section) {148// Brute force (O(n^2)). Bad but not a problem.149150self.lines.retain(|line| {151let prefix = if let Some(pos) = line.find(" =") {152&line[0..pos + 2]153} else {154// Keep non-key lines.155return true;156};157if prefix.starts_with("Font") || prefix.starts_with('#') {158return true;159}160161// keeps the line if this expression returns true.162other.lines.iter().any(|line| line.starts_with(prefix))163});164}165}166167168