Add parseArchTag and fix parseArchSub

master
Layne Gustafson 2019-12-17 09:43:05 -05:00 committed by Andrew Kelley
parent 8f191e0166
commit 8ac138a318
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
1 changed files with 26 additions and 1 deletions

View File

@ -499,7 +499,7 @@ pub const Target = union(enum) {
pub fn parseArchSub(text: []const u8) ParseArchSubError!Arch {
const info = @typeInfo(Arch);
inline for (info.Union.fields) |field| {
if (mem.eql(u8, text, field.name)) {
if (text.len >= field.name.len and mem.eql(u8, text[0..field.name.len], field.name)) {
if (field.field_type == void) {
return @as(Arch, @field(Arch, field.name));
} else {
@ -517,6 +517,31 @@ pub const Target = union(enum) {
return error.UnknownArchitecture;
}
pub fn parseArchTag(text: []const u8) ParseArchSubError!@TagType(Arch) {
const info = @typeInfo(Arch);
inline for (info.Union.fields) |field| {
if (text.len >= field.name.len and mem.eql(u8, text[0..field.name.len], field.name)) {
if (text.len == field.name.len) return @as(@TagType(Arch), @field(Arch, field.name));
if (field.field_type == void) {
return error.UnknownArchitecture;
}
const sub_info = @typeInfo(field.field_type);
inline for (sub_info.Enum.fields) |sub_field| {
const combined = field.name ++ sub_field.name;
if (mem.eql(u8, text, combined)) {
return @as(@TagType(Arch), @field(Arch, field.name));
}
}
return error.UnknownSubArchitecture;
}
}
return error.UnknownArchitecture;
}
pub fn parseOs(text: []const u8) !Os {
const info = @typeInfo(Os);
inline for (info.Enum.fields) |field| {