Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.arch.embedded Subject: Re: Static regex for embedded systems Date: Tue, 21 Jan 2025 16:40:17 +0100 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 21 Jan 2025 16:40:17 +0100 (CET) Injection-Info: dont-email.me; posting-host="e91af23b8d9064545c872479c11f00b9"; logging-data="180543"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19nYmH4cgiBpqCLhzqoSNvTdaEaiRiJSpA=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:etHpzREF02OGKFZI69ND7eocU/g= In-Reply-To: Content-Language: en-GB Bytes: 2443 On 21/01/2025 15:31, pozz wrote: > Many times I need to parse/decode a text string that comes from an > external system, over a serial bus, MQTT, and so on. > > Many times this string has a fixed syntax/layout. In order to parse this > string, I everytime create a custom parser that can be tedious, > cumbersom and error prone. > > For example, suppose you have to decode a string from a modem that uses > AT commands. Many answers from the modem has the following schema: > >   \r\n+: ,\r\n >   \r\nOK\r\n > > The prefix is known, the number and type of fileds are known too. With > regex, the parser would be simple. > There are plenty of libraries for run-time regular expressions - they have been in the C++ standard library since C++11 (IIRC). But run-time parsing of regex strings and matching for the input string will be very time and space costly in a small embedded system. You want compile-time regular expression handling. This had been planned for inclusion in C++23 (and therefore part of the current GNU ARM Embedded toolchain, if that's what you are using), but it didn't make it. However, there are standalone compile-time regex libraries for C++ using templates and compile-time functions, which will in effect generate specialised parsers from the regex strings.