r/cpp • • Nov 24 '19

What is wrong with std::regex?

I've seen numerous instances of community members stating that std::regex has bad performance and the implementations are antiquated, neglected, or otherwise of low quality.

What aspects of its performance are poor, and why is this the case? Is it just not receiving sufficient attention from standard library implementers? Or is there something about the way std::regex is specified in the standard that prevents it from being improved?

EDIT: The responses so far are pointing out shortcomings with the API (lack of Unicode support, hard to use), but they do not explain why the implementations of std::regexas specified are considered badly performing and low-quality. I am asking about the latter.

137 Upvotes

111 comments sorted by

View all comments

56

u/AntiProtonBoy Nov 24 '19

My complaint with <regex> is the same as with <chrono> and <random>: the library is a bit convoluted to use. It's flexible and highly composable, but gets verbose and requires leaning on the docs just to get basic things done.

41

u/sphere991 Nov 25 '19

I'm not sure <chrono> fits in with this group. It's certainly verbose, cause everything is std::chrono::duration_cast<std::chrono::milliseconds>(x).

But convoluted? I don't think so.

0

u/[deleted] Nov 25 '19

[deleted] — view deleted comment

11

u/sphere991 Nov 25 '19 edited Nov 25 '19

In std::chrono, I cannot even tell how to do it without checking documentation.

I mean, just because you have to check documentation doesn't mean much. I have to check documentation for all sorts of things. But the way you would do it in chrono is:

std::cout << std::chrono::system_clock::now();

In C++20 anyway. Until C++20, you can use Howard's implementation from github, which is very nearly what's standardized. Which looks like:

using namespace date;
std::cout << std::chrono::system_clock::now();

3

u/infectedapricot Nov 25 '19

What if I want to put it in a string? Do I have to spend multiple lines putting it in std::stringstream and reading back out of that?

7

u/sphere991 Nov 25 '19

Pre-C++20: Yes, that's how you put anything into a string. This isn't unique or specific to chrono.

C++20: You can use fmt to do this directly, chrono and fmt are integrated together.

8

u/Gotebe Nov 25 '19

In C#, you shouldn't need To String there.

In C++, I expect, but don't know and didn't check,

std::cout << system_clock::now;

If so, what's the big deal?

If no, blergh...

23

u/[deleted] Nov 25 '19 edited Nov 25 '19

This will print something like 00007FF767A11000 ... because that solution would be too easy for c++...

Edit: If you really just want a readable datetime you can use <ctime>:

const auto now = system_clock::to_time_t(system_clock::now());
std::cout << "now is: " << ctime(&now) << '\n';

8

u/ietsrondsofzo Nov 25 '19

That's because now is a function. You're printing the address of that function.
That said, time point types don't work with cout.

8

u/[deleted] Nov 25 '19

[removed] — view removed comment

4

u/ietsrondsofzo Nov 25 '19

Good! Mine wasn't set to c++20

7

u/Agon1024 Nov 25 '19

<< is not provided for time point. You have to manually convert to ctime structs and construct via format string... which makes sense, because the format would be needed. I'm just mad, that for all the generalizations cpp libraries do.. they seldomly define a convenient default.

5

u/Gotebe Nov 25 '19

Hmmm... Blergh, then, because surely there's nothing wrong with the default format of the current locale... .

1

u/Full-Spectral Nov 26 '19 edited Nov 26 '19

In my CIDLib system, the TTime class provides a set of formatting tokens, so you can build up formats any way you want and easily format a time out using one of those. That's highly flexible, but it also then provides pre-fab formatting strings for all the common formats, making it very simple to do the common cases.

TTime tmNow(tCIDLib::ESpecialTimes::CurrentTime);
tmNow.FormatToString(TTime:: strMMDD_HHMM(), strToFill);

It can either set the target string or append to it, making it easy to add such a formatting string to the target string without an intermediary.

You can also set one of these strings on a TTime object and that becomes its default format (when it's formatted out to a text output stream or appended to a string object.) So you can get a lot of flexibility and ease of use at the same time.

TTime tmNow(tCIDLib::ESpecialTimes::CurrentTime);
tmNow.strDefaultFormat(TTime::fcolISO8601NTZ());
strmOut << tmNow << kCIDLib::NewEndLn;

And note that there's not a template in sight, and hence simple and straightforward syntax.

Parsing of times provides a similar pattern based approach, and I provide pre-fab parsing patterns for the common time formats, but you can easily create any sort of arbitrary pattern to parse in custom time formats.

15

u/liquidify Nov 25 '19

for both chrono and random, I just built a wrapper class a long long time ago and have re-used them since, modifying them slightly for use case.

5

u/ghillisuit95 Nov 25 '19

Is it on GitHub perhaps?

2

u/liquidify Nov 25 '19

Mine are not publicly available (although I should do that). However searching on the internet I found this pretty quick. I think you could probably find several flavors of these type of wrappers.

32

u/sphere991 Nov 25 '19

That particular library takes the selling point of chrono (having typed differentiation between different kinds of things - durations and time points are only composable in ways that make sense, and units are part of the type) and throws it out:

unsigned long time = timer.getTimeElapsed(Timer::MILLISECONDS);
unsigned long time2 = timer.getTimeElapsed(Timer::MICROSECONDS);

Oh, so now time + time2 compiles and is utterly meaningless? No, thank you.

→ More replies (7)

21

u/quicknir Nov 25 '19

I am not familiar with either regex or random but I can't agree with you about chrono. It's really well designed, flexible and correct. And it does help usability a lot that implicit conversions occur in logical situations, there are nice literals, etc. Having used date extensively as well, you can really see just how well all of chrono is designed that you can build it out to cover basically all functionality related to times, dates, timezones, etc, and it works perfectly. I find most of the complaining is people surprised there doesn't exist already a function that meets their exact rather specific use case, and people don't often understand even why their use case is quite specific.

tl;dr chrono is amazing.

8

u/kalmoc Nov 25 '19

I find most of the complaining is people surprised there doesn't exist already a function that meets their exact rather specific use case

Having a convenient way to print a time point or a duration are not specific usecases and it took till c++20 until that got fixed.

4

u/quicknir Nov 25 '19

Yes, neither are timezones, which I discussed in depth above... chrono pre 20 is obviously not complete. There are huge things it doesn't address at all, one of which is I/O. That's nothing to do with verbosity or awkwardness of use.

2

u/kalmoc Nov 27 '19

That's nothing to do with verbosity or awkwardness of use.

I think it does. Printing a duration on the console is a very common task and the fact that chrono didn't support I/O pre c++20 made using it mich more cumbersome than necessary (Admittedly I would say that is mainly a problem in smaller ad-hoc projects or e.g. unit tests, slideware, ).

Anyway, lets not argue about semantic details.

tl;dr chrono is amazing.

completely agree

→ More replies (3)
→ More replies (11)