codecop 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. codecop/__init__.py +7 -0
  2. codecop/access.py +140 -0
  3. codecop/access_postgres.py +284 -0
  4. codecop/advisor.py +1512 -0
  5. codecop/advisor_bench.py +493 -0
  6. codecop/advisor_ch.py +2080 -0
  7. codecop/advisor_view.py +381 -0
  8. codecop/advisors.py +184 -0
  9. codecop/agent.py +221 -0
  10. codecop/agentfacts.py +745 -0
  11. codecop/apply/__init__.py +327 -0
  12. codecop/apply/check.py +344 -0
  13. codecop/apply/cli.py +162 -0
  14. codecop/apply/clickhouse.py +379 -0
  15. codecop/apply/common.py +238 -0
  16. codecop/apply/html.py +264 -0
  17. codecop/apply/patch.py +213 -0
  18. codecop/apply/postgres.py +102 -0
  19. codecop/apply/trial.py +166 -0
  20. codecop/assemble.py +104 -0
  21. codecop/baseline.py +1043 -0
  22. codecop/bench.py +2464 -0
  23. codecop/blockers.py +935 -0
  24. codecop/budget.py +980 -0
  25. codecop/catalog_profile.py +525 -0
  26. codecop/changed.py +261 -0
  27. codecop/cicmd.py +1002 -0
  28. codecop/cli.py +5421 -0
  29. codecop/client.py +729 -0
  30. codecop/coverage.py +1067 -0
  31. codecop/coverage_reasons.py +116 -0
  32. codecop/crashlog.py +70 -0
  33. codecop/datagen.py +766 -0
  34. codecop/datagen_postgres.py +630 -0
  35. codecop/datagen_sqlite.py +323 -0
  36. codecop/dbcli.py +885 -0
  37. codecop/definitions.py +457 -0
  38. codecop/discover/__init__.py +702 -0
  39. codecop/discover/arms.py +160 -0
  40. codecop/discover/base.py +234 -0
  41. codecop/discover/dialect.py +296 -0
  42. codecop/discover/elixir.py +64 -0
  43. codecop/discover/engine_go.py +320 -0
  44. codecop/discover/go.py +1947 -0
  45. codecop/discover/java.py +1528 -0
  46. codecop/discover/loops.py +1671 -0
  47. codecop/discover/named_go.py +258 -0
  48. codecop/discover/origins.py +4565 -0
  49. codecop/discover/origins_ts.py +1584 -0
  50. codecop/discover/orm_django.py +2772 -0
  51. codecop/discover/orm_django_models.py +814 -0
  52. codecop/discover/orm_django_sql.py +662 -0
  53. codecop/discover/orm_ent.py +565 -0
  54. codecop/discover/orm_ex_ecto.py +2138 -0
  55. codecop/discover/orm_go.py +2134 -0
  56. codecop/discover/orm_go_bun.py +602 -0
  57. codecop/discover/orm_go_run.py +636 -0
  58. codecop/discover/orm_go_types.py +1082 -0
  59. codecop/discover/orm_java.py +1450 -0
  60. codecop/discover/orm_jdbi.py +743 -0
  61. codecop/discover/orm_jooq.py +806 -0
  62. codecop/discover/orm_jpa.py +1307 -0
  63. codecop/discover/orm_mybatis.py +730 -0
  64. codecop/discover/orm_py.py +557 -0
  65. codecop/discover/orm_py_peewee.py +1266 -0
  66. codecop/discover/orm_py_prisma.py +298 -0
  67. codecop/discover/orm_rb_activerecord.py +1906 -0
  68. codecop/discover/orm_rust.py +1248 -0
  69. codecop/discover/orm_rust_seaorm.py +734 -0
  70. codecop/discover/orm_sqlalchemy.py +2475 -0
  71. codecop/discover/orm_stats.py +307 -0
  72. codecop/discover/orm_tables.py +29 -0
  73. codecop/discover/orm_ts.py +503 -0
  74. codecop/discover/orm_ts_drizzle.py +697 -0
  75. codecop/discover/orm_ts_knex.py +1117 -0
  76. codecop/discover/orm_ts_kysely.py +861 -0
  77. codecop/discover/orm_ts_prisma.py +1045 -0
  78. codecop/discover/orm_ts_schema.py +169 -0
  79. codecop/discover/orm_ts_sequelize.py +1401 -0
  80. codecop/discover/orm_ts_typeorm.py +1067 -0
  81. codecop/discover/probes.py +323 -0
  82. codecop/discover/python.py +595 -0
  83. codecop/discover/ruby.py +793 -0
  84. codecop/discover/rust.py +2378 -0
  85. codecop/discover/schema.py +520 -0
  86. codecop/discover/schema_ent.py +145 -0
  87. codecop/discover/schema_ts.py +420 -0
  88. codecop/discover/tsresolve.py +257 -0
  89. codecop/discover/typescript.py +1728 -0
  90. codecop/discover/xml_mapper.py +526 -0
  91. codecop/engines/__init__.py +515 -0
  92. codecop/engines/base.py +498 -0
  93. codecop/engines/clickhouse.py +1558 -0
  94. codecop/engines/pg_ddl.py +444 -0
  95. codecop/engines/pg_hints.py +137 -0
  96. codecop/engines/postgres.py +1060 -0
  97. codecop/engines/sqlite.py +742 -0
  98. codecop/families/__init__.py +3 -0
  99. codecop/families/build.py +280 -0
  100. codecop/families/cli.py +177 -0
  101. codecop/families/coverage_hook.py +62 -0
  102. codecop/families/driver.py +221 -0
  103. codecop/families/goslice.py +229 -0
  104. codecop/families/goslice_tool/go.mod +3 -0
  105. codecop/families/goslice_tool/main.go +403 -0
  106. codecop/families/holes.py +451 -0
  107. codecop/families/images/Dockerfile.go +5 -0
  108. codecop/families/images/Dockerfile.python +4 -0
  109. codecop/families/images/Dockerfile.rust +4 -0
  110. codecop/families/isolate.py +171 -0
  111. codecop/families/locate.py +270 -0
  112. codecop/families/model.py +301 -0
  113. codecop/families/plangate.py +271 -0
  114. codecop/families/tagpatch.py +174 -0
  115. codecop/fidelity.py +671 -0
  116. codecop/fixgroups.py +130 -0
  117. codecop/formerly.py +91 -0
  118. codecop/grantscript.py +254 -0
  119. codecop/groupbench.py +1375 -0
  120. codecop/groups.py +594 -0
  121. codecop/groups_view.py +186 -0
  122. codecop/heat.py +1405 -0
  123. codecop/heatcli.py +219 -0
  124. codecop/joins.py +1749 -0
  125. codecop/joins_view.py +127 -0
  126. codecop/kinds/__init__.py +507 -0
  127. codecop/kinds/calls/__init__.py +501 -0
  128. codecop/kinds/calls/extract.py +1097 -0
  129. codecop/kinds/calls/harness.py +408 -0
  130. codecop/kinds/calls/measure.py +342 -0
  131. codecop/kinds/calls/rules.py +139 -0
  132. codecop/kinds/calls/runners/RunCalls.java +167 -0
  133. codecop/kinds/calls/runners/run_go.go +141 -0
  134. codecop/kinds/calls/runners/run_node.js +186 -0
  135. codecop/kinds/calls/runners/run_python.py +319 -0
  136. codecop/kinds/calls/runners/run_rust.rs +200 -0
  137. codecop/kinds/calls/standin.py +340 -0
  138. codecop/kinds/pipeline.py +288 -0
  139. codecop/kinds/regex/__init__.py +1167 -0
  140. codecop/kinds/regex/engines/RegexHarness.java +129 -0
  141. codecop/kinds/regex/engines/run_go.go +294 -0
  142. codecop/kinds/regex/engines/run_node.js +99 -0
  143. codecop/kinds/regex/engines/run_python.py +295 -0
  144. codecop/kinds/regex/engines/run_rust.rs +149 -0
  145. codecop/kinds/regex/extract.py +1438 -0
  146. codecop/kinds/regex/harness.py +235 -0
  147. codecop/kinds/regex/measure.py +270 -0
  148. codecop/kinds/regex/shapes.py +418 -0
  149. codecop/kinds/regex/syntax.py +1721 -0
  150. codecop/kinds/testkind.py +107 -0
  151. codecop/kinds/transaction/__init__.py +228 -0
  152. codecop/kinds/transaction/bind.py +355 -0
  153. codecop/kinds/transaction/measure.py +264 -0
  154. codecop/kinds/transaction/plan.py +462 -0
  155. codecop/kinds/transaction/rules.py +540 -0
  156. codecop/kinds/transaction/sandbox.py +94 -0
  157. codecop/kinds/transaction/scenarios.py +520 -0
  158. codecop/kinds/transaction/schedule.py +483 -0
  159. codecop/kinds/transaction/shapes.py +418 -0
  160. codecop/kinds/transaction/verify.py +293 -0
  161. codecop/live.py +237 -0
  162. codecop/loopbench.py +2071 -0
  163. codecop/loopschema.py +591 -0
  164. codecop/mcpserver.py +120 -0
  165. codecop/mechanism.py +214 -0
  166. codecop/members.py +200 -0
  167. codecop/multi.py +443 -0
  168. codecop/observed.py +1868 -0
  169. codecop/parts.py +102 -0
  170. codecop/patterns.py +830 -0
  171. codecop/patterns_view.py +559 -0
  172. codecop/plangate.py +242 -0
  173. codecop/profile.py +439 -0
  174. codecop/prompts/areas.md +19 -0
  175. codecop/prompts/observed_variants.md +20 -0
  176. codecop/prompts/profile.md +60 -0
  177. codecop/prompts/profile_postgres.md +68 -0
  178. codecop/prompts/profile_sizes.md +26 -0
  179. codecop/prompts/profile_sqlite.md +69 -0
  180. codecop/prompts/rank.md +84 -0
  181. codecop/prompts/rank_postgres.md +96 -0
  182. codecop/prompts/rank_sqlite.md +94 -0
  183. codecop/prompts/scenario_variants.md +20 -0
  184. codecop/rank.py +1473 -0
  185. codecop/realdb/__init__.py +688 -0
  186. codecop/realdb/clickhouse.py +1223 -0
  187. codecop/realdb/facts.py +815 -0
  188. codecop/realdb/locality.py +964 -0
  189. codecop/realdb/locks.py +355 -0
  190. codecop/realdb/match.py +531 -0
  191. codecop/realdb/postgres.py +643 -0
  192. codecop/realdb/ratios.py +77 -0
  193. codecop/realdb/sample.py +126 -0
  194. codecop/relations.py +227 -0
  195. codecop/report.py +1854 -0
  196. codecop/resources.py +332 -0
  197. codecop/resources_view.py +266 -0
  198. codecop/rules/entry_points.toml +1013 -0
  199. codecop/rules/sinks.toml +362 -0
  200. codecop/runsonce.py +8 -0
  201. codecop/sandboxschema.py +555 -0
  202. codecop/scenario_agent.py +172 -0
  203. codecop/scenario_bind.py +507 -0
  204. codecop/scenario_sql.py +971 -0
  205. codecop/scenarios.py +675 -0
  206. codecop/scenarios_view.py +274 -0
  207. codecop/schemacli.py +67 -0
  208. codecop/schemas/areas_output.schema.json +21 -0
  209. codecop/schemas/profile_output.schema.json +362 -0
  210. codecop/schemas/rank_output.schema.json +385 -0
  211. codecop/sizes.py +200 -0
  212. codecop/sqlinfo.py +558 -0
  213. codecop/typedparams.py +576 -0
  214. codecop/verdicts.py +212 -0
  215. codecop/writebench.py +720 -0
  216. codecop/writecost.py +440 -0
  217. codecop/writes.py +1208 -0
  218. codecop-0.1.0.dist-info/METADATA +42 -0
  219. codecop-0.1.0.dist-info/RECORD +222 -0
  220. codecop-0.1.0.dist-info/WHEEL +4 -0
  221. codecop-0.1.0.dist-info/entry_points.txt +2 -0
  222. codecop-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,129 @@
1
+ // CodeCop's regex harness for java.util.regex. One JSON request on stdin, JSON lines on stdout (see run_python.py).
2
+ // Runs only the pattern and strings it is given; it loads nothing from the repository. No dependency: a small JSON
3
+ // reader is below.
4
+ import java.io.*;
5
+ import java.nio.charset.StandardCharsets;
6
+ import java.util.*;
7
+ import java.util.regex.*;
8
+
9
+ public class RegexHarness {
10
+ static PrintStream out;
11
+
12
+ static void emit(String s) { out.println(s); out.flush(); }
13
+
14
+ static String q(String s) {
15
+ StringBuilder b = new StringBuilder("\"");
16
+ for (char c : s.toCharArray()) {
17
+ if (c == '"' || c == '\\') b.append('\\').append(c);
18
+ else if (c < 0x20) b.append(String.format("\\u%04x", (int) c));
19
+ else b.append(c);
20
+ }
21
+ return b.append('"').toString();
22
+ }
23
+
24
+ // --- a minimal JSON reader: objects, arrays, strings, numbers, true, false, null ---
25
+ static int p; static String t;
26
+ static void ws() { while (p < t.length() && Character.isWhitespace(t.charAt(p))) p++; }
27
+ static Object val() {
28
+ ws();
29
+ char c = t.charAt(p);
30
+ if (c == '{') { p++; Map<String, Object> m = new HashMap<>(); ws(); if (t.charAt(p) == '}') { p++; return m; }
31
+ while (true) { ws(); String k = (String) val(); ws(); p++; m.put(k, val()); ws(); if (t.charAt(p++) == '}') return m; } }
32
+ if (c == '[') { p++; List<Object> l = new ArrayList<>(); ws(); if (t.charAt(p) == ']') { p++; return l; }
33
+ while (true) { l.add(val()); ws(); if (t.charAt(p++) == ']') return l; } }
34
+ if (c == '"') { p++; StringBuilder b = new StringBuilder();
35
+ while (true) { char d = t.charAt(p++); if (d == '"') return b.toString();
36
+ if (d == '\\') { char e = t.charAt(p++);
37
+ switch (e) { case 'n': b.append('\n'); break; case 't': b.append('\t'); break; case 'r': b.append('\r'); break;
38
+ case 'b': b.append('\b'); break; case 'f': b.append('\f'); break;
39
+ case 'u': b.append((char) Integer.parseInt(t.substring(p, p + 4), 16)); p += 4; break; default: b.append(e); }
40
+ } else b.append(d); } }
41
+ if (t.startsWith("true", p)) { p += 4; return Boolean.TRUE; }
42
+ if (t.startsWith("false", p)) { p += 5; return Boolean.FALSE; }
43
+ if (t.startsWith("null", p)) { p += 4; return null; }
44
+ int s = p; while (p < t.length() && "+-0123456789.eE".indexOf(t.charAt(p)) >= 0) p++;
45
+ return Double.parseDouble(t.substring(s, p));
46
+ }
47
+
48
+ static int flagsOf(String f) {
49
+ int x = 0;
50
+ for (char c : (f == null ? "" : f).toCharArray()) {
51
+ if (c == 'i') x |= Pattern.CASE_INSENSITIVE; if (c == 'm') x |= Pattern.MULTILINE; if (c == 's') x |= Pattern.DOTALL;
52
+ if (c == 'x') x |= Pattern.COMMENTS; if (c == 'u') x |= Pattern.UNICODE_CASE | Pattern.UNICODE_CHARACTER_CLASS;
53
+ }
54
+ return x;
55
+ }
56
+
57
+ static String spans(Matcher m) {
58
+ StringBuilder b = new StringBuilder("[" + m.start() + "," + m.end());
59
+ for (int g = 1; g <= Math.min(m.groupCount(), 20); g++) b.append(m.start(g) < 0 ? ",null" : ",[" + m.start(g) + "," + m.end(g) + "]");
60
+ return b.append("]").toString();
61
+ }
62
+
63
+ static String run(Pattern rx, String mode, String s, int take, int[] n) {
64
+ Matcher m = rx.matcher(s);
65
+ switch (mode) {
66
+ case "bool": { boolean r = m.find(); n[0] = r ? 1 : 0; return r ? "true" : "false"; }
67
+ case "full": { boolean r = m.matches(); n[0] = r ? 1 : 0; return r ? spans(m) : "null"; }
68
+ case "match": { boolean r = m.lookingAt(); n[0] = r ? 1 : 0; return r ? spans(m) : "null"; }
69
+ case "search": { boolean r = m.find(); n[0] = r ? 1 : 0; return r ? spans(m) : "null"; }
70
+ case "count": { int k = 0; while (m.find()) { k++; if (take > 0 && k >= take) break; } n[0] = k; return Integer.toString(k); }
71
+ default: { StringBuilder b = new StringBuilder("["); int k = 0; while (m.find()) { if (k > 0) b.append(","); b.append(spans(m)); if (++k >= 50) break; } n[0] = k; return b.append("]").toString(); }
72
+ }
73
+ }
74
+
75
+ @SuppressWarnings("unchecked")
76
+ public static void main(String[] a) throws Exception {
77
+ out = new PrintStream(new FileOutputStream(FileDescriptor.out), true, "UTF-8");
78
+ t = new String(System.in.readAllBytes(), StandardCharsets.UTF_8); p = 0;
79
+ Map<String, Object> req = (Map<String, Object>) val();
80
+ String op = (String) req.get("op");
81
+ if ("version".equals(op)) { emit("{\"engine\":\"java.util.regex\",\"version\":" + q(System.getProperty("java.version")) + "}"); return; }
82
+ String mode = req.get("mode") == null ? "search" : (String) req.get("mode");
83
+ int take = req.get("take") == null ? 0 : ((Double) req.get("take")).intValue();
84
+ List<Object> inputs = req.get("inputs") == null ? new ArrayList<>() : (List<Object>) req.get("inputs");
85
+ Pattern rx; double compileMs;
86
+ try { long t0 = System.nanoTime(); rx = Pattern.compile((String) req.get("pattern"), flagsOf((String) req.get("flags"))); compileMs = (System.nanoTime() - t0) / 1e6; }
87
+ catch (Exception e) { emit("{\"error\":\"compile\",\"detail\":" + q(String.valueOf(e.getMessage()).split("\n")[0]) + "}"); return; }
88
+ int runs = req.get("runs") == null ? 3 : ((Double) req.get("runs")).intValue();
89
+ double budget = req.get("budget_ms") == null ? 1000 : (Double) req.get("budget_ms");
90
+ int[] n = new int[1];
91
+ if ("time".equals(op)) {
92
+ for (int i = 0; i < inputs.size(); i++) {
93
+ String s = (String) inputs.get(i); StringBuilder ms = new StringBuilder(); boolean stop = false;
94
+ for (int k = 0; k < runs; k++) {
95
+ long t1 = System.nanoTime();
96
+ try { run(rx, mode, s, take, n); }
97
+ catch (StackOverflowError so) { emit("{\"i\":" + i + ",\"len\":" + s.length() + ",\"error\":\"stack_overflow\",\"ms\":[" + ((System.nanoTime() - t1) / 1e6) + "]}"); emit("{\"stopped\":" + i + "}"); return; }
98
+ double e = (System.nanoTime() - t1) / 1e6;
99
+ ms.append(k > 0 ? "," : "").append(e);
100
+ if (e > budget) { stop = true; break; }
101
+ }
102
+ emit("{\"i\":" + i + ",\"len\":" + s.length() + ",\"ms\":[" + ms + "],\"n\":" + n[0] + "}");
103
+ if (stop) { emit("{\"stopped\":" + i + "}"); return; }
104
+ }
105
+ } else if ("results".equals(op)) {
106
+ StringBuilder b = new StringBuilder("{\"results\":[");
107
+ for (int i = 0; i < inputs.size(); i++) {
108
+ String r;
109
+ try { r = run(rx, mode, (String) inputs.get(i), take, n); } catch (StackOverflowError so) { r = "\"stack_overflow\""; }
110
+ b.append(i > 0 ? "," : "").append(r);
111
+ }
112
+ emit(b.append("]}").toString());
113
+ } else if ("compile".equals(op)) {
114
+ int reps = req.get("n") == null ? 200 : ((Double) req.get("n")).intValue();
115
+ String s = inputs.isEmpty() ? "" : (String) inputs.get(0);
116
+ StringBuilder both = new StringBuilder(), only = new StringBuilder();
117
+ int f = flagsOf((String) req.get("flags"));
118
+ for (int k = 0; k < Math.max(runs, 1); k++) {
119
+ long t1 = System.nanoTime();
120
+ for (int j = 0; j < reps; j++) run(Pattern.compile((String) req.get("pattern"), f), mode, s, take, n);
121
+ both.append(k > 0 ? "," : "").append((System.nanoTime() - t1) / 1e6 / reps);
122
+ t1 = System.nanoTime();
123
+ for (int j = 0; j < reps; j++) run(rx, mode, s, take, n);
124
+ only.append(k > 0 ? "," : "").append((System.nanoTime() - t1) / 1e6 / reps);
125
+ }
126
+ emit("{\"compile_ms\":" + compileMs + ",\"per_call_compiled_each_ms\":[" + both + "],\"per_call_precompiled_ms\":[" + only + "]}");
127
+ }
128
+ }
129
+ }
@@ -0,0 +1,294 @@
1
+ // CodeCop's regex harness for Go's regexp (RE2 semantics). One JSON request on stdin, JSON lines on stdout (see
2
+ // run_python.py). Runs only the pattern and strings it is given; it imports nothing from the repository.
3
+ package main
4
+
5
+ import (
6
+ "encoding/json"
7
+ "fmt"
8
+ "os"
9
+ "regexp"
10
+ "runtime"
11
+ "strings"
12
+ "time"
13
+ )
14
+
15
+ type req struct {
16
+ Op string `json:"op"`
17
+ Pattern string `json:"pattern"`
18
+ Flags string `json:"flags"`
19
+ Mode string `json:"mode"`
20
+ Inputs []string `json:"inputs"`
21
+ Runs int `json:"runs"`
22
+ BudgetMs float64 `json:"budget_ms"`
23
+ Take int `json:"take"`
24
+ N int `json:"n"`
25
+ Patterns []tpat `json:"patterns"`
26
+ Variant string `json:"variant"`
27
+ Results bool `json:"results"`
28
+ }
29
+
30
+ // one pattern of a table, with the literals every match must contain (read from its tree by CodeCop)
31
+ type tpat struct {
32
+ Pattern string `json:"pattern"`
33
+ Flags string `json:"flags"`
34
+ Literals []string `json:"literals"`
35
+ CI bool `json:"ci"`
36
+ }
37
+
38
+ // fold: the text a case-insensitive literal is looked for in (Go folds k with the Kelvin sign and s with the long s)
39
+ func fold(s string) string {
40
+ s = strings.NewReplacer("\u0130", "i", "\u0131", "i", "\u017f", "s", "\u212a", "k").Replace(s)
41
+ return strings.ToLower(s)
42
+ }
43
+
44
+ // table: every pattern over one text, as the code runs it (plain) or only when one of its literals is in the text
45
+ func table(r req) {
46
+ type cp struct {
47
+ rx *regexp.Regexp
48
+ lits []string
49
+ ci bool
50
+ }
51
+ ps := []cp{}
52
+ for _, p := range r.Patterns {
53
+ x, err := compile(req{Pattern: p.Pattern, Flags: p.Flags, Mode: r.Mode})
54
+ if err != nil {
55
+ emit(map[string]string{"error": "compile", "detail": fmt.Sprint(err)})
56
+ return
57
+ }
58
+ ps = append(ps, cp{x, p.Literals, p.CI})
59
+ }
60
+ one := func(s string) []interface{} {
61
+ o := make([]interface{}, 0, len(ps))
62
+ low, have := "", false
63
+ for _, p := range ps {
64
+ if r.Variant == "prefilter" && len(p.lits) > 0 {
65
+ hay := s
66
+ if p.ci {
67
+ if !have {
68
+ low, have = fold(s), true
69
+ }
70
+ hay = low
71
+ }
72
+ hit := false
73
+ for _, l := range p.lits {
74
+ if strings.Contains(hay, l) {
75
+ hit = true
76
+ break
77
+ }
78
+ }
79
+ if !hit {
80
+ o = append(o, empty(r.Mode))
81
+ continue
82
+ }
83
+ }
84
+ o = append(o, run(p.rx, r.Mode, s, r.Take))
85
+ }
86
+ return o
87
+ }
88
+ if r.Results {
89
+ o := []interface{}{}
90
+ for _, s := range r.Inputs {
91
+ o = append(o, one(s))
92
+ }
93
+ emit(map[string]interface{}{"results": o})
94
+ return
95
+ }
96
+ for i, s := range r.Inputs {
97
+ ms := []float64{}
98
+ stop := false
99
+ for k := 0; k < r.Runs; k++ {
100
+ t := time.Now()
101
+ one(s)
102
+ e := float64(time.Since(t).Nanoseconds()) / 1e6
103
+ ms = append(ms, e)
104
+ if e > r.BudgetMs {
105
+ stop = true
106
+ break
107
+ }
108
+ }
109
+ emit(map[string]interface{}{"i": i, "len": len(s), "ms": ms})
110
+ if stop {
111
+ emit(map[string]int{"stopped": i})
112
+ return
113
+ }
114
+ }
115
+ }
116
+
117
+ func empty(mode string) interface{} {
118
+ switch mode {
119
+ case "bool":
120
+ return false
121
+ case "count":
122
+ return 0
123
+ case "all":
124
+ return []interface{}{}
125
+ }
126
+ return nil
127
+ }
128
+
129
+ var out = json.NewEncoder(os.Stdout)
130
+
131
+ func emit(v interface{}) { out.Encode(v) }
132
+
133
+ func compile(r req) (*regexp.Regexp, error) {
134
+ p := r.Pattern
135
+ f := ""
136
+ for _, c := range r.Flags {
137
+ if strings.ContainsRune("imsU", c) {
138
+ f += string(c)
139
+ }
140
+ }
141
+ if f != "" {
142
+ p = "(?" + f + ")" + p
143
+ }
144
+ if r.Mode == "full" {
145
+ p = "^(?:" + p + ")$"
146
+ }
147
+ return regexp.Compile(p)
148
+ }
149
+
150
+ func spans(loc []int) []interface{} {
151
+ if loc == nil {
152
+ return nil
153
+ }
154
+ o := []interface{}{loc[0], loc[1]}
155
+ for g := 2; g+1 < len(loc) && g < 42; g += 2 {
156
+ if loc[g] < 0 {
157
+ o = append(o, nil)
158
+ } else {
159
+ o = append(o, []int{loc[g], loc[g+1]})
160
+ }
161
+ }
162
+ return o
163
+ }
164
+
165
+ func run(rx *regexp.Regexp, mode string, s string, take int) interface{} {
166
+ switch mode {
167
+ case "bool":
168
+ return rx.MatchString(s)
169
+ case "match":
170
+ loc := rx.FindStringSubmatchIndex(s)
171
+ if loc == nil || loc[0] != 0 {
172
+ return nil
173
+ }
174
+ return spans(loc)
175
+ case "full", "search":
176
+ return spans(rx.FindStringSubmatchIndex(s))
177
+ case "count":
178
+ n := -1
179
+ if take > 0 {
180
+ n = take
181
+ }
182
+ return len(rx.FindAllStringIndex(s, n))
183
+ }
184
+ all := rx.FindAllStringSubmatchIndex(s, 50)
185
+ o := []interface{}{}
186
+ for _, l := range all {
187
+ o = append(o, spans(l))
188
+ }
189
+ return o
190
+ }
191
+
192
+ func count(r interface{}, mode string) interface{} {
193
+ switch v := r.(type) {
194
+ case int, bool:
195
+ return v
196
+ case []interface{}:
197
+ if mode == "all" {
198
+ return len(v)
199
+ }
200
+ if v == nil {
201
+ return 0
202
+ }
203
+ return 1
204
+ }
205
+ return 0
206
+ }
207
+
208
+ func main() {
209
+ var r req
210
+ if err := json.NewDecoder(os.Stdin).Decode(&r); err != nil {
211
+ emit(map[string]string{"error": "request", "detail": err.Error()})
212
+ return
213
+ }
214
+ if r.Op == "version" {
215
+ emit(map[string]string{"engine": "go-regexp", "version": runtime.Version()})
216
+ return
217
+ }
218
+ if r.Runs == 0 {
219
+ r.Runs = 3
220
+ }
221
+ if r.BudgetMs == 0 {
222
+ r.BudgetMs = 1000
223
+ }
224
+ if r.Op == "table" {
225
+ table(r)
226
+ return
227
+ }
228
+ t0 := time.Now()
229
+ rx, err := compile(r)
230
+ compileMs := float64(time.Since(t0).Nanoseconds()) / 1e6
231
+ if err != nil {
232
+ emit(map[string]string{"error": "compile", "detail": fmt.Sprint(err)})
233
+ return
234
+ }
235
+ if r.Runs == 0 {
236
+ r.Runs = 3
237
+ }
238
+ if r.BudgetMs == 0 {
239
+ r.BudgetMs = 1000
240
+ }
241
+ switch r.Op {
242
+ case "time":
243
+ for i, s := range r.Inputs {
244
+ ms := []float64{}
245
+ var res interface{}
246
+ stop := false
247
+ for k := 0; k < r.Runs; k++ {
248
+ t := time.Now()
249
+ res = run(rx, r.Mode, s, r.Take)
250
+ e := float64(time.Since(t).Nanoseconds()) / 1e6
251
+ ms = append(ms, e)
252
+ if e > r.BudgetMs {
253
+ stop = true
254
+ break
255
+ }
256
+ }
257
+ emit(map[string]interface{}{"i": i, "len": len(s), "ms": ms, "n": count(res, r.Mode)})
258
+ if stop {
259
+ emit(map[string]int{"stopped": i})
260
+ return
261
+ }
262
+ }
263
+ case "results":
264
+ o := []interface{}{}
265
+ for _, s := range r.Inputs {
266
+ o = append(o, run(rx, r.Mode, s, r.Take))
267
+ }
268
+ emit(map[string]interface{}{"results": o})
269
+ case "compile":
270
+ n := r.N
271
+ if n == 0 {
272
+ n = 200
273
+ }
274
+ s := ""
275
+ if len(r.Inputs) > 0 {
276
+ s = r.Inputs[0]
277
+ }
278
+ both, only := []float64{}, []float64{}
279
+ for k := 0; k < r.Runs; k++ {
280
+ t := time.Now()
281
+ for j := 0; j < n; j++ {
282
+ x, _ := compile(r)
283
+ run(x, r.Mode, s, r.Take)
284
+ }
285
+ both = append(both, float64(time.Since(t).Nanoseconds())/1e6/float64(n))
286
+ t = time.Now()
287
+ for j := 0; j < n; j++ {
288
+ run(rx, r.Mode, s, r.Take)
289
+ }
290
+ only = append(only, float64(time.Since(t).Nanoseconds())/1e6/float64(n))
291
+ }
292
+ emit(map[string]interface{}{"compile_ms": compileMs, "per_call_compiled_each_ms": both, "per_call_precompiled_ms": only})
293
+ }
294
+ }
@@ -0,0 +1,99 @@
1
+ // CodeCop's regex harness for Node (V8 irregexp). One JSON request on stdin, JSON lines on stdout (see run_python.py).
2
+ // Runs only the pattern and strings it is given; it loads nothing from the repository.
3
+ "use strict";
4
+ const { performance } = require("perf_hooks");
5
+
6
+ function emit(o) { process.stdout.write(JSON.stringify(o) + "\n"); }
7
+
8
+ function spans(m, limit) {
9
+ if (!m) return null;
10
+ const out = [m.index, m.index + m[0].length];
11
+ for (let g = 1; g < Math.min(m.length, limit + 1); g++) out.push(m[g] === undefined ? null : [-1, m[g].length]);
12
+ return out;
13
+ }
14
+
15
+ function make(pattern, flags, mode) {
16
+ let f = (flags || "").replace(/[gy]/g, "");
17
+ if (mode === "all" || mode === "count") f += "g";
18
+ if (mode === "full") return new RegExp("^(?:" + pattern + ")$", f);
19
+ if (mode === "match") return new RegExp("^(?:" + pattern + ")", f);
20
+ return new RegExp(pattern, f);
21
+ }
22
+
23
+ let run = function (rx, mode, s, take, limit) {
24
+ if (mode === "bool") { rx.lastIndex = 0; return rx.test(s); }
25
+ if (mode === "all" || mode === "count") {
26
+ rx.lastIndex = 0;
27
+ const out = []; let n = 0;
28
+ for (const m of s.matchAll(rx)) {
29
+ n++;
30
+ if (mode === "count") { if (take && n >= take) break; continue; }
31
+ if (out.length < limit) out.push(spans(m, 20)); else break;
32
+ }
33
+ return mode === "count" ? n : out;
34
+ }
35
+ rx.lastIndex = 0;
36
+ return spans(rx.exec(s), 20);
37
+ };
38
+
39
+ let buf = "";
40
+ process.stdin.setEncoding("utf8");
41
+ process.stdin.on("data", (d) => { buf += d; });
42
+ process.stdin.on("end", () => {
43
+ const req = JSON.parse(buf);
44
+ if (req.op === "version") { emit({ engine: "node-v8", version: process.versions.node + " (V8 " + process.versions.v8 + ")" }); return; }
45
+ const mode = req.mode || "search", take = req.take || null;
46
+ let rx, compileMs;
47
+ try {
48
+ const t0 = performance.now();
49
+ rx = make(req.pattern, req.flags, mode);
50
+ compileMs = performance.now() - t0;
51
+ } catch (e) { emit({ error: "compile", detail: String(e.message).slice(0, 200) }); return; }
52
+ if (req.code) {
53
+ // CodeCop's own helper (the restart skip), from the exact text the hand-off shows: a generator of the same matches
54
+ let gen;
55
+ try { gen = new Function(req.code + "\nreturn " + (req.entry || "fixed") + ";")(); }
56
+ catch (e) { emit({ error: "code", detail: String(e.message).slice(0, 200) }); return; }
57
+ run = (rx_, mode_, s, take_, limit) => {
58
+ if (mode_ === "bool" || mode_ === "search") {
59
+ const first = gen(s).next();
60
+ const m = first.done ? null : first.value;
61
+ return mode_ === "bool" ? m !== null : spans(m, 20);
62
+ }
63
+ const out = []; let n = 0;
64
+ for (const m of gen(s)) {
65
+ n++;
66
+ if (mode_ === "count") { if (take_ && n >= take_) break; continue; }
67
+ if (out.length < limit) out.push(spans(m, 20)); else break;
68
+ }
69
+ return mode_ === "count" ? n : out;
70
+ };
71
+ }
72
+ if (req.op === "time") {
73
+ const runs = req.runs || 3, budget = req.budget_ms || 1000;
74
+ for (let i = 0; i < req.inputs.length; i++) {
75
+ const s = req.inputs[i]; const ms = []; let stop = false, r = null;
76
+ for (let k = 0; k < runs; k++) {
77
+ const t = performance.now(); r = run(rx, mode, s, take, 50); const e = performance.now() - t;
78
+ ms.push(e);
79
+ if (e > budget) { stop = true; break; }
80
+ }
81
+ emit({ i, len: s.length, ms, n: typeof r === "number" || typeof r === "boolean" ? r : (Array.isArray(r) && mode === "all" ? r.length : (r ? 1 : 0)) });
82
+ if (stop) { emit({ stopped: i }); return; }
83
+ }
84
+ } else if (req.op === "results") {
85
+ emit({ results: req.inputs.map((s) => run(rx, mode, s, take, 50)) });
86
+ } else if (req.op === "compile") {
87
+ const n = req.n || 200, runs = req.runs || 5, s = (req.inputs || [""])[0];
88
+ const both = [], only = [];
89
+ for (let k = 0; k < runs; k++) {
90
+ let t = performance.now();
91
+ for (let j = 0; j < n; j++) run(make(req.pattern, req.flags, mode), mode, s, take, 50);
92
+ both.push((performance.now() - t) / n);
93
+ t = performance.now();
94
+ for (let j = 0; j < n; j++) run(rx, mode, s, take, 50);
95
+ only.push((performance.now() - t) / n);
96
+ }
97
+ emit({ compile_ms: compileMs, per_call_compiled_each_ms: both, per_call_precompiled_ms: only });
98
+ }
99
+ });