Check name string for allowed characters
This commit is contained in:
parent
73d99df3ab
commit
1b3c553661
1 changed files with 45 additions and 1 deletions
|
@ -108,7 +108,26 @@ impl LeaderBoardEntry {
|
|||
pub fn change_name(&mut self, name: String) -> Result<()> {
|
||||
self.name = name;
|
||||
|
||||
self.name_label.set_text(&self.name)
|
||||
self.name_label.set_text(Self::check_string(&self.name))
|
||||
}
|
||||
|
||||
fn check_string(s: &str) -> String {
|
||||
std::str::from_utf8(
|
||||
&s.as_bytes()
|
||||
.iter()
|
||||
.filter(|&&b| {
|
||||
(b > 96 && b < 123) // small letters
|
||||
|| (b > 64 && b < 91) // big letters
|
||||
|| (b > 47 && b < 58) // numbers
|
||||
|| b == 45 // dash
|
||||
|| b == 95 // underscore
|
||||
|| b == 32 // whitespace
|
||||
})
|
||||
.map(|&b| b)
|
||||
.collect::<Vec<u8>>(),
|
||||
)
|
||||
.unwrap()
|
||||
.to_string()
|
||||
}
|
||||
|
||||
pub fn snippet(&self) -> Arc<GuiSnippet> {
|
||||
|
@ -253,3 +272,28 @@ impl LeaderBoardEntry {
|
|||
self.place_updated = false;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
#[test]
|
||||
fn test_string_replacement() {
|
||||
let s_orig = "blaü";
|
||||
let s = super::LeaderBoardEntry::check_string(s_orig);
|
||||
println!("{} {:?}", s_orig, s);
|
||||
|
||||
for b in s.as_bytes() {
|
||||
println!("{}", b)
|
||||
}
|
||||
|
||||
println!();
|
||||
|
||||
let s2_orig = "az AZ 09 # - _";
|
||||
let s2 = super::LeaderBoardEntry::check_string(s2_orig);
|
||||
println!("{} {:?}", s2_orig, s2);
|
||||
|
||||
for b in s2.as_bytes() {
|
||||
println!("{}", *b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue