2 ms·
> I have seen way too many hand rolled parsers on C, they may have been only 0.1% of the total code but they amounted for at least 50% of the bugs, along with t
by jstimpfle 4y ago
> I have seen way too many hand rolled parsers on C, they may have been only 0.1% of the total code but they amounted for at least 50% of the bugs, along with the constant manual memory management and badly initialized variables.
I have written a number of parsers for programming languages and can't recall ever having a string bug. It's all about creating the right abstractions. A parser is not about string handling! The typical (lexical) parser scans input from left to right and builds up (identifier and string literal) tokens by appending. So basically, you need a function input_read_char() and string_push_char(). When the string is built up, it gets allocated in the global parser state. The "string" now is a pointer or pointer+length, and you can freely copy it around by value as you like, knowing that the string memory is immutable and allocation is already taken care of.
The string memory management is just a large shared push buffer in the parser that pools all the strings in a central place. If you ever need to release resources, you clear out all strings in one swoop by freeing the push buffer. There is no need to track individual strings.
Allocating string storage using a general-purpose memory allocator, and tracking them individually is wrong. It's a general-purpose approach that lacks the right structure, wastes memory and is super slow. std::string doesn't change any of that. As I said, if you have lots of string allocations then write a script.
You're done, there is no other string indexing or string allocation in a parser.
> That is an interesting statement to make for someone who claims to almost never write C string handling code
What I want to say is that if you're dealing with a lot of strings, you're working in a high-level glue domain. And there is no point in using either C or C++. You're probably making a web API, or an ad-hoc throwaway thing. I do write lots of mini-programs to support development. It need not be python, possibly it should even be a shell or awk script. Most of them they live and die in my command-line history, or as a 5-line script in my /tmp folder. Using C++ for these tasks would slow me down by 10x.