On Tue, 19 May 2015, Sven Schreiber wrote:
Am 19.05.2015 um 18:08 schrieb Allin Cottrell:
> On Tue, 19 May 2015, Sven Schreiber wrote:
>
>>
>> At first sight from what I quickly tried to do with escaped double
>> quotes (\") in the regsub() function I would think you're right and
>> there's a bug there.
>
> There may be something going on with GLib versions here. On Linux with
> GLib 2.44.0 the following works fine:
>
> <hansl>
> string s1 = sprintf("string with \"quoted\" bit")
> print s1
> string s2 = regsub(s1, "\"", "*")
> print s2
> </hansl>
>
Yes, but if I do it the other way around:
<hansl>
string s1 = sprintf("string with *quoted* bit")
print s1
string s2 = regsub(s1, "\*", "\"")
print s2
</hansl>
then I get an error saying \" is an unknown escape sequence. Or am I missing
something?
This is subtle. To get the last regsub above (where the replacement
text is a double-quote) to work you have to pass a string that
contains nothing but a double-quote. This is the same as for use of
sed via the *nix shell, except in that context you type a backslash
before the double-quote so that the shell will pass a literal
double-quote to sed (sed itself won't see the backslash).
In hansl, the only way to create a string that contains nothing but a
double-quote is to use printf(). Hence the fact that Jack's approach
works:
string dq = sprintf("\"")
string s2 = regsub(s1, "\*", dq)
In other words, there's no error in GLib's regexp code, and there
isn't really an error in gretl either -- it's just that at some point
in the past we decided not to mess with escapes in the straight
definition of a string literal, but only via printf.
Allin