-
Notifications
You must be signed in to change notification settings - Fork 261
Idea: Adding from_string
to enum metafunction
#1185
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
Changes from all commits
37535c0
9a25f87
8587a6d
e426805
918584b
7c02229
654fff0
9a73208
ab43b7d
0a4bd0d
d3fb6bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,6 +378,40 @@ using _uchar = unsigned char; // normally use u8 instead | |
|
||
namespace string_util { | ||
|
||
// Break a string_view into a vector of views of simple qidentifier | ||
// substrings separated by other characters | ||
auto split_string_list(std::string_view str) | ||
-> std::vector<std::string_view> | ||
{ | ||
std::vector<std::string_view> ret; | ||
|
||
auto is_id_char = [](char c) { | ||
return std::isalnum(c) || c == '_'; | ||
}; | ||
|
||
auto pos = 0; | ||
while( pos < std::ssize(str) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick, if you care: style difference between this |
||
// Skip non-alnum | ||
while (pos < std::ssize(str) && !is_id_char(str[pos])) { | ||
++pos; | ||
} | ||
auto start = pos; | ||
|
||
// Find the end of the current component | ||
while (pos < std::ssize(str) && is_id_char(str[pos])) { | ||
++pos; | ||
} | ||
|
||
// Add nonempty substring to the vector | ||
if (start < pos) { | ||
ret.emplace_back(str.substr(start, pos - start)); | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
|
||
// From https://stackoverflow.com/questions/216823/how-to-trim-a-stdstring | ||
|
||
// Trim from start (in place) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,12 +35,17 @@ main: () = { | |
x: skat_game = skat_game::clubs; | ||
x2 := skat_game::diamonds; | ||
x2 = x; | ||
x3 := skat_game::from_string("hearts"); | ||
x4 := skat_game::from_code("skat_game::hearts"); | ||
|
||
// if x == 9 { } // error, can't compare skat_game and integer | ||
// if x == rgb::red { } // error, can't compare skat_game and rgb color | ||
|
||
std::cout << "x.to_string() is (x.to_string())$\n"; | ||
std::cout << "x2.to_string() is (x2.to_string())$\n"; | ||
std::cout << "x3.to_string() is (x3.to_string())$\n"; | ||
std::cout << "x3.to_code() is (x3.to_code())$\n"; | ||
std::cout << "x4.to_string() is (x3.to_string())$\n"; | ||
|
||
std::cout << "with if else: "; | ||
if x == skat_game::diamonds { // ok, can compare two skat_games | ||
|
@@ -117,4 +122,14 @@ main: () = { | |
is (cpp2::has_flags(f2)) = "includes all f2's flags ('cached' and 'current')"; | ||
is _ = "something else"; | ||
} << "\n"; | ||
|
||
f_from_string := file_attributes::from_string("cached_and_current"); | ||
std::cout << "f_from_string is " << f_from_string.to_string() << "\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It bothers me slightly that both of these:
result in this:
I'm wondering if a bitwise enum That would make the first ones result in Also, should There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks!
What I've been doing with
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
My "nightmare scenario" is something like the creating an enum for the file permissions in
To clarify, I meant in the output string. It was just a thought, not for computer consumption, but more for human consumption. I was thinking that the user always has to spell it as Related to that, would it make sense to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
👍🏻 I was thinking of this myself when I used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I just pushed a commit to change Allowing qualification is doable but just makes reading and parsing harder. A poor person's way to make it a qualified string would be to prepend There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, it could be a thing. Python for example has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if there are use cases here, just things that came to mind as I was reading it. Maybe the "a single combined value if it exactly matches a combined value, and then if it doesn't, only include the bare flags" would be a separate function and it could have a |
||
|
||
f_from_string = file_attributes::from_string("(current, obsolete)"); | ||
std::cout << "f_from_string is " << f_from_string.to_string() << "\n"; | ||
std::cout << "f_from_string.to_code() is " << f_from_string.to_code() << "\n"; | ||
|
||
f_from_string = file_attributes::from_code("(file_attributes::cached | file_attributes::obsolete)"); | ||
std::cout << "f_from_string is " << f_from_string.to_string() << "\n"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hsutter Why do all of these lines start at column 0? There is leading whitespace in the metafunction. Is it because the whitespace is tabs, and those are being stripped somehow?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I don't know offhand.