Skip to content

Instantly share code, notes, and snippets.

@paulcarroty
Last active October 9, 2020 11:26
Show Gist options
  • Save paulcarroty/f7e12f93e1c209230ba642b0d05bffbf to your computer and use it in GitHub Desktop.
Save paulcarroty/f7e12f93e1c209230ba642b0d05bffbf to your computer and use it in GitHub Desktop.
Rust cutting long utf strings
// https://play.rust-lang.org/?version=beta&mode=debug&edition=2018&gist=d99b8ff32cda06ad450c2de61b2053bf
fn main() {
// Limit file name to 200 bytes or less if char is boundary
let mut title_raw = "🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴🚴";
// let mut title_raw = "🚴Billie Eilish - text=\"Everything I Wanted\" song_spot=\"M\" MediaBaseId=\"2548688\" itunesTrackId=\"0\" amgTrackId=\"-1\" amgArtistId=\"0\" TAID=\"31132274\" TPID=\"83782299\" cartcutId=\"0416383001\" amgArtworkURL=\"http://image.iheart.com/bell-ingestion-pipeline-production-umg/full/00602508508462_20191114020958327/19UM1IM00404_T1_cvrart.jpg\" length=\"00:04:02\" unsID=\"-1\" spotInstanceId=\"-1\"🚴";
// let mut title_raw = "Billie Eilish - Everything I Wanted";
let mut limit = 200;
while (title_raw.len() > 200) && (!title_raw.is_char_boundary(limit)) {
limit -= 1;
}
title_raw = &title_raw[..limit.min(title_raw.len())];
let title = title_raw.to_string();
println!("{}", title_raw);
println!("{}", title_raw.len());
println!("{}", title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment