This is an archive of the discontinued LLVM Phabricator instance.

[support] Some improvements to StringSwitch
ClosedPublic

Authored by zturner on Sep 16 2016, 3:08 PM.

Details

Summary

This patch does a number of things:

  1. Adds case-insensitive versions of all case functions. This means CaseLower, StartsWithLower, EndsWithLower, and CasesLower.
  2. Converts the Cases function to use variadic templates. Thanks to rsmith@ for the magic incantation to make this work.
  3. Adds an alternative to Default() that returns an llvm::Expected<T>. This is useful when you Default is insufficient to determine that a switch fell off the end.
  4. Adds unit tests for the new case-insensitive versions of the function, as well as the old ones (there were previously 0 unit tests for StringSwitch).

Diff Detail

Repository
rL LLVM

Event Timeline

zturner updated this revision to Diff 71706.Sep 16 2016, 3:08 PM
zturner retitled this revision from to [support] Some improvements to StringSwitch.
zturner updated this object.
zturner added reviewers: rsmith, chandlerc.
zturner added a subscriber: llvm-commits.

Sadly, the variadic template idea died once I learned that a temporary is being created in the variadic function, and chained calls dont' work as a result. I will go back to using multiple methods :(

zturner updated this revision to Diff 71723.Sep 16 2016, 7:07 PM

Removed variadic template.

zturner updated this revision to Diff 73210.Oct 1 2016, 8:51 PM
zturner edited reviewers, added: ruiu; removed: chandlerc, rsmith.

Rebase against ToT

Could you split this appart? I think it will be much easier to review.

Suggested split and ordering:

  1. Add basic unit tests. Should be super easy patch.
  2. Switch to variadic templates with associated magic. Should have a nice simple unit test that showcases what is going on with this patch. And the unit tests from #1 should clearly show that it doesn't break things.
  3. Add the case insensitive case methods.
  4. Add the variant of Default. However, I wouldn't use Error and Expected here. Those seem both heavyweight and indicative of an *error* as opposed to just detecting whether or not the switch fell off the end. I feel like Optional would be a better fit?

Clearly #3 and #4 can be done in parallel. Happy to discuss any high level issues here to avoid re-writing stuff again and again in rebased patches just to change the underlying design.

Sadly, the variadic template approach died after Richard and I found out that it couldn't be made to work. So all we've got now is the addition of Lower versions of the functions. I just realized that the test file didn't make it to the review, so I'll get that uploaded and change to using Optional. Do you think it's still worth splitting if we're not doing the variadic templates?

zturner updated this revision to Diff 73295.Oct 3 2016, 9:38 AM

Here's the updated patch, including Chandler's suggestion of using Optional instead of Error. I thought about this some more and I don't see the value-add associated with splitting this apart. Primarily because I abandoned the variadic templates idea. As such, the Lower versions of the functions must go in at the same time as the Lower versions of the tests, and those two things constitue 90% of the entire patch (the rest are just case-sensitive versions of the tests for existing functions which have not changed in this patch).

Let me know if you disagree.

ruiu edited edge metadata.Oct 3 2016, 9:57 AM

I wonder if we need OrNone. Isn't it equivalent to .Default(None)?

zturner added a comment.EditedOct 3 2016, 11:07 AM
In D24686#559270, @ruiu wrote:

I wonder if we need OrNone. Isn't it equivalent to .Default(None)?

It works if we add an additional overload of Default() like this:

Optional<R> Default(llvm::NoneType N) const {
  if (Result)
    return *Result;
  return None;
}

If people prefer that I can go with that instead of OrNone

Here's the updated patch, including Chandler's suggestion of using Optional instead of Error. I thought about this some more and I don't see the value-add associated with splitting this apart. Primarily because I abandoned the variadic templates idea. As such, the Lower versions of the functions must go in at the same time as the Lower versions of the tests, and those two things constitue 90% of the entire patch (the rest are just case-sensitive versions of the tests for existing functions which have not changed in this patch).

Let me know if you disagree.

I kind of do... It makes the code review and any potential reverts much more clear IMO.

zturner updated this revision to Diff 73327.Oct 3 2016, 1:15 PM
zturner edited edge metadata.

Unit tests for the case-sensitive versions were removed and submitted separately. The Optional variant of the function is removed and will be submitted later. This patch only contains case-insensitive versions of the functions with associated unit tests.

ruiu accepted this revision.Oct 3 2016, 4:57 PM
ruiu edited edge metadata.

LGTM

This revision is now accepted and ready to land.Oct 3 2016, 4:57 PM
chandlerc accepted this revision.Oct 4 2016, 12:11 AM
chandlerc added a reviewer: chandlerc.

LGTM as well. I like the FooLower pattern. While somewhat verbose and repetitive, it seems much more clear and obvious to the reader. It is also much simpler.

This revision was automatically updated to reflect the committed changes.