Skip to content

Commit ca2b585

Browse files
authored
Merge pull request #135 from stm32-rs/mmap_description
replace %s in field array
2 parents a99c31c + 27d19e3 commit ca2b585

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

CHANGELOG-rust.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This changelog tracks the Rust `svdtools` project. See
55

66
## [Unreleased]
77

8+
* mmap: replace %s in field array
9+
810
## [v0.2.7] 2022-09-18
911

1012
* Print svdtools version on error, update dependencies

src/mmap/mmap_cli.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ fn derived_str(dname: &Option<String>) -> String {
8686
}
8787
}
8888

89+
fn replace_idx(name: &str, idx: &str) -> String {
90+
name.replace("[%s]", idx).replace("%s", idx)
91+
}
92+
8993
fn get_registers(
9094
base_address: u64,
9195
registers: Option<&Vec<RegisterCluster>>,
@@ -116,8 +120,8 @@ fn get_registers(
116120
let addr = str_utils::format_address(
117121
first_addr + (i as u64) * (d.dim_increment as u64),
118122
);
119-
let rname = r.name.replace("%s", &idx);
120-
let description = description.replace("%s", &idx);
123+
let rname = replace_idx(&r.name, &idx);
124+
let description = replace_idx(&description, &idx);
121125
let text = format!(
122126
"{addr} B REGISTER {rname}{derived}{access}: {description}"
123127
);
@@ -144,8 +148,8 @@ fn get_registers(
144148
for (i, idx) in d.indexes().enumerate() {
145149
let caddr = first_addr + (i as u64) * (d.dim_increment as u64);
146150
let addr = str_utils::format_address(caddr);
147-
let cname = c.name.replace("%s", &idx);
148-
let description = description.replace("%s", &idx);
151+
let cname = replace_idx(&c.name, &idx);
152+
let description = replace_idx(&description, &idx);
149153
let text =
150154
format!("{addr} B CLUSTER {cname}{derived}: {description}");
151155
mmap.push(text);
@@ -180,7 +184,8 @@ fn get_fields(register: &RegisterInfo, addr: &str, mmap: &mut Vec<String>) {
180184
for (i, idx) in d.indexes().enumerate() {
181185
let bit_offset = f.bit_range.offset + (i as u32) * d.dim_increment;
182186
let bit_width = f.bit_range.width;
183-
let fname = f.name.replace("%s", &idx);
187+
let fname = replace_idx(&f.name, &idx);
188+
let description = replace_idx(&description, &idx);
184189
let text = format!(
185190
"{addr} C FIELD {bit_offset:02}w{bit_width:02} {fname}{derived}{access}: {description}"
186191
);

0 commit comments

Comments
 (0)