{"action":"created","issue":{"url":"https://api.github.com/repos/stedolan/jq/issues/782","labels_url":"https://api.github.com/repos/stedolan/jq/issues/782/labels{/name}","comments_url":"https://api.github.com/repos/stedolan/jq/issues/782/comments","events_url":"https://api.github.com/repos/stedolan/jq/issues/782/events","html_url":"https://github.com/stedolan/jq/issues/782","id":75074143,"number":782,"title":"fix for gsub bug","user":{"login":"pkoppstein","id":172847,"avatar_url":"https://avatars.githubusercontent.com/u/172847?v=3","gravatar_id":"","url":"https://api.github.com/users/pkoppstein","html_url":"https://github.com/pkoppstein","followers_url":"https://api.github.com/users/pkoppstein/followers","following_url":"https://api.github.com/users/pkoppstein/following{/other_user}","gists_url":"https://api.github.com/users/pkoppstein/gists{/gist_id}","starred_url":"https://api.github.com/users/pkoppstein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pkoppstein/subscriptions","organizations_url":"https://api.github.com/users/pkoppstein/orgs","repos_url":"https://api.github.com/users/pkoppstein/repos","events_url":"https://api.github.com/users/pkoppstein/events{/privacy}","received_events_url":"https://api.github.com/users/pkoppstein/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-05-11T03:54:01Z","updated_at":"2015-05-19T03:13:22Z","closed_at":null,"body":"This update addresses two important issues related to the sub and gsub filters,\r\nwhile also greatly simplifying the implementation of the entire sub/gsub family.\r\n\r\nIn summary: sub/3 is added, and all the other sub and gsub filters are replaced by trivial implementations that use sub/3 directly or indirectly.\r\n\r\nThe main problems with the existing implementations are:\r\n* (1) gsub(re;s) and gsub(re;s;flags) produce incorrect results when \"s\" contains back-references.\r\n* (2) sub/3 is not defined.\r\n\r\nFor example, consider: `\"a1b2\" | gsub(\"(?<d>\\\\d)\"; \":\\(.d);\")`\r\nThis should produce \"a:1;b:2;\" but currently produces \":1;:2;\" instead.\r\n\r\n\r\n\r\n(My apologies for not being able to offer a PR regarding the following.  It's complicated to explain, so I won't try.)\r\n\r\nThe following lines are intended to replace the entire block of lines implementing sub and gsub in builtin.c.  jq has been built and successfully tested with the update.  It would be nice to add a reference to sub/3 in the documentation :-)\r\n\r\n```\r\n  // If s contains capture variables, then create a capture object and pipe it to s\r\n  \"def sub($re; s; flags):\"\r\n  \"  . as $in\"\r\n  \"  | (flags | index(\\\"g\\\")) as $g\"\r\n  \"  | (if $g then flags[0:$g] + flags[$g+1:] else flags end) as $flags\"\r\n  \"  | [match($re; $flags)]\"\r\n  \"  | if length == 0 then $in\"\r\n  \"    else .[0] as $edit\"\r\n  \"    | ($edit | .offset + .length) as $len\"\r\n       //  # create the \"capture\" object:\r\n  \"    | reduce ( $edit | .captures | .[] | select(.name != null) | { (.name) : .string } ) as $pair\"\r\n  \"        ({}; . + $pair)\"\r\n  \"    | $in[0:$edit.offset]\"\r\n  \"      + s\"\r\n  \"      + ($in[$len:] | if $g then sub($re; s; flags) else . end)\"\r\n  \"    end ;\",\r\n  //\r\n  \"def sub($re; s): sub($re; s; \\\"\\\");\",\r\n  // repeated substitution of re (which may contain named captures)\r\n  \"def gsub($re; s; flags): sub($re; s; flags + \\\"g\\\");\",\r\n  \"def gsub($re; s): sub($re; s; \\\"g\\\");\",\r\n```"},"comment":{"url":"https://api.github.com/repos/stedolan/jq/issues/comments/103314262","html_url":"https://github.com/stedolan/jq/issues/782#issuecomment-103314262","issue_url":"https://api.github.com/repos/stedolan/jq/issues/782","id":103314262,"user":{"login":"nicowilliams","id":604851,"avatar_url":"https://avatars.githubusercontent.com/u/604851?v=3","gravatar_id":"","url":"https://api.github.com/users/nicowilliams","html_url":"https://github.com/nicowilliams","followers_url":"https://api.github.com/users/nicowilliams/followers","following_url":"https://api.github.com/users/nicowilliams/following{/other_user}","gists_url":"https://api.github.com/users/nicowilliams/gists{/gist_id}","starred_url":"https://api.github.com/users/nicowilliams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nicowilliams/subscriptions","organizations_url":"https://api.github.com/users/nicowilliams/orgs","repos_url":"https://api.github.com/users/nicowilliams/repos","events_url":"https://api.github.com/users/nicowilliams/events{/privacy}","received_events_url":"https://api.github.com/users/nicowilliams/received_events","type":"User","site_admin":false},"created_at":"2015-05-19T03:13:22Z","updated_at":"2015-05-19T03:13:22Z","body":"Oh, it's a named matching group."}}