On Wed, 8 May 2024 at 17:04, Marcus Comstedt (ACROSS) (Hail Ilpalazzo!) @ Pike (-) developers forum 10353@lyskom.lysator.liu.se wrote:
The behaviour of match() when there is not exactly one thing matching seems a bit weird in general:
Pike v9.0 release 2 running Hilfe v3.5 (Incremental Pike Frontend)
object buf = Stdio.Buffer("#4,7"); buf->match("#%d,%d");
(1) Result: 11
That IS a bit weird, although not undocumented - if it matches multiple strings, it returns their concatenation. But since it's defined as returning the *sum*, this quirk happens. It's designed for a single match pattern and for that it generally does fine.
Definitely an amusing consequence though!
ChrisA
Yes, although I'm still not sure this follows the principle of least astonishment:
If no matches results in "", and the result is the sum of the matches, wouldn't you expect the result in this case to be ""+4+7, i.e. the string "47"? If an arithmetic sum is expected then you should get 0 from a non-match IMO...
pike-devel@lists.lysator.liu.se