31 lines
798 B
Rust
31 lines
798 B
Rust
use std::collections::HashMap;
|
|
|
|
pub struct ConstantsTable {
|
|
string_counter: usize,
|
|
strings_to_names: HashMap<String, String>,
|
|
}
|
|
|
|
impl ConstantsTable {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
string_counter: 0,
|
|
strings_to_names: HashMap::new(),
|
|
}
|
|
}
|
|
|
|
pub fn insert_string(&mut self, s: &str) -> String {
|
|
let name = format!("s_{}", self.string_counter);
|
|
self.string_counter += 1;
|
|
self.strings_to_names.insert(s.into(), name.clone());
|
|
name
|
|
}
|
|
|
|
pub fn string_constants(&self) -> HashMap<String, String> {
|
|
let mut constants = HashMap::new();
|
|
self.strings_to_names.iter().for_each(|(content, name)| {
|
|
constants.insert(name.clone(), content.clone());
|
|
});
|
|
constants
|
|
}
|
|
}
|