Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse without use of regex to work around dependency conflict #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ keywords = ["riscv"]
license = "ISC"

[dependencies]
regex = "1"
lazy_static = "1"
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
extern crate lazy_static;
extern crate regex;

use lazy_static::lazy_static;
use regex::Regex;
use std::collections::HashSet;
use std::str::FromStr;

Expand All @@ -17,8 +15,6 @@ pub struct Target {
const EXTENSION_ORDER: &str = "eimafdqlcbjtpvn";

lazy_static! {
static ref TARGET_REGEX: Regex =
regex::Regex::new("riscv(\\d+)([a-z]+)(:?[^-]*)-(.*)").unwrap();
static ref REGISTERED_EXTENSIONS: HashSet<char> = {
let mut exts = HashSet::new();
for e in EXTENSION_ORDER.chars() {
Expand All @@ -30,14 +26,22 @@ lazy_static! {

impl Target {
pub fn from_target_str(target_str: &str) -> Self {
let target_captures = TARGET_REGEX
.captures(target_str)
.expect("RISC-V target doesn't match the pattern 'riscv(\\d+)([a-z]+)-(.*)'");

let bits = u32::from_str(&target_captures[1]).unwrap();
let mut target_flags: HashSet<char> = target_captures[2].to_lowercase().chars().collect();
let suffix = target_captures[3].to_owned();
let vendor_os = target_captures[4].to_owned();
let parts: Vec<&str> = target_str.splitn(2, '-').collect();
if parts.len() < 2 {
panic!("Target specifier must have at least two parts separated by '-'");
}

if !parts[0].starts_with("riscv") {
panic!("RISC-V target doesn't start with riscv");
}
let arch = &parts[0][5..];
let ext_idx = arch.find(|ch| !char::is_digit(ch, 10)).unwrap_or(arch.len());
let suffix_idx = arch[ext_idx..].find(|ch| ch < 'a' || ch > 'z').unwrap_or(arch.len() - ext_idx) + ext_idx;

let bits = u32::from_str(&arch[0..ext_idx]).expect("riscv must be followed by number of bits");
let mut target_flags: HashSet<char> = arch[ext_idx..suffix_idx].to_lowercase().chars().collect();
let suffix = arch[suffix_idx..].to_owned();
let vendor_os = parts[1].to_owned();

let mut base_extension = 'e';

Expand Down