{"action":"created","comment":{"url":"https://api.github.com/repos/cesanta/v7/pulls/comments/25886867","id":25886867,"diff_hunk":"@@ -457,56 +461,63 @@ static val_t Str_substr(struct v7 *v7, val_t this_obj, val_t args) {\n static val_t Str_substring(struct v7 *v7, val_t this_obj, val_t args) {\n   long start = arg_long(v7, args, 0, 0);\n   long end = arg_long(v7, args, 1, LONG_MAX);\n-  if (start < 0) start = 0;\n-  if (end < 0) end = 0;\n   return s_substr(v7, this_obj, start, end - start);\n }\n \n-static val_t Str_slice(struct v7 *v7, val_t this_obj, val_t args) {\n-  return Str_substring(v7, this_obj, args);\n-}\n-\n static val_t Str_split(struct v7 *v7, val_t this_obj, val_t args) {\n   val_t res = v7_create_array(v7);\n-  val_t s = to_string(v7, this_obj);\n-  val_t arg0 = i_value_of(v7, v7_array_at(v7, args, 0));\n-  long num_elems = 0, limit = arg_long(v7, args, 1, LONG_MAX);\n-  size_t n1, n2, i, j;\n-  const char *s1 = v7_to_string(v7, &s, &n1);\n-\n-  if (v7_is_string(arg0) || v7_is_regexp(arg0)) {\n-    const char *s2 = v7_to_string(v7, &arg0, &n2);\n-    struct v7_regexp *rp = (struct v7_regexp *) v7_to_pointer(arg0);\n+  const char *s, *s_end;\n+  size_t s_len;\n+  long num_args = v7_array_length(v7, args);\n+  struct slre_prog *prog = NULL;\n+  this_obj = to_string(v7, this_obj);\n+  s = v7_to_string(v7, &this_obj, &s_len);\n+  s_end = s + s_len;\n+\n+  if (num_args == 0 || s_len == 0) {\n+    v7_array_append(v7, res, v7_create_string(v7, s, s_len, 1));\n+  } else {\n+    val_t ro = i_value_of(v7, v7_array_at(v7, args, 0));\n+    long len, elem = 0, limit = arg_long(v7, args, 1, LONG_MAX);\n+    size_t shift = 0;\n     struct slre_loot loot;\n+    if (!v7_is_regexp(ro)) {\n+      const char *str;\n+      size_t str_len;\n+      ro = to_string(v7, ro);\n+      str = v7_to_string(v7, &ro, &str_len);\n+      if (slre_compile(str, str_len, NULL, 0, &prog, 0) != SLRE_OK ||\n+          prog == NULL) {\n+        throw_exception(v7, \"Error\", \"Invalid String\");\n+        return V7_UNDEFINED;\n+      }\n+    } else\n+      prog = ((struct v7_regexp *)v7_to_pointer(ro))->compiled_regexp;\n \n-    for (i = j = 0; n2 <= n1 && i <= (n1 - n2); i++) {\n-      if (num_elems >= limit) break;\n-      if (v7_is_string(arg0) && (i > 0 || n2 > 0) &&\n-          memcmp(s1 + i, s2, n2) == 0) {\n-        v7_array_append(v7, res, v7_create_string(v7, s1 + j, i - j, 1));\n-        s1 = v7_to_string(v7, &s, &n1);\n-        s2 = v7_to_string(v7, &arg0, &n2);\n-        num_elems++;\n-        i = j = i + n2;\n-      } else if (v7_is_regexp(arg0)) {\n-        if (slre_exec(rp->compiled_regexp, s1 + i, n1 - i, &loot) == SLRE_OK) {\n-          /* TODO(lsm): fix this */\n-          struct slre_cap *cap = &loot.caps[0];\n-          i = cap->start - s1;\n-          v7_array_append(v7, res, v7_create_string(v7, s1 + j, i - j, 1));\n-          s1 = v7_to_string(v7, &s, &n1);\n-          s2 = v7_to_string(v7, &arg0, &n2);\n-          num_elems++;\n-          i = j = cap->end - s1;\n-        } else {\n-          i = n1 - n2;  /* No match, stop the loop */\n-          break;\n-        }\n+    for (; elem < limit && shift < s_len; elem++) {\n+      val_t tmp_s;\n+      int i;\n+      if (slre_exec(prog, 0, s + shift, s_end, &loot)) break;\n+      if (loot.caps[0].end - loot.caps[0].start == 0) {\n+        tmp_s = v7_create_string(v7, s + shift, 1, 1);\n+        shift++;\n+      } else {\n+        tmp_s =\n+            v7_create_string(v7, s + shift, loot.caps[0].start - s - shift, 1);\n+        shift = loot.caps[0].end - s;\n       }\n+      v7_array_append(v7, res, tmp_s);\n+\n+      for (i = 1; i < loot.num_captures; i++)\n+        v7_array_append(\n+            v7, res,\n+            (loot.caps[i].start != NULL)\n+                ? v7_create_string(v7, loot.caps[i].start,\n+                                   loot.caps[i].end - loot.caps[i].start, 1)\n+                : V7_UNDEFINED);","path":"src/string.c","position":690,"original_position":690,"commit_id":"15b6510b0e7095e3d21da36fcc31180c3fd72e1f","original_commit_id":"c411eee2f32e5b95147ab6c1be2b9613540e1eff","user":{"login":"mmikulicic","id":52673,"avatar_url":"https://avatars.githubusercontent.com/u/52673?v=3","gravatar_id":"","url":"https://api.github.com/users/mmikulicic","html_url":"https://github.com/mmikulicic","followers_url":"https://api.github.com/users/mmikulicic/followers","following_url":"https://api.github.com/users/mmikulicic/following{/other_user}","gists_url":"https://api.github.com/users/mmikulicic/gists{/gist_id}","starred_url":"https://api.github.com/users/mmikulicic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmikulicic/subscriptions","organizations_url":"https://api.github.com/users/mmikulicic/orgs","repos_url":"https://api.github.com/users/mmikulicic/repos","events_url":"https://api.github.com/users/mmikulicic/events{/privacy}","received_events_url":"https://api.github.com/users/mmikulicic/received_events","type":"User","site_admin":false},"body":"`v7_create_undefined`","created_at":"2015-03-05T18:14:19Z","updated_at":"2015-03-05T18:14:19Z","html_url":"https://github.com/cesanta/v7/pull/338#discussion_r25886867","pull_request_url":"https://api.github.com/repos/cesanta/v7/pulls/338","_links":{"self":{"href":"https://api.github.com/repos/cesanta/v7/pulls/comments/25886867"},"html":{"href":"https://github.com/cesanta/v7/pull/338#discussion_r25886867"},"pull_request":{"href":"https://api.github.com/repos/cesanta/v7/pulls/338"}}},"pull_request":{"url":"https://api.github.com/repos/cesanta/v7/pulls/338","id":30538048,"html_url":"https://github.com/cesanta/v7/pull/338","diff_url":"https://github.com/cesanta/v7/pull/338.diff","patch_url":"https://github.com/cesanta/v7/pull/338.patch","issue_url":"https://api.github.com/repos/cesanta/v7/issues/338","number":338,"state":"open","locked":false,"title":"re-new String & RegExp","user":{"login":"vrz","id":8409104,"avatar_url":"https://avatars.githubusercontent.com/u/8409104?v=3","gravatar_id":"","url":"https://api.github.com/users/vrz","html_url":"https://github.com/vrz","followers_url":"https://api.github.com/users/vrz/followers","following_url":"https://api.github.com/users/vrz/following{/other_user}","gists_url":"https://api.github.com/users/vrz/gists{/gist_id}","starred_url":"https://api.github.com/users/vrz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vrz/subscriptions","organizations_url":"https://api.github.com/users/vrz/orgs","repos_url":"https://api.github.com/users/vrz/repos","events_url":"https://api.github.com/users/vrz/events{/privacy}","received_events_url":"https://api.github.com/users/vrz/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-03-05T08:25:28Z","updated_at":"2015-03-05T18:14:19Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d53b706e039662a264b41c7befb45fd3cedc3684","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cesanta/v7/pulls/338/commits","review_comments_url":"https://api.github.com/repos/cesanta/v7/pulls/338/comments","review_comment_url":"https://api.github.com/repos/cesanta/v7/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cesanta/v7/issues/338/comments","statuses_url":"https://api.github.com/repos/cesanta/v7/statuses/15b6510b0e7095e3d21da36fcc31180c3fd72e1f","head":{"label":"vrz:string_regexp","ref":"string_regexp","sha":"15b6510b0e7095e3d21da36fcc31180c3fd72e1f","user":{"login":"vrz","id":8409104,"avatar_url":"https://avatars.githubusercontent.com/u/8409104?v=3","gravatar_id":"","url":"https://api.github.com/users/vrz","html_url":"https://github.com/vrz","followers_url":"https://api.github.com/users/vrz/followers","following_url":"https://api.github.com/users/vrz/following{/other_user}","gists_url":"https://api.github.com/users/vrz/gists{/gist_id}","starred_url":"https://api.github.com/users/vrz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vrz/subscriptions","organizations_url":"https://api.github.com/users/vrz/orgs","repos_url":"https://api.github.com/users/vrz/repos","events_url":"https://api.github.com/users/vrz/events{/privacy}","received_events_url":"https://api.github.com/users/vrz/received_events","type":"User","site_admin":false},"repo":{"id":22812139,"name":"v7","full_name":"vrz/v7","owner":{"login":"vrz","id":8409104,"avatar_url":"https://avatars.githubusercontent.com/u/8409104?v=3","gravatar_id":"","url":"https://api.github.com/users/vrz","html_url":"https://github.com/vrz","followers_url":"https://api.github.com/users/vrz/followers","following_url":"https://api.github.com/users/vrz/following{/other_user}","gists_url":"https://api.github.com/users/vrz/gists{/gist_id}","starred_url":"https://api.github.com/users/vrz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vrz/subscriptions","organizations_url":"https://api.github.com/users/vrz/orgs","repos_url":"https://api.github.com/users/vrz/repos","events_url":"https://api.github.com/users/vrz/events{/privacy}","received_events_url":"https://api.github.com/users/vrz/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/vrz/v7","description":"Embedded JavaScript engine for C/C++ apps","fork":true,"url":"https://api.github.com/repos/vrz/v7","forks_url":"https://api.github.com/repos/vrz/v7/forks","keys_url":"https://api.github.com/repos/vrz/v7/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vrz/v7/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vrz/v7/teams","hooks_url":"https://api.github.com/repos/vrz/v7/hooks","issue_events_url":"https://api.github.com/repos/vrz/v7/issues/events{/number}","events_url":"https://api.github.com/repos/vrz/v7/events","assignees_url":"https://api.github.com/repos/vrz/v7/assignees{/user}","branches_url":"https://api.github.com/repos/vrz/v7/branches{/branch}","tags_url":"https://api.github.com/repos/vrz/v7/tags","blobs_url":"https://api.github.com/repos/vrz/v7/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vrz/v7/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vrz/v7/git/refs{/sha}","trees_url":"https://api.github.com/repos/vrz/v7/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vrz/v7/statuses/{sha}","languages_url":"https://api.github.com/repos/vrz/v7/languages","stargazers_url":"https://api.github.com/repos/vrz/v7/stargazers","contributors_url":"https://api.github.com/repos/vrz/v7/contributors","subscribers_url":"https://api.github.com/repos/vrz/v7/subscribers","subscription_url":"https://api.github.com/repos/vrz/v7/subscription","commits_url":"https://api.github.com/repos/vrz/v7/commits{/sha}","git_commits_url":"https://api.github.com/repos/vrz/v7/git/commits{/sha}","comments_url":"https://api.github.com/repos/vrz/v7/comments{/number}","issue_comment_url":"https://api.github.com/repos/vrz/v7/issues/comments{/number}","contents_url":"https://api.github.com/repos/vrz/v7/contents/{+path}","compare_url":"https://api.github.com/repos/vrz/v7/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vrz/v7/merges","archive_url":"https://api.github.com/repos/vrz/v7/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vrz/v7/downloads","issues_url":"https://api.github.com/repos/vrz/v7/issues{/number}","pulls_url":"https://api.github.com/repos/vrz/v7/pulls{/number}","milestones_url":"https://api.github.com/repos/vrz/v7/milestones{/number}","notifications_url":"https://api.github.com/repos/vrz/v7/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vrz/v7/labels{/name}","releases_url":"https://api.github.com/repos/vrz/v7/releases{/id}","created_at":"2014-08-10T15:39:05Z","updated_at":"2015-03-05T17:47:07Z","pushed_at":"2015-03-05T17:47:07Z","git_url":"git://github.com/vrz/v7.git","ssh_url":"git@github.com:vrz/v7.git","clone_url":"https://github.com/vrz/v7.git","svn_url":"https://github.com/vrz/v7","homepage":"","size":8740,"stargazers_count":0,"watchers_count":0,"language":"C","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cesanta:master","ref":"master","sha":"f6427be5a90c7879d2815c4a47007665c86824fc","user":{"login":"cesanta","id":5111322,"avatar_url":"https://avatars.githubusercontent.com/u/5111322?v=3","gravatar_id":"","url":"https://api.github.com/users/cesanta","html_url":"https://github.com/cesanta","followers_url":"https://api.github.com/users/cesanta/followers","following_url":"https://api.github.com/users/cesanta/following{/other_user}","gists_url":"https://api.github.com/users/cesanta/gists{/gist_id}","starred_url":"https://api.github.com/users/cesanta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cesanta/subscriptions","organizations_url":"https://api.github.com/users/cesanta/orgs","repos_url":"https://api.github.com/users/cesanta/repos","events_url":"https://api.github.com/users/cesanta/events{/privacy}","received_events_url":"https://api.github.com/users/cesanta/received_events","type":"Organization","site_admin":false},"repo":{"id":15176507,"name":"v7","full_name":"cesanta/v7","owner":{"login":"cesanta","id":5111322,"avatar_url":"https://avatars.githubusercontent.com/u/5111322?v=3","gravatar_id":"","url":"https://api.github.com/users/cesanta","html_url":"https://github.com/cesanta","followers_url":"https://api.github.com/users/cesanta/followers","following_url":"https://api.github.com/users/cesanta/following{/other_user}","gists_url":"https://api.github.com/users/cesanta/gists{/gist_id}","starred_url":"https://api.github.com/users/cesanta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cesanta/subscriptions","organizations_url":"https://api.github.com/users/cesanta/orgs","repos_url":"https://api.github.com/users/cesanta/repos","events_url":"https://api.github.com/users/cesanta/events{/privacy}","received_events_url":"https://api.github.com/users/cesanta/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cesanta/v7","description":"Embedded JavaScript engine for C/C++","fork":false,"url":"https://api.github.com/repos/cesanta/v7","forks_url":"https://api.github.com/repos/cesanta/v7/forks","keys_url":"https://api.github.com/repos/cesanta/v7/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cesanta/v7/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cesanta/v7/teams","hooks_url":"https://api.github.com/repos/cesanta/v7/hooks","issue_events_url":"https://api.github.com/repos/cesanta/v7/issues/events{/number}","events_url":"https://api.github.com/repos/cesanta/v7/events","assignees_url":"https://api.github.com/repos/cesanta/v7/assignees{/user}","branches_url":"https://api.github.com/repos/cesanta/v7/branches{/branch}","tags_url":"https://api.github.com/repos/cesanta/v7/tags","blobs_url":"https://api.github.com/repos/cesanta/v7/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cesanta/v7/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cesanta/v7/git/refs{/sha}","trees_url":"https://api.github.com/repos/cesanta/v7/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cesanta/v7/statuses/{sha}","languages_url":"https://api.github.com/repos/cesanta/v7/languages","stargazers_url":"https://api.github.com/repos/cesanta/v7/stargazers","contributors_url":"https://api.github.com/repos/cesanta/v7/contributors","subscribers_url":"https://api.github.com/repos/cesanta/v7/subscribers","subscription_url":"https://api.github.com/repos/cesanta/v7/subscription","commits_url":"https://api.github.com/repos/cesanta/v7/commits{/sha}","git_commits_url":"https://api.github.com/repos/cesanta/v7/git/commits{/sha}","comments_url":"https://api.github.com/repos/cesanta/v7/comments{/number}","issue_comment_url":"https://api.github.com/repos/cesanta/v7/issues/comments{/number}","contents_url":"https://api.github.com/repos/cesanta/v7/contents/{+path}","compare_url":"https://api.github.com/repos/cesanta/v7/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cesanta/v7/merges","archive_url":"https://api.github.com/repos/cesanta/v7/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cesanta/v7/downloads","issues_url":"https://api.github.com/repos/cesanta/v7/issues{/number}","pulls_url":"https://api.github.com/repos/cesanta/v7/pulls{/number}","milestones_url":"https://api.github.com/repos/cesanta/v7/milestones{/number}","notifications_url":"https://api.github.com/repos/cesanta/v7/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cesanta/v7/labels{/name}","releases_url":"https://api.github.com/repos/cesanta/v7/releases{/id}","created_at":"2013-12-13T23:28:04Z","updated_at":"2015-03-05T13:32:23Z","pushed_at":"2015-03-05T18:00:19Z","git_url":"git://github.com/cesanta/v7.git","ssh_url":"git@github.com:cesanta/v7.git","clone_url":"https://github.com/cesanta/v7.git","svn_url":"https://github.com/cesanta/v7","homepage":"","size":17188,"stargazers_count":61,"watchers_count":61,"language":"C","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":29,"forks":16,"open_issues":29,"watchers":61,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cesanta/v7/pulls/338"},"html":{"href":"https://github.com/cesanta/v7/pull/338"},"issue":{"href":"https://api.github.com/repos/cesanta/v7/issues/338"},"comments":{"href":"https://api.github.com/repos/cesanta/v7/issues/338/comments"},"review_comments":{"href":"https://api.github.com/repos/cesanta/v7/pulls/338/comments"},"review_comment":{"href":"https://api.github.com/repos/cesanta/v7/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cesanta/v7/pulls/338/commits"},"statuses":{"href":"https://api.github.com/repos/cesanta/v7/statuses/15b6510b0e7095e3d21da36fcc31180c3fd72e1f"}}}}