sera / fhwang.net (Francis Hwang) writes:

> Hi all,
> 
> I discovered a strange bug in Lafcadio which I'm trying to work out,
> but the regexp is getting really nasty. Basically, to commit text
> values to the database I need to double any apostrophe, except those
> that are preceded by a backslash. This has to account for apostrophes
> at the beginning of the string, and multiline strings where the
> apostrophe is at the start of a new line. Examples would be:
> 
> "I can't drive 55" -> "I can''t drive 55"
> "Don\'t escape here" -> "Don\'t escape here"
> "'" -> "''"
> "line 1\n' line 2" -> "line 1\n'' line 2"
> 
> So here's the regexp that does it:
> 
> value = value.gsub( /(^|[^\\\n])'/ ) { $& + "'" }
> 
> which works fine, except I just discovered that it fails in one odd
> case:
> 
> "'''" -> "'''''"
> 
> (That is, a single line of three apostrophes should become six
> apostrophes, but instead it becomes five.)
> 
> Any idea why this is doing it? I suspect I'm being too clever by
> including the beginning of line in a grouping, and maybe that affects
> how the regexp is processing the string?
> 
> F.

Well you can be crude and say

  value.gsub(/(\\?')/) { |m| m.length == 1 ? "''" : m }

or some moral equivalent.

But I'm sure there's a nicer way.

Mike

-- 
mike / stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike / exegenix.com                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA