Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created April 3, 2018 16:43
Show Gist options
  • Save alexcrichton/5b456d495d6fe1df46a158754565c7a5 to your computer and use it in GitHub Desktop.
Save alexcrichton/5b456d495d6fe1df46a158754565c7a5 to your computer and use it in GitHub Desktop.
use std::fs::*;
use std::io::*;
use std::path::Path;
fn main() {
stabilize_dir("coresimd/x86".as_ref());
stabilize_dir("coresimd/x86_64".as_ref());
// stabilize_file("coresimd/x86/xsave.rs".as_ref());
}
fn stabilize_dir(p: &Path) {
for entry in p.read_dir().unwrap() {
let entry = entry.unwrap().path();
if entry.is_dir() {
stabilize_dir(&entry);
} else {
stabilize_file(&entry);
}
}
}
fn stabilize_file(p: &Path) {
let mut contents = String::new();
File::open(p).unwrap().read_to_string(&mut contents).unwrap();
let contents = stabilize(&contents);
File::create(p).unwrap().write_all(contents.as_bytes()).unwrap();
}
fn stabilize(s: &str) -> String {
let mut prev = Vec::new();
let mut new = String::new();
let mut in_block = false;
for line in s.lines() {
prev.push(line);
if line.is_empty() && !in_block {
for l in prev.drain(..) {
new.push_str(l);
new.push_str("\n");
}
continue
}
if line.ends_with("{") && !in_block {
in_block = true;
}
if in_block {
if line != "}" {
continue
}
in_block = false;
} else if !line.starts_with("pub const") || !line.ends_with(";") {
continue
}
if prev.iter().any(|l| l.contains("__m64")) {
for l in prev.drain(..) {
new.push_str(l);
new.push_str("\n");
}
}
let already_stable = prev.iter().any(|l| l.contains("stable("));
let link = prev.iter()
.find(|l| l.contains("pub unsafe fn"))
.map(|s| {
let start = s.find("fn ").unwrap();
let end = s.find("(").unwrap();
&s[start + 3..end]
})
.map(|name| {
format!("https://software.intel.com/sites/landingpage\
/IntrinsicsGuide\
/#text={}", name)
});
let mut prev_doc = false;
let mut has_intel_doc = false;
for l in prev.drain(..) {
if !already_stable && l.starts_with("pub") {
new.push_str("#[stable(feature = \"simd_x86\", since = \"1.27.0\")]\n");
}
let is_doc = l.starts_with("///");
if !is_doc && prev_doc && !has_intel_doc {
if let Some(ref link) = link {
new.push_str("///\n");
new.push_str(&format!("/// [Intel's documentation]({})\n", link));
}
}
prev_doc = is_doc;
has_intel_doc = has_intel_doc || l.contains("Intel's documentation");
new.push_str(l);
new.push_str("\n");
}
}
return new
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment