Parse "#REPLACE" macro in MML.
This commit is contained in:
		
							parent
							
								
									bc7f16af99
								
							
						
					
					
						commit
						37aa237209
					
				
					 5 changed files with 32 additions and 3 deletions
				
			
		
							
								
								
									
										2
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							
							
						
						
									
										2
									
								
								Cargo.lock
									
										
									
										generated
									
									
									
								
							| 
						 | 
					@ -3,5 +3,5 @@
 | 
				
			||||||
version = 3
 | 
					version = 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[[package]]
 | 
					[[package]]
 | 
				
			||||||
name = "quick-start"
 | 
					name = "slamshuffle"
 | 
				
			||||||
version = "0.1.0"
 | 
					version = "0.1.0"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
[package]
 | 
					[package]
 | 
				
			||||||
name = "quick-start"
 | 
					name = "slamshuffle"
 | 
				
			||||||
version = "0.1.0"
 | 
					version = "0.1.0"
 | 
				
			||||||
edition = "2021"
 | 
					edition = "2021"
 | 
				
			||||||
license = "MIT"
 | 
					license = "MIT"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  description = "Build a cargo project";
 | 
					  description = "Music randomizer for Worlds Collide, based on johnnydmad";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  inputs = {
 | 
					  inputs = {
 | 
				
			||||||
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
 | 
					    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/lib.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1 @@
 | 
				
			||||||
 | 
					pub mod mfvi;
 | 
				
			||||||
							
								
								
									
										28
									
								
								src/mfvi.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								src/mfvi.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,28 @@
 | 
				
			||||||
 | 
					struct Translation<'a> {
 | 
				
			||||||
 | 
					    find: &'a str,
 | 
				
			||||||
 | 
					    replacement: &'a str,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn replace_chars(input: &str) -> String {
 | 
				
			||||||
 | 
					    let trs: Vec<Translation<'_>> = input
 | 
				
			||||||
 | 
					        .lines()
 | 
				
			||||||
 | 
					        .filter_map(|line| {
 | 
				
			||||||
 | 
					            line.strip_prefix("#REPLACE ")
 | 
				
			||||||
 | 
					                .and_then(|l| l.rsplit_once(' '))
 | 
				
			||||||
 | 
					                .and_then(|(first, second)| {
 | 
				
			||||||
 | 
					                    let length = std::cmp::min(first.len(), second.len());
 | 
				
			||||||
 | 
					                    Some(Translation {
 | 
				
			||||||
 | 
					                        find: &first[0..length],
 | 
				
			||||||
 | 
					                        replacement: &second[0..length],
 | 
				
			||||||
 | 
					                    })
 | 
				
			||||||
 | 
					                })
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					        .collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut new = input.to_string();
 | 
				
			||||||
 | 
					    for tr in trs {
 | 
				
			||||||
 | 
					        new = new.replace(tr.find, tr.replacement);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    new
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue